index.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. <template>
  2. <uni-shadow-root class="weapp-lib-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 :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'] = 'weapp/lib/tabs/index'
  33. "use strict";
  34. var __assign = (this && this.__assign) || function () {
  35. __assign = Object.assign || function(t) {
  36. for (var s, i = 1, n = arguments.length; i < n; i++) {
  37. s = arguments[i];
  38. for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
  39. t[p] = s[p];
  40. }
  41. return t;
  42. };
  43. return __assign.apply(this, arguments);
  44. };
  45. Object.defineProperty(exports, "__esModule", { value: true });
  46. var component_1 = require("../common/component");
  47. var touch_1 = require("../mixins/touch");
  48. var utils_1 = require("../common/utils");
  49. var validator_1 = require("../common/validator");
  50. var relation_1 = require("../common/relation");
  51. (0, component_1.VantComponent)({
  52. mixins: [touch_1.touch],
  53. classes: [
  54. 'nav-class',
  55. 'tab-class',
  56. 'tab-active-class',
  57. 'line-class',
  58. 'wrap-class',
  59. ],
  60. relation: (0, relation_1.useChildren)('tab', function () {
  61. this.updateTabs();
  62. }),
  63. props: {
  64. sticky: Boolean,
  65. border: Boolean,
  66. swipeable: Boolean,
  67. titleActiveColor: String,
  68. titleInactiveColor: String,
  69. color: String,
  70. animated: {
  71. type: Boolean,
  72. observer: function () {
  73. var _this = this;
  74. this.children.forEach(function (child, index) {
  75. return child.updateRender(index === _this.data.currentIndex, _this);
  76. });
  77. },
  78. },
  79. lineWidth: {
  80. type: null,
  81. value: 40,
  82. observer: 'resize',
  83. },
  84. lineHeight: {
  85. type: null,
  86. value: -1,
  87. },
  88. active: {
  89. type: null,
  90. value: 0,
  91. observer: function (name) {
  92. if (name !== this.getCurrentName()) {
  93. this.setCurrentIndexByName(name);
  94. }
  95. },
  96. },
  97. type: {
  98. type: String,
  99. value: 'line',
  100. },
  101. ellipsis: {
  102. type: Boolean,
  103. value: true,
  104. },
  105. duration: {
  106. type: Number,
  107. value: 0.3,
  108. },
  109. zIndex: {
  110. type: Number,
  111. value: 1,
  112. },
  113. swipeThreshold: {
  114. type: Number,
  115. value: 5,
  116. observer: function (value) {
  117. this.setData({
  118. scrollable: this.children.length > value || !this.data.ellipsis,
  119. });
  120. },
  121. },
  122. offsetTop: {
  123. type: Number,
  124. value: 0,
  125. },
  126. lazyRender: {
  127. type: Boolean,
  128. value: true,
  129. },
  130. useBeforeChange: {
  131. type: Boolean,
  132. value: false,
  133. },
  134. },
  135. data: {
  136. tabs: [],
  137. scrollLeft: 0,
  138. scrollable: false,
  139. currentIndex: 0,
  140. container: null,
  141. skipTransition: true,
  142. scrollWithAnimation: false,
  143. lineOffsetLeft: 0,
  144. inited: false,
  145. },
  146. mounted: function () {
  147. var _this = this;
  148. (0, utils_1.requestAnimationFrame)(function () {
  149. _this.swiping = true;
  150. _this.setData({
  151. container: function () { return _this.createSelectorQuery().select('.van-tabs'); },
  152. });
  153. _this.resize();
  154. _this.scrollIntoView();
  155. });
  156. },
  157. methods: {
  158. updateTabs: function () {
  159. var _a = this, _b = _a.children, children = _b === void 0 ? [] : _b, data = _a.data;
  160. this.setData({
  161. tabs: children.map(function (child) { return child.data; }),
  162. scrollable: this.children.length > data.swipeThreshold || !data.ellipsis,
  163. });
  164. this.setCurrentIndexByName(data.active || this.getCurrentName());
  165. },
  166. trigger: function (eventName, child) {
  167. var currentIndex = this.data.currentIndex;
  168. var data = this.getChildData(currentIndex, child);
  169. if (!(0, validator_1.isDef)(data)) {
  170. return;
  171. }
  172. this.$emit(eventName, data);
  173. },
  174. onTap: function (event) {
  175. var _this = this;
  176. var index = event.currentTarget.dataset.index;
  177. var child = this.children[index];
  178. if (child.data.disabled) {
  179. this.trigger('disabled', child);
  180. return;
  181. }
  182. this.onBeforeChange(index).then(function () {
  183. _this.setCurrentIndex(index);
  184. (0, utils_1.nextTick)(function () {
  185. _this.trigger('click');
  186. });
  187. });
  188. },
  189. // correct the index of active tab
  190. setCurrentIndexByName: function (name) {
  191. var _a = this.children, children = _a === void 0 ? [] : _a;
  192. var matched = children.filter(function (child) { return child.getComputedName() === name; });
  193. if (matched.length) {
  194. this.setCurrentIndex(matched[0].index);
  195. }
  196. },
  197. setCurrentIndex: function (currentIndex) {
  198. var _this = this;
  199. var _a = this, data = _a.data, _b = _a.children, children = _b === void 0 ? [] : _b;
  200. if (!(0, validator_1.isDef)(currentIndex) ||
  201. currentIndex >= children.length ||
  202. currentIndex < 0) {
  203. return;
  204. }
  205. (0, utils_1.groupSetData)(this, function () {
  206. children.forEach(function (item, index) {
  207. var active = index === currentIndex;
  208. if (active !== item.data.active || !item.inited) {
  209. item.updateRender(active, _this);
  210. }
  211. });
  212. });
  213. if (currentIndex === data.currentIndex) {
  214. return;
  215. }
  216. var shouldEmitChange = data.currentIndex !== null;
  217. this.setData({ currentIndex: currentIndex });
  218. (0, utils_1.requestAnimationFrame)(function () {
  219. _this.resize();
  220. _this.scrollIntoView();
  221. });
  222. (0, utils_1.nextTick)(function () {
  223. _this.trigger('input');
  224. if (shouldEmitChange) {
  225. _this.trigger('change');
  226. }
  227. });
  228. },
  229. getCurrentName: function () {
  230. var activeTab = this.children[this.data.currentIndex];
  231. if (activeTab) {
  232. return activeTab.getComputedName();
  233. }
  234. },
  235. resize: function () {
  236. var _this = this;
  237. if (this.data.type !== 'line') {
  238. return;
  239. }
  240. var _a = this.data, currentIndex = _a.currentIndex, ellipsis = _a.ellipsis, skipTransition = _a.skipTransition;
  241. Promise.all([
  242. (0, utils_1.getAllRect)(this, '.van-tab'),
  243. (0, utils_1.getRect)(this, '.van-tabs__line'),
  244. ]).then(function (_a) {
  245. var _b = _a[0], rects = _b === void 0 ? [] : _b, lineRect = _a[1];
  246. var rect = rects[currentIndex];
  247. if (rect == null) {
  248. return;
  249. }
  250. var lineOffsetLeft = rects
  251. .slice(0, currentIndex)
  252. .reduce(function (prev, curr) { return prev + curr.width; }, 0);
  253. lineOffsetLeft +=
  254. (rect.width - lineRect.width) / 2 + (ellipsis ? 0 : 8);
  255. _this.setData({ lineOffsetLeft: lineOffsetLeft, inited: true });
  256. _this.swiping = true;
  257. if (skipTransition) {
  258. // waiting transition end
  259. setTimeout(function () {
  260. _this.setData({ skipTransition: false });
  261. }, _this.data.duration);
  262. }
  263. });
  264. },
  265. // scroll active tab into view
  266. scrollIntoView: function () {
  267. var _this = this;
  268. var _a = this.data, currentIndex = _a.currentIndex, scrollable = _a.scrollable, scrollWithAnimation = _a.scrollWithAnimation;
  269. if (!scrollable) {
  270. return;
  271. }
  272. Promise.all([
  273. (0, utils_1.getAllRect)(this, '.van-tab'),
  274. (0, utils_1.getRect)(this, '.van-tabs__nav'),
  275. ]).then(function (_a) {
  276. var tabRects = _a[0], navRect = _a[1];
  277. var tabRect = tabRects[currentIndex];
  278. var offsetLeft = tabRects
  279. .slice(0, currentIndex)
  280. .reduce(function (prev, curr) { return prev + curr.width; }, 0);
  281. _this.setData({
  282. scrollLeft: offsetLeft - (navRect.width - tabRect.width) / 2,
  283. });
  284. if (!scrollWithAnimation) {
  285. (0, utils_1.nextTick)(function () {
  286. _this.setData({ scrollWithAnimation: true });
  287. });
  288. }
  289. });
  290. },
  291. onTouchScroll: function (event) {
  292. this.$emit('scroll', event.detail);
  293. },
  294. onTouchStart: function (event) {
  295. if (!this.data.swipeable)
  296. return;
  297. this.swiping = true;
  298. this.touchStart(event);
  299. },
  300. onTouchMove: function (event) {
  301. if (!this.data.swipeable || !this.swiping)
  302. return;
  303. this.touchMove(event);
  304. },
  305. // watch swipe touch end
  306. onTouchEnd: function () {
  307. var _this = this;
  308. if (!this.data.swipeable || !this.swiping)
  309. return;
  310. var _a = this, direction = _a.direction, deltaX = _a.deltaX, offsetX = _a.offsetX;
  311. var minSwipeDistance = 50;
  312. if (direction === 'horizontal' && offsetX >= minSwipeDistance) {
  313. var index_1 = this.getAvaiableTab(deltaX);
  314. if (index_1 !== -1) {
  315. this.onBeforeChange(index_1).then(function () { return _this.setCurrentIndex(index_1); });
  316. }
  317. }
  318. this.swiping = false;
  319. },
  320. getAvaiableTab: function (direction) {
  321. var _a = this.data, tabs = _a.tabs, currentIndex = _a.currentIndex;
  322. var step = direction > 0 ? -1 : 1;
  323. for (var i = step; currentIndex + i < tabs.length && currentIndex + i >= 0; i += step) {
  324. var index = currentIndex + i;
  325. if (index >= 0 &&
  326. index < tabs.length &&
  327. tabs[index] &&
  328. !tabs[index].disabled) {
  329. return index;
  330. }
  331. }
  332. return -1;
  333. },
  334. onBeforeChange: function (index) {
  335. var _this = this;
  336. var useBeforeChange = this.data.useBeforeChange;
  337. if (!useBeforeChange) {
  338. return Promise.resolve();
  339. }
  340. return new Promise(function (resolve, reject) {
  341. _this.$emit('before-change', __assign(__assign({}, _this.getChildData(index)), { callback: function (status) { return (status ? resolve() : reject()); } }));
  342. });
  343. },
  344. getChildData: function (index, child) {
  345. var currentChild = child || this.children[index];
  346. if (!(0, validator_1.isDef)(currentChild)) {
  347. return;
  348. }
  349. return {
  350. index: currentChild.index,
  351. name: currentChild.getComputedName(),
  352. title: currentChild.data.title,
  353. };
  354. },
  355. },
  356. });
  357. export default global['__wxComponents']['weapp/lib/tabs/index']
  358. </script>
  359. <style platform="mp-weixin">
  360. @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);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}.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);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}
  361. </style>