You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

318 lines
10 KiB

5 months ago
  1. <template>
  2. <view
  3. class="u-subsection"
  4. ref="u-subsection"
  5. :class="[`u-subsection--${mode}`]"
  6. :style="[addStyle(customStyle), wrapperStyle]"
  7. >
  8. <view
  9. class="u-subsection__bar cursor-pointer"
  10. ref="u-subsection__bar"
  11. :style="[barStyle]"
  12. :class="[
  13. mode === 'button' && 'u-subsection--button__bar',
  14. innerCurrent === 0 &&
  15. mode === 'subsection' &&
  16. 'u-subsection__bar--first',
  17. innerCurrent > 0 &&
  18. innerCurrent < list.length - 1 &&
  19. mode === 'subsection' &&
  20. 'u-subsection__bar--center',
  21. innerCurrent === list.length - 1 &&
  22. mode === 'subsection' &&
  23. 'u-subsection__bar--last',
  24. ]"
  25. ></view>
  26. <view
  27. class="u-subsection__item cursor-pointer"
  28. :class="[
  29. `u-subsection__item--${index}`,
  30. index < list.length - 1 &&
  31. 'u-subsection__item--no-border-right',
  32. index === 0 && 'u-subsection__item--first',
  33. index === list.length - 1 && 'u-subsection__item--last',
  34. ]"
  35. :ref="`u-subsection__item--${index}`"
  36. :style="[itemStyle(index)]"
  37. @tap="clickHandler(index)"
  38. v-for="(item, index) in list"
  39. :key="index"
  40. >
  41. <text
  42. class="u-subsection__item__text"
  43. :style="[textStyle(index)]"
  44. >{{ getText(item) }}</text
  45. >
  46. </view>
  47. </view>
  48. </template>
  49. <script>
  50. // #ifdef APP-NVUE
  51. const dom = uni.requireNativePlugin("dom");
  52. const animation = uni.requireNativePlugin("animation");
  53. // #endif
  54. import { props } from "./props.js";
  55. import { mpMixin } from '../../libs/mixin/mpMixin';
  56. import { mixin } from '../../libs/mixin/mixin';
  57. import { addStyle, addUnit, sleep } from '../../libs/function/index';
  58. /**
  59. * Subsection 分段器
  60. * @description 该分段器一般用于用户从几个选项中选择某一个的场景
  61. * @tutorial https://ijry.github.io/uview-plus/components/subsection.html
  62. * @property {Array} list tab的数据
  63. * @property {String Number} current 当前活动的tab的index默认 0
  64. * @property {String} activeColor 激活时的颜色默认 '#3c9cff'
  65. * @property {String} inactiveColor 未激活时的颜色默认 '#303133'
  66. * @property {String} mode 模式选择mode=button为按钮形式mode=subsection时为分段模式默认 'button'
  67. * @property {String Number} fontSize 字体大小单位px默认 12
  68. * @property {Boolean} bold 激活选项的字体是否加粗默认 true
  69. * @property {String} bgColor 组件背景颜色mode为button时有效默认 '#eeeeef'
  70. * @property {Object} customStyle 定义需要用到的外部样式
  71. * @property {String} keyName `list`元素对象中读取的键名默认 'name'
  72. *
  73. * @event {Function} change 分段器选项发生改变时触发 回调 index选项的index索引值从0开始
  74. * @example <u-subsection :list="list" :current="curNow" @change="sectionChange"></u-subsection>
  75. */
  76. export default {
  77. name: "u-subsection",
  78. mixins: [mpMixin, mixin, props],
  79. data() {
  80. return {
  81. // 组件尺寸
  82. itemRect: {
  83. width: 0,
  84. height: 0,
  85. },
  86. innerCurrent: '',
  87. windowResizeCallback: {}
  88. };
  89. },
  90. watch: {
  91. list(newValue, oldValue) {
  92. this.init();
  93. },
  94. current: {
  95. immediate: true,
  96. handler(n) {
  97. if (n !== this.innerCurrent) {
  98. this.innerCurrent = n
  99. }
  100. // #ifdef APP-NVUE
  101. // 在安卓nvue上,如果通过translateX进行位移,到最后一个时,会导致右侧无法绘制圆角
  102. // 故用animation模块进行位移
  103. const ref = this.$refs?.["u-subsection__bar"]?.ref;
  104. // 不存在ref的时候(理解为第一次初始化时,需要渲染dom,进行一定延时再获取ref),这里的100ms是经过测试得出的结果(某些安卓需要延时久一点),勿随意修改
  105. sleep(ref ? 0 : 100).then(() => {
  106. animation.transition(this.$refs["u-subsection__bar"].ref, {
  107. styles: {
  108. transform: `translateX(${
  109. n * this.itemRect.width
  110. }px)`,
  111. transformOrigin: "center center",
  112. },
  113. duration: 300,
  114. });
  115. });
  116. // #endif
  117. },
  118. },
  119. },
  120. computed: {
  121. wrapperStyle() {
  122. const style = {};
  123. // button模式时,设置背景色
  124. if (this.mode === "button") {
  125. style.backgroundColor = this.bgColor;
  126. }
  127. return style;
  128. },
  129. // 滑块的样式
  130. barStyle() {
  131. const style = {};
  132. style.width = `${this.itemRect.width}px`;
  133. style.height = `${this.itemRect.height}px`;
  134. // 通过translateX移动滑块,其移动的距离为索引*item的宽度
  135. // #ifndef APP-NVUE
  136. style.transform = `translateX(${
  137. this.innerCurrent * this.itemRect.width
  138. }px)`;
  139. // #endif
  140. if (this.mode === "subsection") {
  141. // 在subsection模式下,需要动态设置滑块的圆角,因为移动滑块使用的是translateX,无法通过父元素设置overflow: hidden隐藏滑块的直角
  142. style.backgroundColor = this.activeColor;
  143. }
  144. return style;
  145. },
  146. // 分段器item的样式
  147. itemStyle(index) {
  148. return (index) => {
  149. const style = {};
  150. if (this.mode === "subsection") {
  151. // 设置border的样式
  152. style.borderColor = this.activeColor;
  153. style.borderWidth = "1px";
  154. style.borderStyle = "solid";
  155. }
  156. return style;
  157. };
  158. },
  159. // 分段器文字颜色
  160. textStyle(index) {
  161. return (index) => {
  162. const style = {};
  163. style.fontWeight =
  164. this.bold && this.innerCurrent === index ? "bold" : "normal";
  165. style.fontSize = addUnit(this.fontSize);
  166. // subsection模式下,激活时默认为白色的文字
  167. if (this.mode === "subsection") {
  168. style.color =
  169. this.innerCurrent === index ? "#fff" : this.inactiveColor;
  170. } else {
  171. // button模式下,激活时文字颜色默认为activeColor
  172. style.color =
  173. this.innerCurrent === index
  174. ? this.activeColor
  175. : this.inactiveColor;
  176. }
  177. return style;
  178. };
  179. },
  180. },
  181. mounted() {
  182. this.init();
  183. this.windowResizeCallback = (res) => {
  184. this.init();
  185. }
  186. uni.onWindowResize(this.windowResizeCallback)
  187. },
  188. beforeUnmount() {
  189. uni.offWindowResize(this.windowResizeCallback)
  190. },
  191. emits: ["change"],
  192. methods: {
  193. addStyle,
  194. init() {
  195. this.innerCurrent = this.current
  196. sleep().then(() => this.getRect());
  197. },
  198. // 判断展示文本
  199. getText(item) {
  200. return typeof item === 'object' ? item[this.keyName] : item
  201. },
  202. // 获取组件的尺寸
  203. getRect() {
  204. // #ifndef APP-NVUE
  205. this.$uGetRect(".u-subsection__item--0").then((size) => {
  206. this.itemRect = size;
  207. });
  208. // #endif
  209. // #ifdef APP-NVUE
  210. const ref = this.$refs["u-subsection__item--0"][0];
  211. ref &&
  212. dom.getComponentRect(ref, (res) => {
  213. this.itemRect = res.size;
  214. });
  215. // #endif
  216. },
  217. clickHandler(index) {
  218. this.innerCurrent = index
  219. this.$emit("change", index);
  220. },
  221. },
  222. };
  223. </script>
  224. <style lang="scss" scoped>
  225. @import "../../libs/css/components.scss";
  226. .u-subsection {
  227. @include flex;
  228. position: relative;
  229. overflow: hidden;
  230. /* #ifndef APP-NVUE */
  231. width: 100%;
  232. box-sizing: border-box;
  233. /* #endif */
  234. &--button {
  235. height: 34px;
  236. background-color: rgb(238, 238, 239);
  237. padding: 3px;
  238. border-radius: 4px;
  239. align-items: stretch;
  240. &__bar {
  241. background-color: #ffffff;
  242. border-radius: 4px !important;
  243. }
  244. }
  245. &--subsection {
  246. height: 32px;
  247. }
  248. &__bar {
  249. position: absolute;
  250. /* #ifndef APP-NVUE */
  251. transition-property: transform, color;
  252. transition-duration: 0.3s;
  253. transition-timing-function: ease-in-out;
  254. /* #endif */
  255. &--first {
  256. border-top-left-radius: 4px;
  257. border-bottom-left-radius: 4px;
  258. border-top-right-radius: 0px;
  259. border-bottom-right-radius: 0px;
  260. }
  261. &--center {
  262. border-top-left-radius: 0px;
  263. border-bottom-left-radius: 0px;
  264. border-top-right-radius: 0px;
  265. border-bottom-right-radius: 0px;
  266. }
  267. &--last {
  268. border-top-left-radius: 0px;
  269. border-bottom-left-radius: 0px;
  270. border-top-right-radius: 4px;
  271. border-bottom-right-radius: 4px;
  272. }
  273. }
  274. &__item {
  275. @include flex;
  276. flex: 1;
  277. justify-content: center;
  278. align-items: center;
  279. // vue环境下,需要设置相对定位,因为滑块为绝对定位,item需要在滑块的上面
  280. position: relative;
  281. &--no-border-right {
  282. border-right-width: 0 !important;
  283. }
  284. &--first {
  285. border-top-left-radius: 4px;
  286. border-bottom-left-radius: 4px;
  287. }
  288. &--last {
  289. border-top-right-radius: 4px;
  290. border-bottom-right-radius: 4px;
  291. }
  292. &__text {
  293. font-size: 12px;
  294. line-height: 14px;
  295. @include flex;
  296. align-items: center;
  297. transition-property: color;
  298. transition-duration: 0.3s;
  299. }
  300. }
  301. }
  302. </style>