You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

348 lines
11 KiB

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