index.js 7.1 KB

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