index.js 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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. useBeforeChange: {
  83. type: Boolean,
  84. value: false,
  85. },
  86. },
  87. data: {
  88. tabs: [],
  89. scrollLeft: 0,
  90. scrollable: false,
  91. currentIndex: 0,
  92. container: null,
  93. skipTransition: true,
  94. scrollWithAnimation: false,
  95. lineOffsetLeft: 0,
  96. inited: false,
  97. },
  98. mounted() {
  99. requestAnimationFrame(() => {
  100. this.swiping = true;
  101. this.setData({
  102. container: () => this.createSelectorQuery().select('.van-tabs'),
  103. });
  104. this.resize();
  105. this.scrollIntoView();
  106. });
  107. },
  108. methods: {
  109. updateTabs() {
  110. const { children = [], data } = this;
  111. this.setData({
  112. tabs: children.map((child) => child.data),
  113. scrollable: this.children.length > data.swipeThreshold || !data.ellipsis,
  114. });
  115. this.setCurrentIndexByName(data.active || this.getCurrentName());
  116. },
  117. trigger(eventName, child) {
  118. const { currentIndex } = this.data;
  119. const data = this.getChildData(currentIndex, child);
  120. if (!isDef(data)) {
  121. return;
  122. }
  123. this.$emit(eventName, data);
  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. return;
  131. }
  132. this.onBeforeChange(index).then(() => {
  133. this.setCurrentIndex(index);
  134. nextTick(() => {
  135. this.trigger('click');
  136. });
  137. });
  138. },
  139. // correct the index of active tab
  140. setCurrentIndexByName(name) {
  141. const { children = [] } = this;
  142. const matched = children.filter((child) => child.getComputedName() === name);
  143. if (matched.length) {
  144. this.setCurrentIndex(matched[0].index);
  145. }
  146. },
  147. setCurrentIndex(currentIndex) {
  148. const { data, children = [] } = this;
  149. if (!isDef(currentIndex) ||
  150. currentIndex >= children.length ||
  151. currentIndex < 0) {
  152. return;
  153. }
  154. groupSetData(this, () => {
  155. children.forEach((item, index) => {
  156. const active = index === currentIndex;
  157. if (active !== item.data.active || !item.inited) {
  158. item.updateRender(active, this);
  159. }
  160. });
  161. });
  162. if (currentIndex === data.currentIndex) {
  163. if (!data.inited) {
  164. this.resize();
  165. }
  166. return;
  167. }
  168. const shouldEmitChange = data.currentIndex !== null;
  169. this.setData({ currentIndex });
  170. requestAnimationFrame(() => {
  171. this.resize();
  172. this.scrollIntoView();
  173. });
  174. nextTick(() => {
  175. this.trigger('input');
  176. if (shouldEmitChange) {
  177. this.trigger('change');
  178. }
  179. });
  180. },
  181. getCurrentName() {
  182. const activeTab = this.children[this.data.currentIndex];
  183. if (activeTab) {
  184. return activeTab.getComputedName();
  185. }
  186. },
  187. resize() {
  188. if (this.data.type !== 'line') {
  189. return;
  190. }
  191. const { currentIndex, ellipsis, skipTransition } = this.data;
  192. Promise.all([
  193. getAllRect(this, '.van-tab'),
  194. getRect(this, '.van-tabs__line'),
  195. ]).then(([rects = [], lineRect]) => {
  196. const rect = rects[currentIndex];
  197. if (rect == null) {
  198. return;
  199. }
  200. let lineOffsetLeft = rects
  201. .slice(0, currentIndex)
  202. .reduce((prev, curr) => prev + curr.width, 0);
  203. lineOffsetLeft +=
  204. (rect.width - lineRect.width) / 2 + (ellipsis ? 0 : 8);
  205. this.setData({ lineOffsetLeft, inited: true });
  206. this.swiping = true;
  207. if (skipTransition) {
  208. // waiting transition end
  209. setTimeout(() => {
  210. this.setData({ skipTransition: false });
  211. }, this.data.duration);
  212. }
  213. });
  214. },
  215. // scroll active tab into view
  216. scrollIntoView() {
  217. const { currentIndex, scrollable, scrollWithAnimation } = this.data;
  218. if (!scrollable) {
  219. return;
  220. }
  221. Promise.all([
  222. getAllRect(this, '.van-tab'),
  223. getRect(this, '.van-tabs__nav'),
  224. ]).then(([tabRects, navRect]) => {
  225. const tabRect = tabRects[currentIndex];
  226. const offsetLeft = tabRects
  227. .slice(0, currentIndex)
  228. .reduce((prev, curr) => prev + curr.width, 0);
  229. this.setData({
  230. scrollLeft: offsetLeft - (navRect.width - tabRect.width) / 2,
  231. });
  232. if (!scrollWithAnimation) {
  233. nextTick(() => {
  234. this.setData({ scrollWithAnimation: true });
  235. });
  236. }
  237. });
  238. },
  239. onTouchScroll(event) {
  240. this.$emit('scroll', event.detail);
  241. },
  242. onTouchStart(event) {
  243. if (!this.data.swipeable)
  244. return;
  245. this.swiping = true;
  246. this.touchStart(event);
  247. },
  248. onTouchMove(event) {
  249. if (!this.data.swipeable || !this.swiping)
  250. return;
  251. this.touchMove(event);
  252. },
  253. // watch swipe touch end
  254. onTouchEnd() {
  255. if (!this.data.swipeable || !this.swiping)
  256. return;
  257. const { direction, deltaX, offsetX } = this;
  258. const minSwipeDistance = 50;
  259. if (direction === 'horizontal' && offsetX >= minSwipeDistance) {
  260. const index = this.getAvaiableTab(deltaX);
  261. if (index !== -1) {
  262. this.onBeforeChange(index).then(() => this.setCurrentIndex(index));
  263. }
  264. }
  265. this.swiping = false;
  266. },
  267. getAvaiableTab(direction) {
  268. const { tabs, currentIndex } = this.data;
  269. const step = direction > 0 ? -1 : 1;
  270. for (let i = step; currentIndex + i < tabs.length && currentIndex + i >= 0; i += step) {
  271. const index = currentIndex + i;
  272. if (index >= 0 &&
  273. index < tabs.length &&
  274. tabs[index] &&
  275. !tabs[index].disabled) {
  276. return index;
  277. }
  278. }
  279. return -1;
  280. },
  281. onBeforeChange(index) {
  282. const { useBeforeChange } = this.data;
  283. if (!useBeforeChange) {
  284. return Promise.resolve();
  285. }
  286. return new Promise((resolve, reject) => {
  287. this.$emit('before-change', Object.assign(Object.assign({}, this.getChildData(index)), { callback: (status) => (status ? resolve() : reject()) }));
  288. });
  289. },
  290. getChildData(index, child) {
  291. const currentChild = child || this.children[index];
  292. if (!isDef(currentChild)) {
  293. return;
  294. }
  295. return {
  296. index: currentChild.index,
  297. name: currentChild.getComputedName(),
  298. title: currentChild.data.title,
  299. };
  300. },
  301. },
  302. });