u-radio.vue 11 KB

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