fs-checkbox-button.vue 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <template>
  2. <view
  3. class="fs-checkbox-button"
  4. :class="[
  5. selected ? checkedColorType : 'fs-checkbox-button-default',
  6. { 'fs-checkbox-button-radius': radius, 'fs-checkbox-button-round': round },
  7. buttonSize
  8. ]"
  9. :style="{ color: checkedColor }"
  10. @click="handleToggle"
  11. >
  12. {{ label }}
  13. <slot />
  14. </view>
  15. </template>
  16. <script>
  17. /**
  18. * 多选框组件
  19. * @description 多选框组件
  20. * @property {String} label 文本
  21. * @property {null} value 标识符(必须传)
  22. * @property {String} size = [mini | small | medium] 按钮大小
  23. * @property {String} checkedColor 选中颜色
  24. * @property {String} checkedColorType = [primary | danger | warning | info | success] 选中颜色类型
  25. */
  26. export default {
  27. name: 'fs-checkbox-button'
  28. }
  29. </script>
  30. <script setup>
  31. import { inject, watch, toRefs, ref } from 'vue'
  32. const props = defineProps({
  33. label: String,
  34. value: {
  35. type: null,
  36. required: true
  37. },
  38. checkedColor: String,
  39. checkedColorType: String,
  40. size: {
  41. type: String,
  42. validator(value) {
  43. return ['mini', 'small', 'medium'].includes(value)
  44. }
  45. }
  46. })
  47. const checkboxGroup = inject('checkboxGroup')
  48. const { inline, radius, round } = checkboxGroup
  49. const checkedColorType = props.checkedColorType || checkboxGroup.checkedColorType
  50. const checkedColor = props.checkedColor || checkboxGroup.checkedColor
  51. const buttonSize = props.size || checkboxGroup.size
  52. let selected = ref(false)
  53. checkboxGroup.updateChildren({
  54. selected,
  55. value: props.value
  56. })
  57. const handleToggle = () => {
  58. checkboxGroup.updateValue(props.value)
  59. }
  60. </script>
  61. <style lang="scss" scoped>
  62. .fs-checkbox-button {
  63. padding: 10rpx 30rpx;
  64. white-space: nowrap;
  65. border: 2rpx solid currentColor;
  66. margin-right: 20rpx;
  67. margin-bottom: 20rpx;
  68. &-default {
  69. color: #999999;
  70. }
  71. &-radius {
  72. border-radius: var(--radius);
  73. }
  74. &-round {
  75. border-radius: 60rpx;
  76. }
  77. &.medium {
  78. padding: 8rpx 25rpx;
  79. font-size: 13px;
  80. }
  81. &.small {
  82. padding: 6rpx 20rpx;
  83. font-size: 12px;
  84. }
  85. &.mini {
  86. padding: 2rpx 15rpx;
  87. font-size: 11px;
  88. }
  89. }
  90. </style>