u-subsection.vue 9.9 KB

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