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.
 
 
 

355 line
12 KiB

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