FormToggle.vue 2.5 KB

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