u-tabs.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. "use strict";
  2. const common_vendor = require("../../../../common/vendor.js");
  3. const uni_modules_uviewPlus_components_uTabs_props = require("./props.js");
  4. const uni_modules_uviewPlus_libs_mixin_mpMixin = require("../../libs/mixin/mpMixin.js");
  5. const uni_modules_uviewPlus_libs_mixin_mixin = require("../../libs/mixin/mixin.js");
  6. const uni_modules_uviewPlus_libs_config_props = require("../../libs/config/props.js");
  7. const uni_modules_uviewPlus_libs_function_index = require("../../libs/function/index.js");
  8. const _sfc_main = {
  9. name: "u-tabs",
  10. mixins: [uni_modules_uviewPlus_libs_mixin_mpMixin.mpMixin, uni_modules_uviewPlus_libs_mixin_mixin.mixin, uni_modules_uviewPlus_components_uTabs_props.props],
  11. data() {
  12. return {
  13. firstTime: true,
  14. scrollLeft: 0,
  15. scrollViewWidth: 0,
  16. lineOffsetLeft: 0,
  17. tabsRect: {
  18. left: 0
  19. },
  20. innerCurrent: 0,
  21. moving: false
  22. };
  23. },
  24. watch: {
  25. current: {
  26. immediate: true,
  27. handler(newValue, oldValue) {
  28. if (newValue !== this.innerCurrent) {
  29. this.innerCurrent = newValue;
  30. this.$nextTick(() => {
  31. this.resize();
  32. });
  33. }
  34. }
  35. },
  36. // list变化时,重新渲染list各项信息
  37. list() {
  38. this.$nextTick(() => {
  39. this.resize();
  40. });
  41. }
  42. },
  43. computed: {
  44. textStyle() {
  45. return (index) => {
  46. const style = {};
  47. const customeStyle = index === this.innerCurrent ? uni_modules_uviewPlus_libs_function_index.addStyle(this.activeStyle) : common_vendor.index.$u.addStyle(
  48. this.inactiveStyle
  49. );
  50. if (this.list[index].disabled) {
  51. style.color = "#c8c9cc";
  52. }
  53. return uni_modules_uviewPlus_libs_function_index.deepMerge(customeStyle, style);
  54. };
  55. },
  56. propsBadge() {
  57. return uni_modules_uviewPlus_libs_config_props.defProps.badge;
  58. }
  59. },
  60. async mounted() {
  61. this.init();
  62. },
  63. emits: ["click", "change"],
  64. methods: {
  65. addStyle: uni_modules_uviewPlus_libs_function_index.addStyle,
  66. addUnit: uni_modules_uviewPlus_libs_function_index.addUnit,
  67. setLineLeft() {
  68. const tabItem = this.list[this.innerCurrent];
  69. if (!tabItem) {
  70. return;
  71. }
  72. let lineOffsetLeft = this.list.slice(0, this.innerCurrent).reduce((total, curr) => total + curr.rect.width, 0);
  73. const lineWidth = uni_modules_uviewPlus_libs_function_index.getPx(this.lineWidth);
  74. this.lineOffsetLeft = lineOffsetLeft + (tabItem.rect.width - lineWidth) / 2;
  75. if (this.firstTime) {
  76. setTimeout(() => {
  77. this.firstTime = false;
  78. }, 10);
  79. }
  80. },
  81. // nvue下设置滑块的位置
  82. animation(x, duration = 0) {
  83. },
  84. // 点击某一个标签
  85. clickHandler(item, index) {
  86. this.$emit("click", {
  87. ...item,
  88. index
  89. });
  90. if (item.disabled)
  91. return;
  92. this.innerCurrent = index;
  93. this.resize();
  94. this.$emit("change", {
  95. ...item,
  96. index
  97. });
  98. },
  99. init() {
  100. uni_modules_uviewPlus_libs_function_index.sleep().then(() => {
  101. this.resize();
  102. });
  103. },
  104. setScrollLeft() {
  105. const tabRect = this.list[this.innerCurrent];
  106. const offsetLeft = this.list.slice(0, this.innerCurrent).reduce((total, curr) => {
  107. return total + curr.rect.width;
  108. }, 0);
  109. const windowWidth = uni_modules_uviewPlus_libs_function_index.sys().windowWidth;
  110. let scrollLeft = offsetLeft - (this.tabsRect.width - tabRect.rect.width) / 2 - (windowWidth - this.tabsRect.right) / 2 + this.tabsRect.left / 2;
  111. scrollLeft = Math.min(scrollLeft, this.scrollViewWidth - this.tabsRect.width);
  112. this.scrollLeft = Math.max(0, scrollLeft);
  113. },
  114. // 获取所有标签的尺寸
  115. resize() {
  116. if (this.list.length === 0) {
  117. return;
  118. }
  119. Promise.all([this.getTabsRect(), this.getAllItemRect()]).then(([tabsRect, itemRect = []]) => {
  120. this.tabsRect = tabsRect;
  121. this.scrollViewWidth = 0;
  122. itemRect.map((item, index) => {
  123. this.scrollViewWidth += item.width;
  124. this.list[index].rect = item;
  125. });
  126. this.setLineLeft();
  127. this.setScrollLeft();
  128. });
  129. },
  130. // 获取导航菜单的尺寸
  131. getTabsRect() {
  132. return new Promise((resolve) => {
  133. this.queryRect("u-tabs__wrapper__scroll-view").then((size) => resolve(size));
  134. });
  135. },
  136. // 获取所有标签的尺寸
  137. getAllItemRect() {
  138. return new Promise((resolve) => {
  139. const promiseAllArr = this.list.map((item, index) => this.queryRect(
  140. `u-tabs__wrapper__nav__item-${index}`,
  141. true
  142. ));
  143. Promise.all(promiseAllArr).then((sizes) => resolve(sizes));
  144. });
  145. },
  146. // 获取各个标签的尺寸
  147. queryRect(el, item) {
  148. return new Promise((resolve) => {
  149. this.$uGetRect(`.${el}`).then((size) => {
  150. resolve(size);
  151. });
  152. });
  153. }
  154. }
  155. };
  156. if (!Array) {
  157. const _easycom_u_badge2 = common_vendor.resolveComponent("u-badge");
  158. _easycom_u_badge2();
  159. }
  160. const _easycom_u_badge = () => "../u-badge/u-badge.js";
  161. if (!Math) {
  162. _easycom_u_badge();
  163. }
  164. function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
  165. return {
  166. a: common_vendor.f(_ctx.list, (item, index, i0) => {
  167. return {
  168. a: common_vendor.t(item[_ctx.keyName]),
  169. b: common_vendor.n(item.disabled && "u-tabs__wrapper__nav__item__text--disabled"),
  170. c: common_vendor.s($options.textStyle(index)),
  171. d: "02b0c54f-0-" + i0,
  172. e: common_vendor.p({
  173. show: !!(item.badge && (item.badge.show || item.badge.isDot || item.badge.value)),
  174. isDot: item.badge && item.badge.isDot || $options.propsBadge.isDot,
  175. value: item.badge && item.badge.value || $options.propsBadge.value,
  176. max: item.badge && item.badge.max || $options.propsBadge.max,
  177. type: item.badge && item.badge.type || $options.propsBadge.type,
  178. showZero: item.badge && item.badge.showZero || $options.propsBadge.showZero,
  179. bgColor: item.badge && item.badge.bgColor || $options.propsBadge.bgColor,
  180. color: item.badge && item.badge.color || $options.propsBadge.color,
  181. shape: item.badge && item.badge.shape || $options.propsBadge.shape,
  182. numberType: item.badge && item.badge.numberType || $options.propsBadge.numberType,
  183. inverted: item.badge && item.badge.inverted || $options.propsBadge.inverted,
  184. customStyle: "margin-left: 4px;"
  185. }),
  186. f: index,
  187. g: common_vendor.o(($event) => $options.clickHandler(item, index), index),
  188. h: `u-tabs__wrapper__nav__item-${index}`,
  189. i: common_vendor.n(`u-tabs__wrapper__nav__item-${index}`),
  190. j: common_vendor.n(item.disabled && "u-tabs__wrapper__nav__item--disabled")
  191. };
  192. }),
  193. b: common_vendor.s($options.addStyle(_ctx.itemStyle)),
  194. c: common_vendor.s({
  195. flex: _ctx.scrollable ? "" : 1
  196. }),
  197. d: common_vendor.s({
  198. width: $options.addUnit(_ctx.lineWidth),
  199. transform: `translate(${$data.lineOffsetLeft}px)`,
  200. transitionDuration: `${$data.firstTime ? 0 : _ctx.duration}ms`,
  201. height: $options.addUnit(_ctx.lineHeight),
  202. background: _ctx.lineColor,
  203. backgroundSize: _ctx.lineBgSize
  204. }),
  205. e: _ctx.scrollable,
  206. f: $data.scrollLeft
  207. };
  208. }
  209. const Component = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["render", _sfc_render], ["__scopeId", "data-v-02b0c54f"]]);
  210. wx.createComponent(Component);