su-tabs.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. "use strict";
  2. const common_vendor = require("../../../common/vendor.js");
  3. const sheep_helper_index = require("../../helper/index.js");
  4. require("../../helper/test.js");
  5. require("../../helper/digit.js");
  6. const _sfc_main = {
  7. name: "su-tabs",
  8. data() {
  9. return {
  10. addStyle: sheep_helper_index.addStyle,
  11. addUnit: sheep_helper_index.addUnit,
  12. firstTime: true,
  13. scrollLeft: 0,
  14. scrollViewWidth: 0,
  15. lineOffsetLeft: 0,
  16. tabsRect: {
  17. left: 0
  18. },
  19. innerCurrent: 0,
  20. moving: false
  21. };
  22. },
  23. props: {
  24. // 滑块的移动过渡时间,单位ms
  25. duration: {
  26. type: Number,
  27. default: 300
  28. },
  29. // tabs标签数组
  30. list: {
  31. type: Array,
  32. default: []
  33. },
  34. badge: {
  35. type: Boolean,
  36. default: false
  37. },
  38. // 滑块颜色
  39. lineColor: {
  40. type: String,
  41. default: ""
  42. },
  43. // 菜单选择中时的样式
  44. activeStyle: {
  45. type: [String, Object],
  46. default() {
  47. return {
  48. color: "#303133"
  49. };
  50. }
  51. },
  52. // 菜单非选中时的样式
  53. inactiveStyle: {
  54. type: [String, Object],
  55. default() {
  56. return {
  57. color: "#606266"
  58. };
  59. }
  60. },
  61. // 滑块长度
  62. lineWidth: {
  63. type: [String, Number],
  64. default: 20
  65. },
  66. // 滑块高度
  67. lineHeight: {
  68. type: [String, Number],
  69. default: 3
  70. },
  71. // 滑块背景显示大小,当滑块背景设置为图片时使用
  72. lineBgSize: {
  73. type: String,
  74. default: "cover"
  75. },
  76. // 菜单item的样式
  77. itemStyle: {
  78. type: [String, Object],
  79. default() {
  80. return {
  81. height: "44px"
  82. };
  83. }
  84. },
  85. // 菜单是否可滚动
  86. scrollable: {
  87. type: Boolean,
  88. default: true
  89. },
  90. // 当前选中标签的索引
  91. current: {
  92. type: [Number, String],
  93. default: 0
  94. },
  95. // 默认读取的键名
  96. keyName: {
  97. type: String,
  98. default: "name"
  99. }
  100. },
  101. watch: {
  102. current: {
  103. immediate: true,
  104. handler(newValue, oldValue) {
  105. if (newValue !== this.innerCurrent) {
  106. this.innerCurrent = newValue;
  107. this.$nextTick(() => {
  108. this.resize();
  109. });
  110. }
  111. }
  112. },
  113. // list变化时,重新渲染list各项信息
  114. list() {
  115. this.$nextTick(() => {
  116. this.resize();
  117. });
  118. }
  119. },
  120. computed: {
  121. textStyle() {
  122. return (index) => {
  123. const style = {};
  124. const customeStyle = index === this.innerCurrent ? sheep_helper_index.addStyle(this.activeStyle) : sheep_helper_index.addStyle(this.inactiveStyle);
  125. if (this.list[index].disabled) {
  126. style.color = "#c8c9cc";
  127. }
  128. return sheep_helper_index.deepMerge(customeStyle, style);
  129. };
  130. }
  131. },
  132. async mounted() {
  133. this.init();
  134. },
  135. methods: {
  136. $uGetRect(selector, all) {
  137. return new Promise((resolve) => {
  138. common_vendor.index.createSelectorQuery().in(this)[all ? "selectAll" : "select"](selector).boundingClientRect((rect) => {
  139. if (all && Array.isArray(rect) && rect.length) {
  140. resolve(rect);
  141. }
  142. if (!all && rect) {
  143. resolve(rect);
  144. }
  145. }).exec();
  146. });
  147. },
  148. setLineLeft() {
  149. const tabItem = this.list[this.innerCurrent];
  150. if (!tabItem) {
  151. return;
  152. }
  153. let lineOffsetLeft = this.list.slice(0, this.innerCurrent).reduce((total, curr) => total + curr.rect.width, 0);
  154. const lineWidth = sheep_helper_index.getPx(this.lineWidth);
  155. this.lineOffsetLeft = lineOffsetLeft + (tabItem.rect.width - lineWidth) / 2;
  156. if (this.firstTime) {
  157. setTimeout(() => {
  158. this.firstTime = false;
  159. }, 10);
  160. }
  161. },
  162. // nvue下设置滑块的位置
  163. animation(x, duration = 0) {
  164. },
  165. // 点击某一个标签
  166. clickHandler(item, index) {
  167. this.$emit("click", {
  168. ...item,
  169. index
  170. });
  171. if (item.disabled)
  172. return;
  173. this.innerCurrent = index;
  174. this.resize();
  175. this.$emit("change", {
  176. ...item,
  177. index
  178. });
  179. },
  180. init() {
  181. sheep_helper_index.sleep().then(() => {
  182. this.resize();
  183. });
  184. },
  185. setScrollLeft() {
  186. const tabRect = this.list[this.innerCurrent];
  187. const offsetLeft = this.list.slice(0, this.innerCurrent).reduce((total, curr) => {
  188. return total + curr.rect.width;
  189. }, 0);
  190. const windowWidth = sheep_helper_index.sys().windowWidth;
  191. let scrollLeft = offsetLeft - (this.tabsRect.width - tabRect.rect.width) / 2 - (windowWidth - this.tabsRect.right) / 2 + this.tabsRect.left / 2;
  192. scrollLeft = Math.min(scrollLeft, this.scrollViewWidth - this.tabsRect.width);
  193. this.scrollLeft = Math.max(0, scrollLeft);
  194. },
  195. // 获取所有标签的尺寸
  196. resize() {
  197. if (this.list.length === 0) {
  198. return;
  199. }
  200. Promise.all([this.getTabsRect(), this.getAllItemRect()]).then(
  201. ([tabsRect, itemRect = []]) => {
  202. this.tabsRect = tabsRect;
  203. this.scrollViewWidth = 0;
  204. itemRect.map((item, index) => {
  205. this.scrollViewWidth += item.width;
  206. this.list[index].rect = item;
  207. });
  208. this.setLineLeft();
  209. this.setScrollLeft();
  210. }
  211. );
  212. },
  213. // 获取导航菜单的尺寸
  214. getTabsRect() {
  215. return new Promise((resolve) => {
  216. this.queryRect("u-tabs__wrapper__scroll-view").then((size) => resolve(size));
  217. });
  218. },
  219. // 获取所有标签的尺寸
  220. getAllItemRect() {
  221. return new Promise((resolve) => {
  222. const promiseAllArr = this.list.map(
  223. (item, index) => this.queryRect(`u-tabs__wrapper__nav__item-${index}`, true)
  224. );
  225. Promise.all(promiseAllArr).then((sizes) => resolve(sizes));
  226. });
  227. },
  228. // 获取各个标签的尺寸
  229. queryRect(el, item) {
  230. return new Promise((resolve) => {
  231. this.$uGetRect(`.${el}`).then((size) => {
  232. resolve(size);
  233. });
  234. });
  235. }
  236. }
  237. };
  238. if (!Array) {
  239. const _easycom_uni_badge2 = common_vendor.resolveComponent("uni-badge");
  240. _easycom_uni_badge2();
  241. }
  242. const _easycom_uni_badge = () => "../../../uni_modules/uni-badge/components/uni-badge/uni-badge.js";
  243. if (!Math) {
  244. _easycom_uni_badge();
  245. }
  246. function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
  247. return {
  248. a: common_vendor.f($props.list, (item, index, i0) => {
  249. return common_vendor.e({
  250. a: $props.badge && item.isShow
  251. }, $props.badge && item.isShow ? {
  252. b: common_vendor.t(item[$props.keyName]),
  253. c: common_vendor.n(item.disabled && "u-tabs__wrapper__nav__item__text--disabled"),
  254. d: common_vendor.s($options.textStyle(index)),
  255. e: "035e7744-0-" + i0,
  256. f: common_vendor.p({
  257. text: item.num,
  258. absolute: "rightTop",
  259. size: "small"
  260. })
  261. } : {
  262. g: common_vendor.t(item[$props.keyName]),
  263. h: common_vendor.n(item.disabled && "u-tabs__wrapper__nav__item__text--disabled"),
  264. i: common_vendor.s($options.textStyle(index))
  265. }, {
  266. j: index,
  267. k: common_vendor.o(($event) => $options.clickHandler(item, index), index),
  268. l: `u-tabs__wrapper__nav__item-${index}`,
  269. m: common_vendor.n(`u-tabs__wrapper__nav__item-${index}`),
  270. n: common_vendor.n(item.disabled && "u-tabs__wrapper__nav__item--disabled")
  271. });
  272. }),
  273. b: common_vendor.s($data.addStyle($props.itemStyle)),
  274. c: common_vendor.s({
  275. flex: $props.scrollable ? "" : 1
  276. }),
  277. d: common_vendor.s({
  278. width: $data.addUnit($props.lineWidth),
  279. transform: `translate(${$data.lineOffsetLeft}px)`,
  280. transitionDuration: `${$data.firstTime ? 0 : $props.duration}ms`,
  281. height: $data.addUnit($props.lineHeight),
  282. background: $props.lineColor ? $props.lineColor : "var(--ui-BG-Main)",
  283. backgroundSize: $props.lineBgSize
  284. }),
  285. e: $props.scrollable,
  286. f: $data.scrollLeft
  287. };
  288. }
  289. const Component = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["render", _sfc_render], ["__scopeId", "data-v-035e7744"], ["__file", "D:/zx/mall-front-app/sheep/ui/su-tabs/su-tabs.vue"]]);
  290. wx.createComponent(Component);