FormCheckbox.vue 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <script setup>
  2. import { useIdGenerator } from '@/composables/helpers'
  3. defineOptions({
  4. inheritAttrs: false
  5. })
  6. const props = defineProps({
  7. modelValue: Boolean,
  8. fieldName: {
  9. type: String,
  10. default: '',
  11. required: true
  12. },
  13. label: {
  14. type: String,
  15. default: ''
  16. },
  17. labelClass: {
  18. type: String,
  19. default: ''
  20. },
  21. help: {
  22. type: String,
  23. default: ''
  24. },
  25. isIndented: Boolean,
  26. isDisabled: Boolean,
  27. })
  28. const emit = defineEmits(['update:modelValue'])
  29. const legendId = useIdGenerator('legend', props.fieldName).inputId
  30. const attrs = useAttrs()
  31. const model = computed({
  32. get() {
  33. return props.modelValue;
  34. },
  35. set(value) {
  36. emit("update:modelValue", value);
  37. },
  38. })
  39. function toggleModel() {
  40. if (attrs['disabled'] != true) {
  41. model.value = !model.value
  42. }
  43. }
  44. </script>
  45. <template>
  46. <div class="field is-flex">
  47. <div v-if="isIndented" class="mx-2 pr-1" :style="{ 'opacity': isDisabled ? '0.5' : '1' }">
  48. <FontAwesomeIcon class="has-text-grey" :icon="['fas', 'chevron-right']" transform="rotate-135"/>
  49. </div>
  50. <div>
  51. <input :id="fieldName" type="checkbox" :name="fieldName" class="is-checkradio is-info" v-model="model" :disabled="isDisabled" :aria-describedby="help ? legendId : undefined" />
  52. <label tabindex="0" :for="fieldName" class="label" :class="labelClass" v-html="$t(label)" v-on:keypress.space.prevent="toggleModel" />
  53. <p :id="legendId" class="help" v-html="$t(help)" v-if="help" />
  54. </div>
  55. </div>
  56. </template>