index.vue 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. <template>
  2. <uni-shadow-root class="vant-dist-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="scrollWithAnimation" :scroll-left="scrollLeft" :class="utils.bem('tabs__scroll', [type])" :style="color ? 'border-color: ' + color : ''">
  7. <view :class="(utils.bem('tabs__nav', [type, { complete: !ellipsis }]))+' nav-class'" :style="computed.navStyle(color, type)">
  8. <view v-if="type === 'line'" class="van-tabs__line" :style="computed.lineStyle({ color, lineOffsetLeft, lineHeight, skipTransition, duration, lineWidth })"></view>
  9. <view v-for="(item,index) in (tabs)" :key="item.index" :data-index="index" :class="(computed.tabClass(index === currentIndex, ellipsis))+' '+(utils.bem('tab', { active: index === currentIndex, disabled: item.disabled, complete: !ellipsis }))" :style="computed.tabStyle({ active: index === currentIndex, ellipsis, color, type, disabled: 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="computed.trackStyle({ duration, currentIndex, animated })">
  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="computed"></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/dist/tabs/index'
  33. import { VantComponent } from '../common/component';
  34. import { touch } from '../mixins/touch';
  35. import { getAllRect, getRect, groupSetData, nextTick, requestAnimationFrame, } from '../common/utils';
  36. import { isDef } from '../common/validator';
  37. import { useChildren } from '../common/relation';
  38. VantComponent({
  39. mixins: [touch],
  40. classes: ['nav-class', 'tab-class', 'tab-active-class', 'line-class'],
  41. relation: useChildren('tab', function () {
  42. this.updateTabs();
  43. }),
  44. props: {
  45. sticky: Boolean,
  46. border: Boolean,
  47. swipeable: Boolean,
  48. titleActiveColor: String,
  49. titleInactiveColor: String,
  50. color: String,
  51. animated: {
  52. type: Boolean,
  53. observer() {
  54. this.children.forEach((child, index) => child.updateRender(index === this.data.currentIndex, this));
  55. },
  56. },
  57. lineWidth: {
  58. type: null,
  59. value: 40,
  60. observer: 'resize',
  61. },
  62. lineHeight: {
  63. type: null,
  64. value: -1,
  65. },
  66. active: {
  67. type: null,
  68. value: 0,
  69. observer(name) {
  70. if (!this.skipInit) {
  71. this.skipInit = true;
  72. }
  73. if (name !== this.getCurrentName()) {
  74. this.setCurrentIndexByName(name);
  75. }
  76. },
  77. },
  78. type: {
  79. type: String,
  80. value: 'line',
  81. },
  82. ellipsis: {
  83. type: Boolean,
  84. value: true,
  85. },
  86. duration: {
  87. type: Number,
  88. value: 0.3,
  89. },
  90. zIndex: {
  91. type: Number,
  92. value: 1,
  93. },
  94. swipeThreshold: {
  95. type: Number,
  96. value: 5,
  97. observer(value) {
  98. this.setData({
  99. scrollable: this.children.length > value || !this.data.ellipsis,
  100. });
  101. },
  102. },
  103. offsetTop: {
  104. type: Number,
  105. value: 0,
  106. },
  107. lazyRender: {
  108. type: Boolean,
  109. value: true,
  110. },
  111. },
  112. data: {
  113. tabs: [],
  114. scrollLeft: 0,
  115. scrollable: false,
  116. currentIndex: 0,
  117. container: null,
  118. skipTransition: true,
  119. scrollWithAnimation: false,
  120. lineOffsetLeft: 0,
  121. },
  122. mounted() {
  123. requestAnimationFrame(() => {
  124. this.setData({
  125. container: () => this.createSelectorQuery().select('.van-tabs'),
  126. });
  127. if (!this.skipInit) {
  128. this.resize();
  129. this.scrollIntoView();
  130. }
  131. });
  132. },
  133. methods: {
  134. updateTabs() {
  135. const { children = [], data } = this;
  136. this.setData({
  137. tabs: children.map((child) => child.data),
  138. scrollable: this.children.length > data.swipeThreshold || !data.ellipsis,
  139. });
  140. this.setCurrentIndexByName(data.active || this.getCurrentName());
  141. },
  142. trigger(eventName, child) {
  143. const { currentIndex } = this.data;
  144. const currentChild = child || this.children[currentIndex];
  145. if (!isDef(currentChild)) {
  146. return;
  147. }
  148. this.$emit(eventName, {
  149. index: currentChild.index,
  150. name: currentChild.getComputedName(),
  151. title: currentChild.data.title,
  152. });
  153. },
  154. onTap(event) {
  155. const { index } = event.currentTarget.dataset;
  156. const child = this.children[index];
  157. if (child.data.disabled) {
  158. this.trigger('disabled', child);
  159. }
  160. else {
  161. this.setCurrentIndex(index);
  162. nextTick(() => {
  163. this.trigger('click');
  164. });
  165. }
  166. },
  167. // correct the index of active tab
  168. setCurrentIndexByName(name) {
  169. const { children = [] } = this;
  170. const matched = children.filter((child) => child.getComputedName() === name);
  171. if (matched.length) {
  172. this.setCurrentIndex(matched[0].index);
  173. }
  174. },
  175. setCurrentIndex(currentIndex) {
  176. const { data, children = [] } = this;
  177. if (!isDef(currentIndex) ||
  178. currentIndex >= children.length ||
  179. currentIndex < 0) {
  180. return;
  181. }
  182. groupSetData(this, () => {
  183. children.forEach((item, index) => {
  184. const active = index === currentIndex;
  185. if (active !== item.data.active || !item.inited) {
  186. item.updateRender(active, this);
  187. }
  188. });
  189. });
  190. if (currentIndex === data.currentIndex) {
  191. return;
  192. }
  193. const shouldEmitChange = data.currentIndex !== null;
  194. this.setData({ currentIndex });
  195. requestAnimationFrame(() => {
  196. this.resize();
  197. this.scrollIntoView();
  198. });
  199. nextTick(() => {
  200. this.trigger('input');
  201. if (shouldEmitChange) {
  202. this.trigger('change');
  203. }
  204. });
  205. },
  206. getCurrentName() {
  207. const activeTab = this.children[this.data.currentIndex];
  208. if (activeTab) {
  209. return activeTab.getComputedName();
  210. }
  211. },
  212. resize() {
  213. if (this.data.type !== 'line') {
  214. return;
  215. }
  216. const { currentIndex, ellipsis, skipTransition } = this.data;
  217. Promise.all([
  218. getAllRect(this, '.van-tab'),
  219. getRect(this, '.van-tabs__line'),
  220. ]).then(([rects = [], lineRect]) => {
  221. const rect = rects[currentIndex];
  222. if (rect == null) {
  223. return;
  224. }
  225. let lineOffsetLeft = rects
  226. .slice(0, currentIndex)
  227. .reduce((prev, curr) => prev + curr.width, 0);
  228. lineOffsetLeft +=
  229. (rect.width - lineRect.width) / 2 + (ellipsis ? 0 : 8);
  230. this.setData({ lineOffsetLeft });
  231. if (skipTransition) {
  232. nextTick(() => {
  233. this.setData({ skipTransition: false });
  234. });
  235. }
  236. });
  237. },
  238. // scroll active tab into view
  239. scrollIntoView() {
  240. const { currentIndex, scrollable, scrollWithAnimation } = this.data;
  241. if (!scrollable) {
  242. return;
  243. }
  244. Promise.all([
  245. getAllRect(this, '.van-tab'),
  246. getRect(this, '.van-tabs__nav'),
  247. ]).then(([tabRects, navRect]) => {
  248. const tabRect = tabRects[currentIndex];
  249. const offsetLeft = tabRects
  250. .slice(0, currentIndex)
  251. .reduce((prev, curr) => prev + curr.width, 0);
  252. this.setData({
  253. scrollLeft: offsetLeft - (navRect.width - tabRect.width) / 2,
  254. });
  255. if (!scrollWithAnimation) {
  256. nextTick(() => {
  257. this.setData({ scrollWithAnimation: true });
  258. });
  259. }
  260. });
  261. },
  262. onTouchScroll(event) {
  263. this.$emit('scroll', event.detail);
  264. },
  265. onTouchStart(event) {
  266. if (!this.data.swipeable)
  267. return;
  268. this.touchStart(event);
  269. },
  270. onTouchMove(event) {
  271. if (!this.data.swipeable)
  272. return;
  273. this.touchMove(event);
  274. },
  275. // watch swipe touch end
  276. onTouchEnd() {
  277. if (!this.data.swipeable)
  278. return;
  279. const { direction, deltaX, offsetX } = this;
  280. const minSwipeDistance = 50;
  281. if (direction === 'horizontal' && offsetX >= minSwipeDistance) {
  282. const index = this.getAvaiableTab(deltaX);
  283. if (index !== -1) {
  284. this.setCurrentIndex(index);
  285. }
  286. }
  287. },
  288. getAvaiableTab(direction) {
  289. const { tabs, currentIndex } = this.data;
  290. const step = direction > 0 ? -1 : 1;
  291. for (let i = step; currentIndex + i < tabs.length && currentIndex + i >= 0; i += step) {
  292. const index = currentIndex + i;
  293. if (index >= 0 &&
  294. index < tabs.length &&
  295. tabs[index] &&
  296. !tabs[index].disabled) {
  297. return index;
  298. }
  299. }
  300. return -1;
  301. },
  302. },
  303. });
  304. export default global['__wxComponents']['vant/dist/tabs/index']
  305. </script>
  306. <style platform="mp-weixin">
  307. @import '../common/index.css';.van-tabs{position:relative;-webkit-tap-highlight-color:transparent}.van-tabs__wrap{display:flex;overflow:hidden}.van-tabs__wrap--scrollable .van-tab{flex:0 0 22%}.van-tabs__wrap--scrollable .van-tab--complete{flex:1 0 auto!important;padding:0 12px}.van-tabs__wrap--scrollable .van-tabs__nav--complete{padding-right:8px;padding-left:8px}.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__scroll::-webkit-scrollbar{display:none}.van-tabs__nav{position:relative;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) - var(--border-width-base, 1px)*2);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:flex;transition-property:left}.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;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__title__info{position:relative!important;top:-1px!important;display:inline-block;transform:translateX(0)!important}
  308. </style>