u-subsection.vue 10 KB

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