index.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. import { VantComponent } from '../common/component';
  2. import { touch } from '../mixins/touch';
  3. import { getAllRect, getRect, groupSetData, nextTick, requestAnimationFrame, } from '../common/utils';
  4. import { isDef } from '../common/validator';
  5. import { useChildren } from '../common/relation';
  6. VantComponent({
  7. mixins: [touch],
  8. classes: [
  9. 'nav-class',
  10. 'tab-class',
  11. 'tab-active-class',
  12. 'line-class',
  13. 'wrap-class',
  14. ],
  15. relation: useChildren('tab', function () {
  16. this.updateTabs();
  17. }),
  18. props: {
  19. sticky: Boolean,
  20. border: Boolean,
  21. swipeable: Boolean,
  22. titleActiveColor: String,
  23. titleInactiveColor: String,
  24. color: String,
  25. animated: {
  26. type: Boolean,
  27. observer() {
  28. this.children.forEach((child, index) => child.updateRender(index === this.data.currentIndex, this));
  29. },
  30. },
  31. lineWidth: {
  32. type: null,
  33. value: 40,
  34. observer: 'resize',
  35. },
  36. lineHeight: {
  37. type: null,
  38. value: -1,
  39. },
  40. active: {
  41. type: null,
  42. value: 0,
  43. observer(name) {
  44. if (name !== this.getCurrentName()) {
  45. this.setCurrentIndexByName(name);
  46. }
  47. },
  48. },
  49. type: {
  50. type: String,
  51. value: 'line',
  52. },
  53. ellipsis: {
  54. type: Boolean,
  55. value: true,
  56. },
  57. duration: {
  58. type: Number,
  59. value: 0.3,
  60. },
  61. zIndex: {
  62. type: Number,
  63. value: 1,
  64. },
  65. swipeThreshold: {
  66. type: Number,
  67. value: 5,
  68. observer(value) {
  69. this.setData({
  70. scrollable: this.children.length > value || !this.data.ellipsis,
  71. });
  72. },
  73. },
  74. offsetTop: {
  75. type: Number,
  76. value: 0,
  77. },
  78. lazyRender: {
  79. type: Boolean,
  80. value: true,
  81. },
  82. },
  83. data: {
  84. tabs: [],
  85. scrollLeft: 0,
  86. scrollable: false,
  87. currentIndex: 0,
  88. container: null,
  89. skipTransition: true,
  90. scrollWithAnimation: false,
  91. lineOffsetLeft: 0,
  92. inited: false,
  93. },
  94. mounted() {
  95. requestAnimationFrame(() => {
  96. this.swiping = true;
  97. this.setData({
  98. container: () => this.createSelectorQuery().select('.van-tabs'),
  99. });
  100. this.resize();
  101. this.scrollIntoView();
  102. });
  103. },
  104. methods: {
  105. updateTabs() {
  106. const { children = [], data } = this;
  107. this.setData({
  108. tabs: children.map((child) => child.data),
  109. scrollable: this.children.length > data.swipeThreshold || !data.ellipsis,
  110. });
  111. this.setCurrentIndexByName(data.active || this.getCurrentName());
  112. },
  113. trigger(eventName, child) {
  114. const { currentIndex } = this.data;
  115. const currentChild = child || this.children[currentIndex];
  116. if (!isDef(currentChild)) {
  117. return;
  118. }
  119. this.$emit(eventName, {
  120. index: currentChild.index,
  121. name: currentChild.getComputedName(),
  122. title: currentChild.data.title,
  123. });
  124. },
  125. onTap(event) {
  126. const { index } = event.currentTarget.dataset;
  127. const child = this.children[index];
  128. if (child.data.disabled) {
  129. this.trigger('disabled', child);
  130. }
  131. else {
  132. this.setCurrentIndex(index);
  133. nextTick(() => {
  134. this.trigger('click');
  135. });
  136. }
  137. },
  138. // correct the index of active tab
  139. setCurrentIndexByName(name) {
  140. const { children = [] } = this;
  141. const matched = children.filter((child) => child.getComputedName() === name);
  142. if (matched.length) {
  143. this.setCurrentIndex(matched[0].index);
  144. }
  145. },
  146. setCurrentIndex(currentIndex) {
  147. const { data, children = [] } = this;
  148. if (!isDef(currentIndex) ||
  149. currentIndex >= children.length ||
  150. currentIndex < 0) {
  151. return;
  152. }
  153. groupSetData(this, () => {
  154. children.forEach((item, index) => {
  155. const active = index === currentIndex;
  156. if (active !== item.data.active || !item.inited) {
  157. item.updateRender(active, this);
  158. }
  159. });
  160. });
  161. if (currentIndex === data.currentIndex) {
  162. return;
  163. }
  164. const shouldEmitChange = data.currentIndex !== null;
  165. this.setData({ currentIndex });
  166. requestAnimationFrame(() => {
  167. this.resize();
  168. this.scrollIntoView();
  169. });
  170. nextTick(() => {
  171. this.trigger('input');
  172. if (shouldEmitChange) {
  173. this.trigger('change');
  174. }
  175. });
  176. },
  177. getCurrentName() {
  178. const activeTab = this.children[this.data.currentIndex];
  179. if (activeTab) {
  180. return activeTab.getComputedName();
  181. }
  182. },
  183. resize() {
  184. if (this.data.type !== 'line') {
  185. return;
  186. }
  187. const { currentIndex, ellipsis, skipTransition } = this.data;
  188. Promise.all([
  189. getAllRect(this, '.van-tab'),
  190. getRect(this, '.van-tabs__line'),
  191. ]).then(([rects = [], lineRect]) => {
  192. const rect = rects[currentIndex];
  193. if (rect == null) {
  194. return;
  195. }
  196. let lineOffsetLeft = rects
  197. .slice(0, currentIndex)
  198. .reduce((prev, curr) => prev + curr.width, 0);
  199. lineOffsetLeft +=
  200. (rect.width - lineRect.width) / 2 + (ellipsis ? 0 : 8);
  201. this.setData({ lineOffsetLeft, inited: true });
  202. this.swiping = true;
  203. if (skipTransition) {
  204. // waiting transition end
  205. setTimeout(() => {
  206. this.setData({ skipTransition: false });
  207. }, this.data.duration);
  208. }
  209. });
  210. },
  211. // scroll active tab into view
  212. scrollIntoView() {
  213. const { currentIndex, scrollable, scrollWithAnimation } = this.data;
  214. if (!scrollable) {
  215. return;
  216. }
  217. Promise.all([
  218. getAllRect(this, '.van-tab'),
  219. getRect(this, '.van-tabs__nav'),
  220. ]).then(([tabRects, navRect]) => {
  221. const tabRect = tabRects[currentIndex];
  222. const offsetLeft = tabRects
  223. .slice(0, currentIndex)
  224. .reduce((prev, curr) => prev + curr.width, 0);
  225. this.setData({
  226. scrollLeft: offsetLeft - (navRect.width - tabRect.width) / 2,
  227. });
  228. if (!scrollWithAnimation) {
  229. nextTick(() => {
  230. this.setData({ scrollWithAnimation: true });
  231. });
  232. }
  233. });
  234. },
  235. onTouchScroll(event) {
  236. this.$emit('scroll', event.detail);
  237. },
  238. onTouchStart(event) {
  239. if (!this.data.swipeable)
  240. return;
  241. this.swiping = true;
  242. this.touchStart(event);
  243. },
  244. onTouchMove(event) {
  245. if (!this.data.swipeable || !this.swiping)
  246. return;
  247. this.touchMove(event);
  248. },
  249. // watch swipe touch end
  250. onTouchEnd() {
  251. if (!this.data.swipeable || !this.swiping)
  252. return;
  253. const { direction, deltaX, offsetX } = this;
  254. const minSwipeDistance = 50;
  255. if (direction === 'horizontal' && offsetX >= minSwipeDistance) {
  256. const index = this.getAvaiableTab(deltaX);
  257. if (index !== -1) {
  258. this.setCurrentIndex(index);
  259. }
  260. }
  261. this.swiping = false;
  262. },
  263. getAvaiableTab(direction) {
  264. const { tabs, currentIndex } = this.data;
  265. const step = direction > 0 ? -1 : 1;
  266. for (let i = step; currentIndex + i < tabs.length && currentIndex + i >= 0; i += step) {
  267. const index = currentIndex + i;
  268. if (index >= 0 &&
  269. index < tabs.length &&
  270. tabs[index] &&
  271. !tabs[index].disabled) {
  272. return index;
  273. }
  274. }
  275. return -1;
  276. },
  277. },
  278. });