uv-radio.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. <template>
  2. <view
  3. class="uv-radio"
  4. @tap.stop="wrapperClickHandler"
  5. :style="[radioStyle]"
  6. :class="[`uv-radio-label--${parentData.iconPlacement}`, parentData.borderBottom && parentData.placement === 'column' && 'uv-border-bottom']"
  7. >
  8. <view
  9. class="uv-radio__icon-wrap"
  10. @tap.stop="iconClickHandler"
  11. :class="iconClasses"
  12. :style="[iconWrapStyle]"
  13. >
  14. <slot name="icon">
  15. <uv-icon
  16. class="uv-radio__icon-wrap__icon"
  17. name="checkbox-mark"
  18. :size="elIconSize"
  19. :color="elIconColor"
  20. />
  21. </slot>
  22. </view>
  23. <view
  24. class="uv-radio__label-wrap"
  25. @tap.stop="labelClickHandler">
  26. <slot>
  27. <text
  28. :style="{
  29. color: elDisabled ? elInactiveColor : elLabelColor,
  30. fontSize: elLabelSize,
  31. lineHeight: elLabelSize
  32. }"
  33. >{{label}}</text>
  34. </slot>
  35. </view>
  36. </view>
  37. </template>
  38. <script>
  39. import mpMixin from '@/uni_modules/uv-ui-tools/libs/mixin/mpMixin.js'
  40. import mixin from '@/uni_modules/uv-ui-tools/libs/mixin/mixin.js'
  41. import props from './props.js';
  42. /**
  43. * radio 单选框
  44. * @description 单选框用于有一个选择,用户只能选择其中一个的场景。搭配uv-radio-group使用
  45. * @tutorial https://www.uvui.cn/components/radio.html
  46. * @property {String | Number} name radio的名称
  47. * @property {String} shape 形状,square为方形,circle为圆型
  48. * @property {Boolean} disabled 是否禁用
  49. * @property {String | Boolean} labelDisabled 是否禁止点击提示语选中单选框
  50. * @property {String} activeColor 选中时的颜色,如设置parent的active-color将失效
  51. * @property {String} inactiveColor 未选中的颜色
  52. * @property {String | Number} iconSize 图标大小,单位px
  53. * @property {String | Number} labelSize label字体大小,单位px
  54. * @property {String | Number} label label提示文字,因为nvue下,直接slot进来的文字,由于特殊的结构,无法修改样式
  55. * @property {String | Number} size 整体的大小
  56. * @property {String} iconColor 图标颜色
  57. * @property {String} labelColor label的颜色
  58. * @property {Object} customStyle 组件的样式,对象形式
  59. *
  60. * @event {Function} change 某个radio状态发生变化时触发(选中状态)
  61. * @example <uv-radio :labelDisabled="false">门掩黄昏,无计留春住</uv-radio>
  62. */
  63. export default {
  64. name: "uv-radio",
  65. mixins: [mpMixin, mixin, props],
  66. data() {
  67. return {
  68. checked: false,
  69. // 当你看到这段代码的时候,
  70. // 父组件的默认值,因为头条小程序不支持在computed中使用this.parent.shape的形式
  71. // 故只能使用如此方法
  72. parentData: {
  73. iconSize: 12,
  74. labelSize: 14,
  75. labelColor: '#303133',
  76. labelDisabled: null,
  77. disabled: null,
  78. shape: null,
  79. activeColor: null,
  80. inactiveColor: null,
  81. size: 18,
  82. value: null,
  83. modelValue: null,
  84. iconColor: null,
  85. placement: 'row',
  86. borderBottom: false,
  87. iconPlacement: 'left'
  88. }
  89. }
  90. },
  91. computed: {
  92. // 是否禁用,如果父组件uv-raios-group禁用的话,将会忽略子组件的配置
  93. elDisabled() {
  94. return this.disabled !== '' ? this.disabled : this.parentData.disabled !== null ? this.parentData.disabled : false;
  95. },
  96. // 是否禁用label点击
  97. elLabelDisabled() {
  98. return this.labelDisabled !== '' ? this.labelDisabled : this.parentData.labelDisabled !== null ? this.parentData.labelDisabled :
  99. false;
  100. },
  101. // 组件尺寸,对应size的值,默认值为21px
  102. elSize() {
  103. return this.size ? this.size : (this.parentData.size ? this.parentData.size : 21);
  104. },
  105. // 组件的勾选图标的尺寸,默认12px
  106. elIconSize() {
  107. return this.iconSize ? this.iconSize : (this.parentData.iconSize ? this.parentData.iconSize : 12);
  108. },
  109. // 组件选中激活时的颜色
  110. elActiveColor() {
  111. return this.activeColor ? this.activeColor : (this.parentData.activeColor ? this.parentData.activeColor : '#2979ff');
  112. },
  113. // 组件选未中激活时的颜色
  114. elInactiveColor() {
  115. return this.inactiveColor ? this.inactiveColor : (this.parentData.inactiveColor ? this.parentData.inactiveColor :
  116. '#c8c9cc');
  117. },
  118. // label的颜色
  119. elLabelColor() {
  120. return this.labelColor ? this.labelColor : (this.parentData.labelColor ? this.parentData.labelColor : '#606266')
  121. },
  122. // 组件的形状
  123. elShape() {
  124. return this.shape ? this.shape : (this.parentData.shape ? this.parentData.shape : 'circle');
  125. },
  126. // label大小
  127. elLabelSize() {
  128. return this.$uv.addUnit(this.labelSize ? this.labelSize : (this.parentData.labelSize ? this.parentData.labelSize :
  129. '15'))
  130. },
  131. elIconColor() {
  132. const iconColor = this.iconColor ? this.iconColor : (this.parentData.iconColor ? this.parentData.iconColor :
  133. '#ffffff');
  134. // 图标的颜色
  135. if (this.elDisabled) {
  136. // disabled状态下,已勾选的radio图标改为elInactiveColor
  137. return this.checked ? this.elInactiveColor : 'transparent'
  138. } else {
  139. return this.checked ? iconColor : 'transparent'
  140. }
  141. },
  142. iconClasses() {
  143. let classes = []
  144. // 组件的形状
  145. classes.push('uv-radio__icon-wrap--' + this.elShape)
  146. if (this.elDisabled) {
  147. classes.push('uv-radio__icon-wrap--disabled')
  148. }
  149. if (this.checked && this.elDisabled) {
  150. classes.push('uv-radio__icon-wrap--disabled--checked')
  151. }
  152. // 支付宝,头条小程序无法动态绑定一个数组类名,否则解析出来的结果会带有",",而导致失效
  153. // #ifdef MP-ALIPAY || MP-TOUTIAO
  154. classes = classes.join(' ')
  155. // #endif
  156. return classes
  157. },
  158. iconWrapStyle() {
  159. // radio的整体样式
  160. const style = {}
  161. style.backgroundColor = this.checked && !this.elDisabled ? this.elActiveColor : '#ffffff'
  162. style.borderColor = this.checked && !this.elDisabled ? this.elActiveColor : this.elInactiveColor
  163. style.width = this.$uv.addUnit(this.elSize)
  164. style.height = this.$uv.addUnit(this.elSize)
  165. // 如果是图标在右边的话,移除它的右边距
  166. if (this.parentData.iconPlacement === 'right') {
  167. style.marginRight = 0
  168. }
  169. return style
  170. },
  171. radioStyle() {
  172. const style = {}
  173. if (this.parentData.borderBottom && this.parentData.placement === 'row') {
  174. this.$uv.error('检测到您将borderBottom设置为true,需要同时将uv-radio-group的placement设置为column才有效')
  175. }
  176. // 当父组件设置了显示下边框并且排列形式为纵向时,给内容和边框之间加上一定间隔
  177. if (this.parentData.borderBottom && this.parentData.placement === 'column') {
  178. // ios像素密度高,需要多一点的距离
  179. style.paddingBottom = this.$uv.os() === 'ios' ? '12px' : '8px'
  180. }
  181. return this.$uv.deepMerge(style, this.$uv.addStyle(this.customStyle))
  182. }
  183. },
  184. created() {
  185. this.init()
  186. },
  187. methods: {
  188. init() {
  189. // 支付宝小程序不支持provide/inject,所以使用这个方法获取整个父组件,在created定义,避免循环引用
  190. this.updateParentData()
  191. if (!this.parent) {
  192. this.$uv.error('uv-radio必须搭配uv-radio-group组件使用')
  193. }
  194. // 设置初始化时,是否默认选中的状态
  195. this.$nextTick(()=>{
  196. let parentValue = null;
  197. // #ifdef VUE2
  198. parentValue = this.parentData.value;
  199. // #endif
  200. // #ifdef VUE3
  201. parentValue = this.parentData.modelValue;
  202. // #endif
  203. this.checked = this.name === parentValue
  204. })
  205. },
  206. updateParentData() {
  207. this.getParentData('uv-radio-group')
  208. },
  209. // 点击图标
  210. iconClickHandler(e) {
  211. this.preventEvent(e)
  212. // 如果整体被禁用,不允许被点击
  213. if (!this.elDisabled) {
  214. this.setRadioCheckedStatus()
  215. }
  216. },
  217. // 横向两端排列时,点击组件即可触发选中事件
  218. wrapperClickHandler(e) {
  219. this.parentData.iconPlacement === 'right' && this.iconClickHandler(e)
  220. },
  221. // 点击label
  222. labelClickHandler(e) {
  223. this.preventEvent(e)
  224. // 如果按钮整体被禁用或者label被禁用,则不允许点击文字修改状态
  225. if (!this.elLabelDisabled && !this.elDisabled) {
  226. this.setRadioCheckedStatus()
  227. }
  228. },
  229. emitEvent() {
  230. // uv-radio的checked不为true时(意味着未选中),才发出事件,避免多次点击触发事件
  231. if (!this.checked) {
  232. this.$emit('change', this.name)
  233. // 尝试调用uv-form的验证方法,进行一定延迟,否则微信小程序更新可能会不及时
  234. this.$nextTick(() => {
  235. this.$uv.formValidate(this, 'change')
  236. })
  237. }
  238. },
  239. // 改变组件选中状态
  240. // 这里的改变的依据是,更改本组件的checked值为true,同时通过父组件遍历所有uv-radio实例
  241. // 将本组件外的其他uv-radio的checked都设置为false(都被取消选中状态),因而只剩下一个为选中状态
  242. setRadioCheckedStatus() {
  243. this.emitEvent()
  244. // 将本组件标记为选中状态
  245. this.checked = true
  246. typeof this.parent.unCheckedOther === 'function' && this.parent.unCheckedOther(this)
  247. }
  248. }
  249. }
  250. </script>
  251. <style lang="scss" scoped>
  252. $show-border: 1;
  253. $show-border-bottom: 1;
  254. @import '@/uni_modules/uv-ui-tools/libs/css/variable.scss';
  255. @import '@/uni_modules/uv-ui-tools/libs/css/components.scss';
  256. @import '@/uni_modules/uv-ui-tools/libs/css/color.scss';
  257. $uv-radio-label-wrap-padding-right: 6px !default;
  258. $uv-radio-wrap-font-size: 20px !default;
  259. $uv-radio-wrap-border-width: 1px !default;
  260. $uv-radio-wrap-border-color: #c8c9cc !default;
  261. $uv-radio-line-height: 0 !default;
  262. $uv-radio-circle-border-radius: 100% !default;
  263. $uv-radio-square-border-radius: 3px !default;
  264. $uv-radio-checked-color: #fff !default;
  265. $uv-radio-checked-background-color: red !default;
  266. $uv-radio-checked-border-color: #2979ff !default;
  267. $uv-radio-disabled-background-color: #ebedf0 !default;
  268. $uv-radio-disabled--checked-color: #c8c9cc !default;
  269. $uv-radio-label-margin-left: 5px !default;
  270. $uv-radio-label-margin-right: 12px !default;
  271. $uv-radio-label-color: $uv-content-color !default;
  272. $uv-radio-label-font-size: 15px !default;
  273. $uv-radio-label-disabled-color: #c8c9cc !default;
  274. .uv-radio {
  275. /* #ifndef APP-NVUE */
  276. @include flex(row);
  277. /* #endif */
  278. overflow: hidden;
  279. flex-direction: row;
  280. align-items: center;
  281. &-label--left {
  282. flex-direction: row
  283. }
  284. &-label--right {
  285. flex-direction: row-reverse;
  286. justify-content: space-between
  287. }
  288. &__icon-wrap {
  289. /* #ifndef APP-NVUE */
  290. box-sizing: border-box;
  291. // nvue下,border-color过渡有问题
  292. transition-property: border-color, background-color, color;
  293. transition-duration: 0.2s;
  294. /* #endif */
  295. color: $uv-content-color;
  296. @include flex;
  297. align-items: center;
  298. justify-content: center;
  299. color: transparent;
  300. text-align: center;
  301. font-size: $uv-radio-wrap-font-size;
  302. border-width: $uv-radio-wrap-border-width;
  303. border-color: $uv-radio-wrap-border-color;
  304. border-style: solid;
  305. /* #ifdef MP-TOUTIAO */
  306. // 头条小程序兼容性问题,需要设置行高为0,否则图标偏下
  307. &__icon {
  308. line-height: $uv-radio-line-height;
  309. }
  310. /* #endif */
  311. &--circle {
  312. border-radius: $uv-radio-circle-border-radius;
  313. }
  314. &--square {
  315. border-radius: $uv-radio-square-border-radius;
  316. }
  317. &--checked {
  318. color: $uv-radio-checked-color;
  319. background-color: $uv-radio-checked-background-color;
  320. border-color: $uv-radio-checked-border-color;
  321. }
  322. &--disabled {
  323. background-color: $uv-radio-disabled-background-color !important;
  324. }
  325. &--disabled--checked {
  326. color: $uv-radio-disabled--checked-color !important;
  327. }
  328. }
  329. &__label {
  330. /* #ifndef APP-NVUE */
  331. word-wrap: break-word;
  332. /* #endif */
  333. margin-left: $uv-radio-label-margin-left;
  334. margin-right: $uv-radio-label-margin-right;
  335. color: $uv-radio-label-color;
  336. font-size: $uv-radio-label-font-size;
  337. &--disabled {
  338. color: $uv-radio-label-disabled-color;
  339. }
  340. }
  341. &__label-wrap {
  342. flex: 1;
  343. padding-left: $uv-radio-label-wrap-padding-right;
  344. }
  345. }
  346. </style>