uv-subsection.vue 8.1 KB

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