index.js 7.0 KB

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