u-radio.vue 14 KB

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