index.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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, { complete: !ellipsis }]))+' nav-class'" :style="getters.tabCardTypeBorderStyle(color, type)">
  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="getters.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="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. current: 'tabs',
  43. linked(target) {
  44. target.index = this.children.length - 1;
  45. this.updateTabs();
  46. },
  47. unlinked() {
  48. this.children = this.children.map((child, index) => {
  49. child.index = index;
  50. return child;
  51. });
  52. this.updateTabs();
  53. },
  54. },
  55. props: {
  56. color: {
  57. type: String,
  58. observer: 'setLine',
  59. },
  60. sticky: Boolean,
  61. animated: {
  62. type: Boolean,
  63. observer() {
  64. this.children.forEach((child, index) =>
  65. child.updateRender(index === this.data.currentIndex, this)
  66. );
  67. },
  68. },
  69. swipeable: Boolean,
  70. lineWidth: {
  71. type: [String, Number],
  72. value: -1,
  73. observer: 'setLine',
  74. },
  75. lineHeight: {
  76. type: [String, Number],
  77. value: -1,
  78. observer: 'setLine',
  79. },
  80. titleActiveColor: String,
  81. titleInactiveColor: String,
  82. active: {
  83. type: [String, Number],
  84. value: 0,
  85. observer(name) {
  86. if (name !== this.getCurrentName()) {
  87. this.setCurrentIndexByName(name);
  88. }
  89. },
  90. },
  91. type: {
  92. type: String,
  93. value: 'line',
  94. },
  95. border: {
  96. type: Boolean,
  97. value: true,
  98. },
  99. ellipsis: {
  100. type: Boolean,
  101. value: true,
  102. },
  103. duration: {
  104. type: Number,
  105. value: 0.3,
  106. },
  107. zIndex: {
  108. type: Number,
  109. value: 1,
  110. },
  111. swipeThreshold: {
  112. type: Number,
  113. value: 5,
  114. observer(value) {
  115. this.setData({
  116. scrollable: this.children.length > value || !this.data.ellipsis,
  117. });
  118. },
  119. },
  120. offsetTop: {
  121. type: Number,
  122. value: 0,
  123. },
  124. lazyRender: {
  125. type: Boolean,
  126. value: true,
  127. },
  128. },
  129. data: {
  130. tabs: [],
  131. lineStyle: '',
  132. scrollLeft: 0,
  133. scrollable: false,
  134. trackStyle: '',
  135. currentIndex: null,
  136. container: null,
  137. },
  138. mounted() {
  139. wx.nextTick(() => {
  140. this.setLine(true);
  141. this.scrollIntoView();
  142. });
  143. },
  144. methods: {
  145. updateContainer() {
  146. this.setData({
  147. container: () => uni.createSelectorQuery().select('.van-tabs'),
  148. });
  149. },
  150. updateTabs() {
  151. const { children = [], data } = this;
  152. this.setData({
  153. tabs: children.map((child) => child.data),
  154. scrollable:
  155. this.children.length > data.swipeThreshold || !data.ellipsis,
  156. });
  157. this.setCurrentIndexByName(this.getCurrentName() || data.active);
  158. },
  159. trigger(eventName, child) {
  160. const { currentIndex } = this.data;
  161. const currentChild = child || this.children[currentIndex];
  162. if (!isDef(currentChild)) {
  163. return;
  164. }
  165. this.$emit(eventName, {
  166. index: currentChild.index,
  167. name: currentChild.getComputedName(),
  168. title: currentChild.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', child);
  176. } else {
  177. this.setCurrentIndex(index*1);
  178. wx.nextTick(() => {
  179. this.trigger('click');
  180. });
  181. }
  182. },
  183. // correct the index of active tab
  184. setCurrentIndexByName(name) {
  185. const { children = [] } = this;
  186. const matched = children.filter(
  187. (child) => child.getComputedName() === name
  188. );
  189. if (matched.length) {
  190. this.setCurrentIndex(matched[0].index);
  191. }
  192. },
  193. setCurrentIndex(currentIndex) {
  194. const { data, children = [] } = this;
  195. if (
  196. !isDef(currentIndex) ||
  197. currentIndex >= children.length ||
  198. currentIndex < 0
  199. ) {
  200. return;
  201. }
  202. children.forEach((item, index) => {
  203. const active = index == currentIndex;
  204. if (active != item.data.active || !item.inited) {
  205. item.updateRender(active, this);
  206. }
  207. });
  208. if (currentIndex == data.currentIndex) {
  209. return;
  210. }
  211. const shouldEmitChange = data.currentIndex !== null;
  212. this.setData({ currentIndex });
  213. wx.nextTick(() => {
  214. this.setLine();
  215. this.scrollIntoView();
  216. this.updateContainer();
  217. this.trigger('input');
  218. if (shouldEmitChange) {
  219. this.trigger('change');
  220. }
  221. });
  222. },
  223. getCurrentName() {
  224. const activeTab = this.children[this.data.currentIndex];
  225. if (activeTab) {
  226. return activeTab.getComputedName();
  227. }
  228. },
  229. setLine(skipTransition) {
  230. if (this.data.type !== 'line') {
  231. return;
  232. }
  233. const {
  234. color,
  235. duration,
  236. currentIndex,
  237. lineWidth,
  238. lineHeight,
  239. } = this.data;
  240. this.getRect('.van-tab', true).then((rects = []) => {
  241. const rect = rects[currentIndex];
  242. if (rect == null) {
  243. return;
  244. }
  245. const width = lineWidth !== -1 ? lineWidth : rect.width / 2;
  246. const height =
  247. lineHeight !== -1
  248. ? `height: ${addUnit(lineHeight)}; border-radius: ${addUnit(
  249. lineHeight
  250. )};`
  251. : '';
  252. let left = rects
  253. .slice(0, currentIndex)
  254. .reduce((prev, curr) => prev + curr.width, 0);
  255. left += (rect.width - width) / 2;
  256. const transition = skipTransition
  257. ? ''
  258. : `transition-duration: ${duration}s; -webkit-transition-duration: ${duration}s;`;
  259. this.setData({
  260. lineStyle: `
  261. ${height}
  262. width: ${addUnit(width)};
  263. background-color: ${color};
  264. -webkit-transform: translateX(${left}px);
  265. transform: translateX(${left}px);
  266. ${transition}
  267. `,
  268. });
  269. });
  270. },
  271. // scroll active tab into view
  272. scrollIntoView() {
  273. const { currentIndex, scrollable } = this.data;
  274. if (!scrollable) {
  275. return;
  276. }
  277. Promise.all([
  278. this.getRect('.van-tab', true),
  279. this.getRect('.van-tabs__nav'),
  280. ]).then(([tabRects, navRect]) => {
  281. const tabRect = tabRects[currentIndex];
  282. const offsetLeft = tabRects
  283. .slice(0, currentIndex)
  284. .reduce((prev, curr) => prev + curr.width, 0);
  285. this.setData({
  286. scrollLeft: offsetLeft - (navRect.width - tabRect.width) / 2,
  287. });
  288. });
  289. },
  290. onTouchScroll(event) {
  291. this.$emit('scroll', event.detail);
  292. },
  293. onTouchStart(event) {
  294. if (!this.data.swipeable) return;
  295. this.touchStart(event);
  296. },
  297. onTouchMove(event) {
  298. if (!this.data.swipeable) return;
  299. this.touchMove(event);
  300. },
  301. // watch swipe touch end
  302. onTouchEnd() {
  303. if (!this.data.swipeable) return;
  304. const { direction, deltaX, offsetX } = this;
  305. const minSwipeDistance = 50;
  306. if (direction === 'horizontal' && offsetX >= minSwipeDistance) {
  307. const index = this.getAvaiableTab(deltaX);
  308. if (index !== -1) {
  309. this.setCurrentIndex(index);
  310. }
  311. }
  312. },
  313. getAvaiableTab(direction) {
  314. const { tabs, currentIndex } = this.data;
  315. const step = direction > 0 ? -1 : 1;
  316. for (
  317. let i = step;
  318. currentIndex + i < tabs.length && currentIndex + i >= 0;
  319. i += step
  320. ) {
  321. const index = currentIndex + i;
  322. if (
  323. index >= 0 &&
  324. index < tabs.length &&
  325. tabs[index] &&
  326. !tabs[index].disabled
  327. ) {
  328. return index;
  329. }
  330. }
  331. return -1;
  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{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__wrap--scrollable .van-tab--complete{-webkit-flex:1 0 auto!important;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:-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__title__info{position:relative!important;top:-1px!important;display:inline-block;-webkit-transform:translateX(0)!important;transform:translateX(0)!important}
  339. </style>