index.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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="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/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 (name !== this.getCurrentName()) {
  71. this.setCurrentIndexByName(name);
  72. }
  73. },
  74. },
  75. type: {
  76. type: String,
  77. value: 'line',
  78. },
  79. ellipsis: {
  80. type: Boolean,
  81. value: true,
  82. },
  83. duration: {
  84. type: Number,
  85. value: 0.3,
  86. },
  87. zIndex: {
  88. type: Number,
  89. value: 1,
  90. },
  91. swipeThreshold: {
  92. type: Number,
  93. value: 5,
  94. observer(value) {
  95. this.setData({
  96. scrollable: this.children.length > value || !this.data.ellipsis,
  97. });
  98. },
  99. },
  100. offsetTop: {
  101. type: Number,
  102. value: 0,
  103. },
  104. lazyRender: {
  105. type: Boolean,
  106. value: true,
  107. },
  108. },
  109. data: {
  110. tabs: [],
  111. scrollLeft: 0,
  112. scrollable: false,
  113. currentIndex: 0,
  114. container: null,
  115. skipTransition: true,
  116. scrollWithAnimation: false,
  117. lineOffsetLeft: 0,
  118. },
  119. mounted() {
  120. requestAnimationFrame(() => {
  121. this.swiping = true;
  122. this.setData({
  123. container: () => this.createSelectorQuery().select('.van-tabs'),
  124. });
  125. this.resize();
  126. this.scrollIntoView();
  127. });
  128. },
  129. methods: {
  130. updateTabs() {
  131. const { children = [], data } = this;
  132. this.setData({
  133. tabs: children.map((child) => child.data),
  134. scrollable: this.children.length > data.swipeThreshold || !data.ellipsis,
  135. });
  136. this.setCurrentIndexByName(data.active || this.getCurrentName());
  137. },
  138. trigger(eventName, child) {
  139. const { currentIndex } = this.data;
  140. const currentChild = child || this.children[currentIndex];
  141. if (!isDef(currentChild)) {
  142. return;
  143. }
  144. this.$emit(eventName, {
  145. index: currentChild.index,
  146. name: currentChild.getComputedName(),
  147. title: currentChild.data.title,
  148. });
  149. },
  150. onTap(event) {
  151. const { index } = event.currentTarget.dataset;
  152. const child = this.children[index];
  153. if (child.data.disabled) {
  154. this.trigger('disabled', child);
  155. }
  156. else {
  157. this.setCurrentIndex(index);
  158. nextTick(() => {
  159. this.trigger('click');
  160. });
  161. }
  162. },
  163. // correct the index of active tab
  164. setCurrentIndexByName(name) {
  165. const { children = [] } = this;
  166. const matched = children.filter((child) => child.getComputedName() === name);
  167. if (matched.length) {
  168. this.setCurrentIndex(matched[0].index);
  169. }
  170. },
  171. setCurrentIndex(currentIndex) {
  172. const { data, children = [] } = this;
  173. if (!isDef(currentIndex) ||
  174. currentIndex >= children.length ||
  175. currentIndex < 0) {
  176. return;
  177. }
  178. groupSetData(this, () => {
  179. children.forEach((item, index) => {
  180. const active = index === currentIndex;
  181. if (active !== item.data.active || !item.inited) {
  182. item.updateRender(active, this);
  183. }
  184. });
  185. });
  186. if (currentIndex === data.currentIndex) {
  187. return;
  188. }
  189. const shouldEmitChange = data.currentIndex !== null;
  190. this.setData({ currentIndex });
  191. requestAnimationFrame(() => {
  192. this.resize();
  193. this.scrollIntoView();
  194. });
  195. nextTick(() => {
  196. this.trigger('input');
  197. if (shouldEmitChange) {
  198. this.trigger('change');
  199. }
  200. });
  201. },
  202. getCurrentName() {
  203. const activeTab = this.children[this.data.currentIndex];
  204. if (activeTab) {
  205. return activeTab.getComputedName();
  206. }
  207. },
  208. resize() {
  209. if (this.data.type !== 'line') {
  210. return;
  211. }
  212. const { currentIndex, ellipsis, skipTransition } = this.data;
  213. Promise.all([
  214. getAllRect(this, '.van-tab'),
  215. getRect(this, '.van-tabs__line'),
  216. ]).then(([rects = [], lineRect]) => {
  217. const rect = rects[currentIndex];
  218. if (rect == null) {
  219. return;
  220. }
  221. let lineOffsetLeft = rects
  222. .slice(0, currentIndex)
  223. .reduce((prev, curr) => prev + curr.width, 0);
  224. lineOffsetLeft +=
  225. (rect.width - lineRect.width) / 2 + (ellipsis ? 0 : 8);
  226. this.setData({ lineOffsetLeft });
  227. this.swiping = true;
  228. if (skipTransition) {
  229. nextTick(() => {
  230. this.setData({ skipTransition: false });
  231. });
  232. }
  233. });
  234. },
  235. // scroll active tab into view
  236. scrollIntoView() {
  237. const { currentIndex, scrollable, scrollWithAnimation } = this.data;
  238. if (!scrollable) {
  239. return;
  240. }
  241. Promise.all([
  242. getAllRect(this, '.van-tab'),
  243. getRect(this, '.van-tabs__nav'),
  244. ]).then(([tabRects, navRect]) => {
  245. const tabRect = tabRects[currentIndex];
  246. const offsetLeft = tabRects
  247. .slice(0, currentIndex)
  248. .reduce((prev, curr) => prev + curr.width, 0);
  249. this.setData({
  250. scrollLeft: offsetLeft - (navRect.width - tabRect.width) / 2,
  251. });
  252. if (!scrollWithAnimation) {
  253. nextTick(() => {
  254. this.setData({ scrollWithAnimation: true });
  255. });
  256. }
  257. });
  258. },
  259. onTouchScroll(event) {
  260. this.$emit('scroll', event.detail);
  261. },
  262. onTouchStart(event) {
  263. if (!this.data.swipeable)
  264. return;
  265. this.touchStart(event);
  266. },
  267. onTouchMove(event) {
  268. if (!this.data.swipeable || !this.swiping)
  269. return;
  270. this.touchMove(event);
  271. },
  272. // watch swipe touch end
  273. onTouchEnd() {
  274. if (!this.data.swipeable || !this.swiping)
  275. return;
  276. const { direction, deltaX, offsetX } = this;
  277. const minSwipeDistance = 50;
  278. if (direction === 'horizontal' && offsetX >= minSwipeDistance) {
  279. const index = this.getAvaiableTab(deltaX);
  280. if (index !== -1) {
  281. this.setCurrentIndex(index);
  282. }
  283. }
  284. this.swiping = false;
  285. },
  286. getAvaiableTab(direction) {
  287. const { tabs, currentIndex } = this.data;
  288. const step = direction > 0 ? -1 : 1;
  289. for (let i = step; currentIndex + i < tabs.length && currentIndex + i >= 0; i += step) {
  290. const index = currentIndex + i;
  291. if (index >= 0 &&
  292. index < tabs.length &&
  293. tabs[index] &&
  294. !tabs[index].disabled) {
  295. return index;
  296. }
  297. }
  298. return -1;
  299. },
  300. },
  301. });
  302. export default global['__wxComponents']['vant/tabs/index']
  303. </script>
  304. <style platform="mp-weixin">
  305. @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}.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)}.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;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}.van-tabs--line .van-tabs__wrap{height:var(--tabs-line-height,44px)}.van-tabs--card .van-tabs__wrap{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);font-weight:var(--font-weight-bold,500)}.van-tab--disabled{color:var(--tab-disabled-text-color,#c8c9cc)}.van-tab__title__info{display:inline-block;position:relative!important;top:-1px!important;transform:translateX(0)!important}
  306. </style>