fs-radio.vue 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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="selected ? (selectIcon || icon) : 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. selectIcon: String,
  41. checkedColor: String,
  42. checkedColorType: {
  43. type: String,
  44. default: 'primary'
  45. },
  46. checked: Boolean
  47. })
  48. const emits = defineEmits(['change'])
  49. const radioGroup = inject('radioGroup')
  50. const { reverse, justify, inline } = radioGroup
  51. let selected = ref(props.checked)
  52. watch(() => props.checked, val => {
  53. selected.value = val
  54. })
  55. radioGroup.updateChildren({
  56. selected,
  57. value: props.value
  58. })
  59. const handleToggle = () => {
  60. radioGroup.updateValue(props.value)
  61. }
  62. </script>
  63. <style lang="scss" scoped>
  64. .fs-radio{
  65. display: flex;
  66. align-items: center;
  67. justify-content: flex-start;
  68. padding: 10rpx 0;
  69. &-lable{
  70. margin-left: 6rpx;
  71. margin-right: 20rpx;
  72. }
  73. &-reverse{
  74. flex-direction: row-reverse;
  75. justify-content: flex-end;
  76. }
  77. &-reverse &-lable {
  78. margin-left: 20rpx;
  79. margin-right: 6rpx;
  80. }
  81. &-right{
  82. justify-content: space-between;
  83. }
  84. }
  85. </style>