u-radio.vue 11 KB

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