fs-checkbox.vue 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <template>
  2. <view
  3. class="fs-checkbox"
  4. :class="[justify, { 'fs-checkbox-reverse': reverse, 'fs-checkbox-inline': inline }]"
  5. @click="handleToggle"
  6. >
  7. <fs-icon
  8. v-if="icon"
  9. source="out"
  10. :type="selected ? selectIcon || icon : icon"
  11. :colorType="selected ? checkedColorType : 'gray'"
  12. :size="iconSize"
  13. :color="checkedColor"
  14. ></fs-icon>
  15. <fs-icon
  16. v-else
  17. :type="selected ? 'icon-squarecheck' : 'icon-square'"
  18. :colorType="selected ? checkedColorType : 'gray'"
  19. :size="iconSize"
  20. :color="checkedColor"
  21. ></fs-icon>
  22. <view class="fs-checkbox-lable">
  23. {{ label }}
  24. <slot />
  25. </view>
  26. </view>
  27. </template>
  28. <script>
  29. /**
  30. * 多选框组件
  31. * @description 多选框组件
  32. * @property {String} label 文本
  33. * @property {String} icon 自定义图标
  34. * @property {String} iconSize 图标大小
  35. * @property {String} selectIcon 自定义选中图标
  36. * @property {null} value 标识符(必须传)
  37. * @property {String} checkedColor 选中颜色
  38. * @property {String} checkedColorType = [primary | danger | warning | info | success] 选中颜色类型
  39. */
  40. export default {
  41. name: 'fs-checkbox'
  42. }
  43. </script>
  44. <script setup>
  45. import { inject, watch, toRefs, ref, computed } from 'vue'
  46. const props = defineProps({
  47. label: String,
  48. icon: String,
  49. selectIcon: String,
  50. iconSize: {
  51. type: String,
  52. default: '40rpx'
  53. },
  54. value: {
  55. type: null,
  56. required: true
  57. },
  58. checkedColor: String,
  59. checkedColorType: {
  60. type: String,
  61. default: 'primary',
  62. validator(value) {
  63. return ['primary', 'success', 'info', 'warning', 'danger'].includes(value)
  64. }
  65. }
  66. })
  67. const checkboxGroup = inject('checkboxGroup')
  68. const { reverse, inline, justify } = checkboxGroup
  69. let selected = ref(false)
  70. checkboxGroup.updateChildren({
  71. selected,
  72. value: props.value
  73. })
  74. const handleToggle = () => {
  75. checkboxGroup.updateValue(props.value)
  76. }
  77. </script>
  78. <style lang="scss" scoped>
  79. .fs-checkbox {
  80. display: flex;
  81. align-items: center;
  82. justify-content: flex-start;
  83. margin-bottom: 14rpx;
  84. &-lable {
  85. margin-left: 6rpx;
  86. margin-right: 20rpx;
  87. }
  88. &-reverse {
  89. flex-direction: row-reverse;
  90. justify-content: flex-end;
  91. }
  92. &-reverse &-lable {
  93. margin-left: 0;
  94. margin-right: 6rpx;
  95. }
  96. &.right {
  97. justify-content: space-between;
  98. }
  99. }
  100. </style>