u-subsection.vue 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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. methods: {
  179. init() {
  180. uni.$u.sleep().then(() => this.getRect());
  181. },
  182. // 判断展示文本
  183. getText(item) {
  184. return typeof item === 'object' ? item[this.keyName] : item
  185. },
  186. // 获取组件的尺寸
  187. getRect() {
  188. // #ifndef APP-NVUE
  189. this.$uGetRect(".u-subsection__item--0").then((size) => {
  190. this.itemRect = size;
  191. });
  192. // #endif
  193. // #ifdef APP-NVUE
  194. const ref = this.$refs["u-subsection__item--0"][0];
  195. ref &&
  196. dom.getComponentRect(ref, (res) => {
  197. this.itemRect = res.size;
  198. });
  199. // #endif
  200. },
  201. clickHandler(index) {
  202. this.$emit("change", index);
  203. },
  204. },
  205. };
  206. </script>
  207. <style lang="scss" scoped>
  208. @import "../../libs/css/components.scss";
  209. .u-subsection {
  210. @include flex;
  211. position: relative;
  212. overflow: hidden;
  213. /* #ifndef APP-NVUE */
  214. width: 100%;
  215. box-sizing: border-box;
  216. /* #endif */
  217. &--button {
  218. height: 32px;
  219. background-color: rgb(238, 238, 239);
  220. padding: 3px;
  221. border-radius: 3px;
  222. align-items: stretch;
  223. &__bar {
  224. background-color: #ffffff;
  225. border-radius: 3px !important;
  226. }
  227. }
  228. &--subsection {
  229. height: 30px;
  230. }
  231. &__bar {
  232. position: absolute;
  233. /* #ifndef APP-NVUE */
  234. transition-property: transform, color;
  235. transition-duration: 0.3s;
  236. transition-timing-function: ease-in-out;
  237. /* #endif */
  238. &--first {
  239. border-top-left-radius: 3px;
  240. border-bottom-left-radius: 3px;
  241. border-top-right-radius: 0px;
  242. border-bottom-right-radius: 0px;
  243. }
  244. &--center {
  245. border-top-left-radius: 0px;
  246. border-bottom-left-radius: 0px;
  247. border-top-right-radius: 0px;
  248. border-bottom-right-radius: 0px;
  249. }
  250. &--last {
  251. border-top-left-radius: 0px;
  252. border-bottom-left-radius: 0px;
  253. border-top-right-radius: 3px;
  254. border-bottom-right-radius: 3px;
  255. }
  256. }
  257. &__item {
  258. @include flex;
  259. flex: 1;
  260. justify-content: center;
  261. align-items: center;
  262. // vue环境下,需要设置相对定位,因为滑块为绝对定位,item需要在滑块的上面
  263. position: relative;
  264. &--no-border-right {
  265. border-right-width: 0 !important;
  266. }
  267. &--first {
  268. border-top-left-radius: 3px;
  269. border-bottom-left-radius: 3px;
  270. }
  271. &--last {
  272. border-top-right-radius: 3px;
  273. border-bottom-right-radius: 3px;
  274. }
  275. &__text {
  276. font-size: 12px;
  277. line-height: 12px;
  278. @include flex;
  279. align-items: center;
  280. transition-property: color;
  281. transition-duration: 0.3s;
  282. }
  283. }
  284. }
  285. </style>