index.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. import { VantComponent } from '../common/component';
  2. import { touch } from '../mixins/touch';
  3. import { isDef, addUnit } 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
  17. .map((child, index) => {
  18. child.index = index;
  19. return child;
  20. });
  21. this.updateTabs();
  22. }
  23. },
  24. props: {
  25. color: {
  26. type: String,
  27. observer: 'setLine'
  28. },
  29. sticky: Boolean,
  30. animated: {
  31. type: Boolean,
  32. observer() {
  33. this.children.forEach((child, index) => child.updateRender(index === this.data.currentIndex, this));
  34. }
  35. },
  36. swipeable: Boolean,
  37. lineWidth: {
  38. type: [String, Number],
  39. value: -1,
  40. observer: 'setLine'
  41. },
  42. lineHeight: {
  43. type: [String, Number],
  44. value: -1,
  45. observer: 'setLine'
  46. },
  47. titleActiveColor: String,
  48. titleInactiveColor: String,
  49. active: {
  50. type: [String, Number],
  51. value: 0,
  52. observer(name) {
  53. if (name !== this.getCurrentName()) {
  54. this.setCurrentIndexByName(name);
  55. }
  56. }
  57. },
  58. type: {
  59. type: String,
  60. value: 'line'
  61. },
  62. border: {
  63. type: Boolean,
  64. value: true
  65. },
  66. ellipsis: {
  67. type: Boolean,
  68. value: true
  69. },
  70. duration: {
  71. type: Number,
  72. value: 0.3
  73. },
  74. zIndex: {
  75. type: Number,
  76. value: 1
  77. },
  78. swipeThreshold: {
  79. type: Number,
  80. value: 4,
  81. observer(value) {
  82. this.setData({
  83. scrollable: this.children.length > value || !this.data.ellipsis
  84. });
  85. }
  86. },
  87. offsetTop: {
  88. type: Number,
  89. value: 0
  90. },
  91. lazyRender: {
  92. type: Boolean,
  93. value: true
  94. }
  95. },
  96. data: {
  97. tabs: [],
  98. lineStyle: '',
  99. scrollLeft: 0,
  100. scrollable: false,
  101. trackStyle: '',
  102. currentIndex: null,
  103. container: null
  104. },
  105. mounted() {
  106. wx.nextTick(() => {
  107. this.setLine(true);
  108. this.scrollIntoView();
  109. });
  110. },
  111. methods: {
  112. updateContainer() {
  113. this.setData({
  114. container: () => this.createSelectorQuery().select('.van-tabs')
  115. });
  116. },
  117. updateTabs() {
  118. const { children = [], data } = this;
  119. this.setData({
  120. tabs: children.map((child) => child.data),
  121. scrollable: this.children.length > data.swipeThreshold || !data.ellipsis
  122. });
  123. this.setCurrentIndexByName(this.getCurrentName() || data.active);
  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. }
  143. else {
  144. this.setCurrentIndex(index);
  145. wx.nextTick(() => {
  146. this.trigger('click');
  147. });
  148. }
  149. },
  150. // correct the index of active tab
  151. setCurrentIndexByName(name) {
  152. const { children = [] } = this;
  153. const matched = children.filter((child) => child.getComputedName() === name);
  154. if (matched.length) {
  155. this.setCurrentIndex(matched[0].index);
  156. }
  157. },
  158. setCurrentIndex(currentIndex) {
  159. const { data, children = [] } = this;
  160. if (!isDef(currentIndex) ||
  161. currentIndex >= children.length ||
  162. currentIndex < 0) {
  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) {
  193. if (this.data.type !== 'line') {
  194. return;
  195. }
  196. const { color, duration, currentIndex, lineWidth, lineHeight } = this.data;
  197. this.getRect('.van-tab', true).then((rects = []) => {
  198. const rect = rects[currentIndex];
  199. if (rect == null) {
  200. return;
  201. }
  202. const width = lineWidth !== -1 ? lineWidth : rect.width / 2;
  203. const height = lineHeight !== -1
  204. ? `height: ${addUnit(lineHeight)}; border-radius: ${addUnit(lineHeight)};`
  205. : '';
  206. let left = rects
  207. .slice(0, currentIndex)
  208. .reduce((prev, curr) => prev + curr.width, 0);
  209. left += (rect.width - width) / 2;
  210. const transition = skipTransition
  211. ? ''
  212. : `transition-duration: ${duration}s; -webkit-transition-duration: ${duration}s;`;
  213. this.setData({
  214. lineStyle: `
  215. ${height}
  216. width: ${addUnit(width)};
  217. background-color: ${color};
  218. -webkit-transform: translateX(${left}px);
  219. transform: translateX(${left}px);
  220. ${transition}
  221. `
  222. });
  223. });
  224. },
  225. // scroll active tab into view
  226. scrollIntoView() {
  227. const { currentIndex, scrollable } = this.data;
  228. if (!scrollable) {
  229. return;
  230. }
  231. Promise.all([
  232. this.getRect('.van-tab', true),
  233. this.getRect('.van-tabs__nav')
  234. ]).then(([tabRects, navRect]) => {
  235. const tabRect = tabRects[currentIndex];
  236. const offsetLeft = tabRects
  237. .slice(0, currentIndex)
  238. .reduce((prev, curr) => prev + curr.width, 0);
  239. this.setData({
  240. scrollLeft: offsetLeft - (navRect.width - tabRect.width) / 2
  241. });
  242. });
  243. },
  244. onTouchScroll(event) {
  245. this.$emit('scroll', event.detail);
  246. },
  247. onTouchStart(event) {
  248. if (!this.data.swipeable)
  249. return;
  250. this.touchStart(event);
  251. },
  252. onTouchMove(event) {
  253. if (!this.data.swipeable)
  254. return;
  255. this.touchMove(event);
  256. },
  257. // watch swipe touch end
  258. onTouchEnd() {
  259. if (!this.data.swipeable)
  260. return;
  261. const { tabs, currentIndex } = this.data;
  262. const { direction, deltaX, offsetX } = this;
  263. const minSwipeDistance = 50;
  264. if (direction === 'horizontal' && offsetX >= minSwipeDistance) {
  265. if (deltaX > 0 && currentIndex !== 0) {
  266. this.setCurrentIndex(currentIndex - 1);
  267. }
  268. else if (deltaX < 0 && currentIndex !== tabs.length - 1) {
  269. this.setCurrentIndex(currentIndex + 1);
  270. }
  271. }
  272. }
  273. }
  274. });