fs-radio-group.vue 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <template>
  2. <view class="fs-radio-group" :class="{ inline }"><slot></slot></view>
  3. </template>
  4. <script>
  5. /**
  6. * 单选框组组件
  7. * @description 单选框组组件
  8. * @property {String} justify 图标对齐方式
  9. * @property {Boolean} reverse 是否反转
  10. * @property {Boolean} inline 单行显示
  11. * @property {Boolean} radius 是否圆角(仅对按钮样式有效)
  12. * @property {Boolean} round 是否半圆(仅对按钮样式有效)
  13. * @property {String} checkedColor 选中颜色
  14. * @property {String} checkedColorType = [primary | danger | warning | info | success] 选中颜色类型
  15. * @property {String} size = [mini | small | medium] 按钮大小(仅对按钮样式有效)
  16. * @event {Function} change change事件
  17. */
  18. export default {
  19. name: 'fs-radio-group'
  20. }
  21. </script>
  22. <script setup>
  23. import { provide, reactive, watch } from 'vue'
  24. const props = defineProps({
  25. justify: String,
  26. reverse: Boolean,
  27. inline: Boolean,
  28. checkedColor: String,
  29. checkedColorType: {
  30. type: String,
  31. default: 'primary',
  32. validator(value) {
  33. return ['primary', 'success', 'info', 'warning', 'danger'].includes(value)
  34. }
  35. },
  36. radius: Boolean,
  37. round: Boolean,
  38. size: {
  39. type: String,
  40. validator(value) {
  41. return ['mini', 'small', 'medium'].includes(value)
  42. }
  43. },
  44. modelValue: String
  45. })
  46. const emits = defineEmits(['update:modelValue', 'change'])
  47. const state = reactive({
  48. selectedValue: props.modelValue,
  49. children: []
  50. })
  51. watch(
  52. () => props.modelValue,
  53. val => {
  54. state.selectedValue = val
  55. }
  56. )
  57. const radioStrategy = value => {
  58. state.children.forEach(item => {
  59. item.selected = item.value === state.selectedValue
  60. })
  61. }
  62. const updateChildren = child => {
  63. state.children.push(child)
  64. radioStrategy()
  65. }
  66. const updateValue = value => (state.selectedValue = value)
  67. watch(
  68. () => state.selectedValue,
  69. val => {
  70. radioStrategy()
  71. emits('update:modelValue', val)
  72. emits('change', val)
  73. }
  74. )
  75. provide('radioGroup', {
  76. ...props,
  77. updateChildren,
  78. updateValue
  79. })
  80. </script>
  81. <style lang="scss">
  82. .fs-radio-group {
  83. &.inline {
  84. display: flex;
  85. flex-wrap: wrap;
  86. }
  87. }
  88. </style>