fs-checkbox.vue 1.6 KB

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