FormCheckbox.vue 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. isLocked: Boolean,
  28. })
  29. const emit = defineEmits(['update:modelValue'])
  30. const legendId = useIdGenerator('legend', props.fieldName).inputId
  31. const attrs = useAttrs()
  32. const model = computed({
  33. get() {
  34. return props.modelValue;
  35. },
  36. set(value) {
  37. emit("update:modelValue", value);
  38. },
  39. })
  40. function toggleModel() {
  41. if (attrs['disabled'] != true) {
  42. model.value = !model.value
  43. }
  44. }
  45. </script>
  46. <template>
  47. <div class="field is-flex">
  48. <div v-if="isIndented" class="mx-2 pr-1" :class="{ 'is-opacity-5' : isDisabled || isLocked }">
  49. <FontAwesomeIcon class="has-text-grey" :icon="['fas', 'chevron-right']" transform="rotate-135"/>
  50. </div>
  51. <div>
  52. <input :id="fieldName" type="checkbox" :name="fieldName" class="is-checkradio is-info" v-model="model" :disabled="isDisabled || isLocked" :aria-describedby="help ? legendId : undefined" />
  53. <label tabindex="0" :for="fieldName" class="label" :class="labelClass" v-on:keypress.space.prevent="toggleModel">
  54. {{ $t(label) }}<FontAwesomeIcon v-if="isLocked" :icon="['fas', 'lock']" class="ml-2" size="xs" />
  55. </label>
  56. <p :id="legendId" class="help" v-html="$t(help)" v-if="help" />
  57. </div>
  58. </div>
  59. </template>