fs-radio.vue 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <template>
  2. <view
  3. class="fs-radio"
  4. :class="['fs-radio-' + justify,{'fs-radio-reverse': reverse,'fs-radio-inline': inline}]"
  5. @click="handleToggle">
  6. <fs-icon
  7. v-if="icon"
  8. source="out"
  9. :type="icon"
  10. :color-type="selected ? checkedColorType : 'gray'"
  11. :size="iconSize"
  12. :color="checkedColor">
  13. </fs-icon>
  14. <fs-icon
  15. v-else
  16. :type="selected ? 'icon-checked' : 'icon-uncheck'"
  17. :color-type="selected ? checkedColorType : 'gray'"
  18. :size="iconSize"
  19. :color="checkedColor">
  20. </fs-icon>
  21. <view class="fs-radio-lable">
  22. {{label}}
  23. <slot />
  24. </view>
  25. </view>
  26. </template>
  27. <script setup>
  28. import { inject, reactive, watch, toRefs, ref } from 'vue'
  29. const props = defineProps({
  30. label: String,
  31. iconSize: {
  32. type: String,
  33. default: '42rpx'
  34. },
  35. value: {
  36. type: null,
  37. required: true
  38. },
  39. icon: String,
  40. checkedColor: String,
  41. checkedColorType: {
  42. type: String,
  43. default: 'primary'
  44. },
  45. checked: Boolean
  46. })
  47. const emits = defineEmits(['change'])
  48. const radioGroup = inject('radioGroup')
  49. const { reverse, justify, inline } = radioGroup
  50. let selected = ref(props.checked)
  51. watch(() => props.checked, val => {
  52. selected.value = val
  53. })
  54. radioGroup.updateChildren({
  55. selected,
  56. value: props.value
  57. })
  58. const handleToggle = () => {
  59. radioGroup.updateValue(props.value)
  60. }
  61. </script>
  62. <style lang="scss" scoped>
  63. .fs-radio{
  64. display: flex;
  65. align-items: center;
  66. justify-content: flex-start;
  67. padding: 10rpx 0;
  68. &-lable{
  69. margin-left: 6rpx;
  70. margin-right: 20rpx;
  71. }
  72. &-reverse{
  73. flex-direction: row-reverse;
  74. justify-content: flex-end;
  75. }
  76. &-reverse &-lable {
  77. margin-left: 20rpx;
  78. margin-right: 6rpx;
  79. }
  80. &-right{
  81. justify-content: space-between;
  82. }
  83. }
  84. </style>