index.vue 15 KB

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