u-radio.vue 12 KB

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