FormToggle.vue 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <template>
  2. <div class="field" :class="{ 'pt-3' : hasOffset }" role="radiogroup" :aria-labelledby="inputId('label', fieldName)">
  3. <label v-if="label" :id="inputId('label', fieldName)" class="label" v-html="label"></label>
  4. <div class="is-toggle buttons">
  5. <button
  6. :id="inputId('button',fieldName + choice.value)"
  7. role="radio"
  8. type="button"
  9. class="button"
  10. :aria-checked="form[fieldName] === choice.value"
  11. :disabled="isDisabled"
  12. v-for="(choice, index) in choices"
  13. :key="index"
  14. :class="{
  15. 'is-link' : form[fieldName] === choice.value,
  16. 'is-dark' : $root.showDarkMode,
  17. 'is-multiline' : choice.legend,
  18. }"
  19. v-on:click.stop="setRadio(choice.value)"
  20. :title="choice.title ? choice.title : ''"
  21. >
  22. <input
  23. :id="inputId(inputType, choice.value)"
  24. :type="inputType"
  25. class="is-hidden"
  26. :checked="form[fieldName] === choice.value"
  27. :value="choice.value"
  28. v-model="form[fieldName]"
  29. :disabled="isDisabled"
  30. />
  31. <span v-if="choice.legend" v-html="choice.legend" class="is-block is-size-7"></span>
  32. <font-awesome-icon :icon="['fas', choice.icon]" v-if="choice.icon" class="mr-2" /> {{ choice.text }}
  33. </button>
  34. </div>
  35. <field-error :form="form" :field="fieldName" />
  36. <p class="help" v-html="help" v-if="help"></p>
  37. </div>
  38. </template>
  39. <script>
  40. export default {
  41. name: 'FormToggle',
  42. data() {
  43. return {
  44. inputType: 'radio'
  45. }
  46. },
  47. props: {
  48. label: {
  49. type: String,
  50. default: ''
  51. },
  52. fieldName: {
  53. type: String,
  54. default: '',
  55. required: true
  56. },
  57. choices: {
  58. type: Array,
  59. required: true
  60. },
  61. form: {
  62. type: Object,
  63. required: true
  64. },
  65. help: {
  66. type: String,
  67. default: ''
  68. },
  69. hasOffset: {
  70. type: Boolean,
  71. default: false
  72. },
  73. isDisabled: {
  74. type: Boolean,
  75. default: false
  76. }
  77. },
  78. methods: {
  79. setRadio(event) {
  80. this.form[this.fieldName] = event
  81. this.$emit(this.fieldName, this.form[this.fieldName])
  82. }
  83. }
  84. }
  85. </script>