fs-radio-group.vue 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <template>
  2. <view class="fs-radio-group" :class="{inline}">
  3. <slot></slot>
  4. </view>
  5. </template>
  6. <script setup>
  7. import { provide, reactive, watch } from 'vue'
  8. const props = defineProps({
  9. justify: String,
  10. reverse: Boolean,
  11. inline: Boolean,
  12. checkedColor: String,
  13. checkedColorType: {
  14. type: String,
  15. default: 'primary',
  16. validator(value) {
  17. return ['primary', 'success', 'info', 'warning', 'danger'].includes(value)
  18. }
  19. },
  20. radius: Boolean,
  21. round: Boolean,
  22. size: {
  23. type: String,
  24. validator(value) {
  25. return ['mini', 'small', 'medium'].includes(value)
  26. }
  27. },
  28. modelValue: String
  29. })
  30. const emits = defineEmits(['update:modelValue', 'change'])
  31. const state = reactive({
  32. selectedValue: props.modelValue,
  33. children: []
  34. })
  35. watch(() => props.modelValue, val => {
  36. state.selectedValue = val
  37. })
  38. const radioStrategy = value => {
  39. state.children.forEach(item => {
  40. item.selected = item.value === state.selectedValue
  41. })
  42. }
  43. const updateChildren = child => {
  44. state.children.push(child)
  45. radioStrategy()
  46. }
  47. const updateValue = value => state.selectedValue = value
  48. watch(() => state.selectedValue, val => {
  49. radioStrategy()
  50. emits('update:modelValue', val)
  51. emits('change', val)
  52. })
  53. provide('radioGroup', {
  54. ...props,
  55. updateChildren,
  56. updateValue
  57. })
  58. </script>
  59. <style lang="scss">
  60. .fs-radio-group{
  61. &.inline{
  62. display: flex;
  63. flex-wrap: wrap;
  64. }
  65. }
  66. </style>