u-subsection.vue 14 KB

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