FormCheckbox.vue 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <script setup>
  2. defineOptions({
  3. inheritAttrs: false
  4. })
  5. const props = defineProps({
  6. modelValue: Boolean,
  7. fieldName: {
  8. type: String,
  9. default: '',
  10. required: true
  11. },
  12. label: {
  13. type: String,
  14. default: ''
  15. },
  16. labelClass: {
  17. type: String,
  18. default: ''
  19. },
  20. help: {
  21. type: String,
  22. default: ''
  23. },
  24. })
  25. const emit = defineEmits(['update:modelValue'])
  26. const attrs = useAttrs()
  27. const model = computed({
  28. get() {
  29. return props.modelValue;
  30. },
  31. set(value) {
  32. emit("update:modelValue", value);
  33. },
  34. })
  35. function toggleModel() {
  36. if (attrs['disabled'] != true) {
  37. model.value = !model.value
  38. }
  39. }
  40. </script>
  41. <template>
  42. <div class="field">
  43. <input :id="fieldName" type="checkbox" :name="fieldName" class="is-checkradio is-info" v-model="model" v-bind="$attrs"/>
  44. <label tabindex="0" :for="fieldName" class="label" :class="labelClass" v-html="$t(label)" v-on:keypress.space.prevent="toggleModel" />
  45. <p class="help" v-html="$t(help)" v-if="help" />
  46. </div>
  47. </template>