index.js 6.8 KB

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