index.js 8.6 KB

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