index.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. <template>
  2. <uni-shadow-root class="vant-tabs-index"><view :class="'custom-class '+(utils.bem('tabs', [type]))">
  3. <van-sticky :disabled="(!sticky)" :z-index="zIndex" :offset-top="offsetTop" :container="container" @scroll="onTouchScroll">
  4. <view :class="(utils.bem('tabs__wrap', { scrollable }))+' '+(type === 'line' && border ? 'van-hairline--top-bottom' : '')">
  5. <slot name="nav-left"></slot>
  6. <scroll-view :scroll-x="scrollable" scroll-with-animation :scroll-left="scrollLeft" :class="utils.bem('tabs__scroll', [type])" :style="color ? 'border-color: ' + color : ''">
  7. <view :class="(utils.bem('tabs__nav', [type]))+' nav-class'">
  8. <view v-if="type === 'line'" class="van-tabs__line" :style="lineStyle"></view>
  9. <view v-for="(item,index) in (tabs)" :key="item.index" :data-index="index" :class="(getters.tabClass(index === currentIndex, ellipsis))+' '+(utils.bem('tab', { active: index === currentIndex, disabled: item.disabled, complete: !ellipsis }))" :style="getters.tabStyle(index === currentIndex, ellipsis, color, type, item.disabled, titleActiveColor, titleInactiveColor, swipeThreshold, scrollable)" @click="onTap">
  10. <view :class="ellipsis ? 'van-ellipsis' : ''" :style="item.titleStyle">
  11. {{ item.title }}
  12. <van-info v-if="item.info !== null || item.dot" :info="item.info" :dot="item.dot" custom-class="van-tab__title__info"></van-info>
  13. </view>
  14. </view>
  15. </view>
  16. </scroll-view>
  17. <slot name="nav-right"></slot>
  18. </view>
  19. </van-sticky>
  20. <view class="van-tabs__content" @touchstart="onTouchStart" @touchmove="onTouchMove" @touchend="onTouchEnd" @touchcancel="onTouchEnd">
  21. <view :class="(utils.bem('tabs__track', [{ animated }]))+' van-tabs__track'" :style="trackStyle">
  22. <slot></slot>
  23. </view>
  24. </view>
  25. </view></uni-shadow-root>
  26. </template>
  27. <wxs src="../wxs/utils.wxs" module="utils"></wxs><wxs src="./index.wxs" module="getters"></wxs>
  28. <script>
  29. import VanInfo from '../info/index.vue'
  30. import VanSticky from '../sticky/index.vue'
  31. global['__wxVueOptions'] = {components:{'van-info': VanInfo,'van-sticky': VanSticky}}
  32. global['__wxRoute'] = 'vant/tabs/index'
  33. import { VantComponent } from '../common/component';
  34. import { touch } from '../mixins/touch';
  35. import { isDef, addUnit } from '../common/utils';
  36. VantComponent({
  37. mixins: [touch],
  38. classes: ['nav-class', 'tab-class', 'tab-active-class', 'line-class'],
  39. relation: {
  40. name: 'tab',
  41. type: 'descendant',
  42. linked(target) {
  43. target.index = this.children.length;
  44. this.children.push(target);
  45. this.updateTabs();
  46. },
  47. unlinked(target) {
  48. this.children = this.children
  49. .filter((child) => child !== target)
  50. .map((child, index) => {
  51. child.index = index;
  52. return child;
  53. });
  54. this.updateTabs();
  55. }
  56. },
  57. props: {
  58. color: {
  59. type: String,
  60. observer: 'setLine'
  61. },
  62. sticky: Boolean,
  63. animated: {
  64. type: Boolean,
  65. observer() {
  66. this.setTrack();
  67. this.children.forEach((child) => child.updateRender());
  68. }
  69. },
  70. swipeable: Boolean,
  71. lineWidth: {
  72. type: [String, Number],
  73. value: -1,
  74. observer: 'setLine'
  75. },
  76. lineHeight: {
  77. type: [String, Number],
  78. value: -1,
  79. observer: 'setLine'
  80. },
  81. titleActiveColor: String,
  82. titleInactiveColor: String,
  83. active: {
  84. type: [String, Number],
  85. value: 0,
  86. observer(name) {
  87. if (name !== this.getCurrentName()) {
  88. this.setCurrentIndexByName(name);
  89. }
  90. }
  91. },
  92. type: {
  93. type: String,
  94. value: 'line'
  95. },
  96. border: {
  97. type: Boolean,
  98. value: true
  99. },
  100. ellipsis: {
  101. type: Boolean,
  102. value: true
  103. },
  104. duration: {
  105. type: Number,
  106. value: 0.3
  107. },
  108. zIndex: {
  109. type: Number,
  110. value: 1
  111. },
  112. swipeThreshold: {
  113. type: Number,
  114. value: 4,
  115. observer(value) {
  116. this.setData({
  117. scrollable: this.children.length > value || !this.data.ellipsis
  118. });
  119. }
  120. },
  121. offsetTop: {
  122. type: Number,
  123. value: 0
  124. },
  125. lazyRender: {
  126. type: Boolean,
  127. value: true
  128. },
  129. },
  130. data: {
  131. tabs: [],
  132. lineStyle: '',
  133. scrollLeft: 0,
  134. scrollable: false,
  135. trackStyle: '',
  136. currentIndex: null,
  137. container: null
  138. },
  139. beforeCreate() {
  140. this.children = [];
  141. },
  142. mounted() {
  143. this.setData({
  144. container: () => this.createSelectorQuery().select('.van-tabs')
  145. });
  146. this.setLine(true);
  147. this.setTrack();
  148. this.scrollIntoView();
  149. },
  150. methods: {
  151. updateTabs() {
  152. const { children = [], data } = this;
  153. this.setData({
  154. tabs: children.map((child) => child.data),
  155. scrollable: this.children.length > data.swipeThreshold || !data.ellipsis
  156. });
  157. this.setCurrentIndexByName(this.getCurrentName() || data.active);
  158. },
  159. trigger(eventName) {
  160. const { currentIndex } = this.data;
  161. const child = this.children[currentIndex];
  162. if (!isDef(child)) {
  163. return;
  164. }
  165. this.$emit(eventName, {
  166. index: currentIndex,
  167. name: child.getComputedName(),
  168. title: child.data.title
  169. });
  170. },
  171. onTap(event) {
  172. const { index } = event.currentTarget.dataset;
  173. const child = this.children[index];
  174. if (child.data.disabled) {
  175. this.trigger('disabled');
  176. }
  177. else {
  178. this.setCurrentIndex(index);
  179. wx.nextTick(() => {
  180. this.trigger('click');
  181. });
  182. }
  183. },
  184. // correct the index of active tab
  185. setCurrentIndexByName(name) {
  186. const { children = [] } = this;
  187. const matched = children.filter((child) => child.getComputedName() === name);
  188. if (matched.length) {
  189. this.setCurrentIndex(matched[0].index);
  190. }
  191. },
  192. setCurrentIndex(currentIndex) {
  193. const { data, children = [] } = this;
  194. if (!isDef(currentIndex) ||
  195. currentIndex >= children.length ||
  196. currentIndex < 0) {
  197. return;
  198. }
  199. children.forEach((item, index) => {
  200. const active = index === currentIndex;
  201. if (active !== item.data.active || !item.inited) {
  202. item.updateRender(active, this);
  203. }
  204. });
  205. if (currentIndex === data.currentIndex) {
  206. return;
  207. }
  208. const shouldEmitChange = data.currentIndex !== null;
  209. this.setData({ currentIndex });
  210. wx.nextTick(() => {
  211. this.setLine();
  212. this.setTrack();
  213. this.scrollIntoView();
  214. this.trigger('input');
  215. if (shouldEmitChange) {
  216. this.trigger('change');
  217. }
  218. });
  219. },
  220. getCurrentName() {
  221. const activeTab = this.children[this.data.currentIndex];
  222. if (activeTab) {
  223. return activeTab.getComputedName();
  224. }
  225. },
  226. setLine(skipTransition) {
  227. if (this.data.type !== 'line') {
  228. return;
  229. }
  230. const { color, duration, currentIndex, lineWidth, lineHeight } = this.data;
  231. this.getRect('.van-tab', true).then((rects = []) => {
  232. const rect = rects[currentIndex];
  233. if (rect == null) {
  234. return;
  235. }
  236. const width = lineWidth !== -1 ? lineWidth : rect.width / 2;
  237. const height = lineHeight !== -1
  238. ? `height: ${addUnit(lineHeight)}; border-radius: ${addUnit(lineHeight)};`
  239. : '';
  240. let left = rects
  241. .slice(0, currentIndex)
  242. .reduce((prev, curr) => prev + curr.width, 0);
  243. left += (rect.width - width) / 2;
  244. const transition = skipTransition
  245. ? ''
  246. : `transition-duration: ${duration}s; -webkit-transition-duration: ${duration}s;`;
  247. this.setData({
  248. lineStyle: `
  249. ${height}
  250. width: ${addUnit(width)};
  251. background-color: ${color};
  252. -webkit-transform: translateX(${left}px);
  253. transform: translateX(${left}px);
  254. ${transition}
  255. `
  256. });
  257. });
  258. },
  259. setTrack() {
  260. const { animated, duration, currentIndex } = this.data;
  261. if (!animated) {
  262. return;
  263. }
  264. this.setData({
  265. trackStyle: `
  266. transform: translate3d(${-100 * currentIndex}%, 0, 0);
  267. -webkit-transition-duration: ${duration}s;
  268. transition-duration: ${duration}s;
  269. `
  270. });
  271. },
  272. // scroll active tab into view
  273. scrollIntoView() {
  274. const { currentIndex, scrollable } = this.data;
  275. if (!scrollable) {
  276. return;
  277. }
  278. Promise.all([
  279. this.getRect('.van-tab', true),
  280. this.getRect('.van-tabs__nav')
  281. ]).then(([tabRects, navRect]) => {
  282. const tabRect = tabRects[currentIndex];
  283. const offsetLeft = tabRects
  284. .slice(0, currentIndex)
  285. .reduce((prev, curr) => prev + curr.width, 0);
  286. this.setData({
  287. scrollLeft: offsetLeft - (navRect.width - tabRect.width) / 2
  288. });
  289. });
  290. },
  291. onTouchScroll(event) {
  292. this.$emit('scroll', event.detail);
  293. },
  294. onTouchStart(event) {
  295. if (!this.data.swipeable)
  296. return;
  297. this.touchStart(event);
  298. },
  299. onTouchMove(event) {
  300. if (!this.data.swipeable)
  301. return;
  302. this.touchMove(event);
  303. },
  304. // watch swipe touch end
  305. onTouchEnd() {
  306. if (!this.data.swipeable)
  307. return;
  308. const { tabs, currentIndex } = this.data;
  309. const { direction, deltaX, offsetX } = this;
  310. const minSwipeDistance = 50;
  311. if (direction === 'horizontal' && offsetX >= minSwipeDistance) {
  312. if (deltaX > 0 && currentIndex !== 0) {
  313. this.setCurrentIndex(currentIndex - 1);
  314. }
  315. else if (deltaX < 0 && currentIndex !== tabs.length - 1) {
  316. this.setCurrentIndex(currentIndex + 1);
  317. }
  318. }
  319. }
  320. }
  321. });
  322. export default global['__wxComponents']['vant/tabs/index']
  323. </script>
  324. <style platform="mp-weixin">
  325. @import '../common/index.css';.van-tabs{position:relative;-webkit-tap-highlight-color:transparent}.van-tabs__wrap{display:-webkit-flex;display:flex;overflow:hidden}.van-tabs__wrap--scrollable .van-tab{-webkit-flex:0 0 22%;flex:0 0 22%}.van-tabs__scroll{background-color:#fff;background-color:var(--tabs-nav-background-color,#fff)}.van-tabs__scroll--line{box-sizing:initial;height:calc(100% + 15px)}.van-tabs__scroll--card{margin:0 16px;margin:0 var(--padding-md,16px)}.van-tabs__nav{position:relative;display:-webkit-flex;display:flex;-webkit-user-select:none;user-select:none}.van-tabs__nav--card{box-sizing:border-box;height:30px;height:var(--tabs-card-height,30px);border:1px solid #ee0a24;border:var(--border-width-base,1px) solid var(--tabs-default-color,#ee0a24);border-radius:2px;border-radius:var(--border-radius-sm,2px)}.van-tabs__nav--card .van-tab{color:#ee0a24;color:var(--tabs-default-color,#ee0a24);line-height:28px;line-height:calc(var(--tabs-card-height, 30px) - 2*var(--border-width-base, 1px));border-right:1px solid #ee0a24;border-right:var(--border-width-base,1px) solid var(--tabs-default-color,#ee0a24)}.van-tabs__nav--card .van-tab:last-child{border-right:none}.van-tabs__nav--card .van-tab.van-tab--active{color:#fff;color:var(--white,#fff);background-color:#ee0a24;background-color:var(--tabs-default-color,#ee0a24)}.van-tabs__nav--card .van-tab--disabled{color:#c8c9cc;color:var(--tab-disabled-text-color,#c8c9cc)}.van-tabs__line{position:absolute;bottom:0;left:0;z-index:1;height:3px;height:var(--tabs-bottom-bar-height,3px);border-radius:3px;border-radius:var(--tabs-bottom-bar-height,3px);background-color:#ee0a24;background-color:var(--tabs-bottom-bar-color,#ee0a24)}.van-tabs__track{position:relative;width:100%;height:100%}.van-tabs__track--animated{display:-webkit-flex;display:flex;transition-property:-webkit-transform;transition-property:transform;transition-property:transform,-webkit-transform}.van-tabs__content{overflow:hidden}.van-tabs--line .van-tabs__wrap{height:44px;height:var(--tabs-line-height,44px)}.van-tabs--card .van-tabs__wrap{height:30px;height:var(--tabs-card-height,30px)}.van-tab{position:relative;-webkit-flex:1;flex:1;box-sizing:border-box;min-width:0;padding:0 5px;text-align:center;cursor:pointer;color:#646566;color:var(--tab-text-color,#646566);font-size:14px;font-size:var(--tab-font-size,14px);line-height:44px;line-height:var(--tabs-line-height,44px)}.van-tab--active{font-weight:500;font-weight:var(--font-weight-bold,500);color:#323233;color:var(--tab-active-text-color,#323233)}.van-tab--disabled{color:#c8c9cc;color:var(--tab-disabled-text-color,#c8c9cc)}.van-tab--complete{-webkit-flex:1 0 auto!important;flex:1 0 auto!important}.van-tab__title__info{position:relative!important;top:-1px!important;display:inline-block;-webkit-transform:translateX(0)!important;transform:translateX(0)!important}
  326. </style>