SchemaFormSelectWidget.vue 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <!--
  2. Copyright 2019 Hackware SpA <human@hackware.cl>
  3. This file is part of "Hackware Userland" and licensed under the terms
  4. of the GNU Affero General Public License version 3, or (at your option)
  5. a later version. You should have received a copy of this license along
  6. with the software. If not, see <https://www.gnu.org/licenses/>.
  7. -->
  8. <template>
  9. <div>
  10. <select
  11. class="custom-select"
  12. disabled="readonly"
  13. :name="!readonly ? name : undefined"
  14. >
  15. <option
  16. v-for="option in options"
  17. :key="option.const"
  18. :value="option.const"
  19. :selected="isDefault(option)"
  20. v-text="option.title"
  21. />
  22. </select>
  23. <input
  24. v-if="!!property.default"
  25. type="hidden"
  26. :name="name"
  27. :value="property.default"
  28. >
  29. </div>
  30. </template>
  31. <script>
  32. export default {
  33. name: 'SchemaFormSelectWidget',
  34. props: {
  35. name: {
  36. type: String,
  37. required: true,
  38. },
  39. property: {
  40. type: Object,
  41. required: true,
  42. },
  43. readonly: {
  44. type: Boolean,
  45. default: false,
  46. },
  47. },
  48. computed: {
  49. options() { // Transform enum to oneOf schema spec: [{const: 'x', title: 'y'}, ...]
  50. let options;
  51. if ('enum' in this.property) {
  52. options = this.property.enum.map(val => ({ const: val, title: val }));
  53. } else if ('oneOf' in this.property) {
  54. options = this.property.oneOf;
  55. } else {
  56. throw ReferenceError('Unsupported select property, must contain "enum" or "oneOf"');
  57. }
  58. return options;
  59. },
  60. },
  61. methods: {
  62. isDefault(option) {
  63. return this.property.default === String(option.const);
  64. },
  65. },
  66. };
  67. </script>