fs-checkbox.vue 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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="selected ? (selectIcon || icon) : 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. selectIcon: String,
  33. iconSize: {
  34. type: String,
  35. default: '40rpx'
  36. },
  37. value: {
  38. type: null,
  39. required: true
  40. },
  41. checkedColor: String,
  42. checkedColorType: {
  43. type: String,
  44. default: 'primary'
  45. }
  46. })
  47. const checkboxGroup = inject('checkboxGroup')
  48. const { reverse, inline, justify } = checkboxGroup
  49. let selected = ref(false)
  50. checkboxGroup.updateChildren({
  51. selected,
  52. value: props.value
  53. })
  54. const handleToggle = () => {
  55. checkboxGroup.updateValue(props.value)
  56. }
  57. </script>
  58. <style lang="scss" scoped>
  59. .fs-checkbox {
  60. display: flex;
  61. align-items: center;
  62. justify-content: flex-start;
  63. margin-bottom: 14rpx;
  64. &-lable {
  65. margin-left: 6rpx;
  66. margin-right: 20rpx;
  67. }
  68. &-reverse {
  69. flex-direction: row-reverse;
  70. justify-content: flex-end;
  71. }
  72. &-reverse &-lable {
  73. margin-left: 0;
  74. margin-right: 6rpx;
  75. }
  76. &.right {
  77. justify-content: space-between;
  78. }
  79. }
  80. </style>