index.js 8.7KB

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