|
| 1 | +<template lang="pug"> |
| 2 | +//control-wrapper(v-bind="controlWrapper" :styles="styles" :isFocused="isFocused" :appliedOptions="appliedOptions") |
| 3 | +div |
| 4 | + q-select( |
| 5 | + :model-value="control.data" |
| 6 | + :options="suggestions" |
| 7 | + :input-debounce="0" |
| 8 | + fill-input |
| 9 | + :use-input="suggestions.length === 0" |
| 10 | + use-chips |
| 11 | + :id="control.id + '-input'" |
| 12 | + :disable="!control.enabled" |
| 13 | + :placeholder="appliedOptions.placeholder" |
| 14 | + :label="computedLabel" |
| 15 | + :hint="control.description" |
| 16 | + :error="control.errors !== ''" |
| 17 | + :error-message="control.errors" |
| 18 | + :clearable="true" |
| 19 | + :hide-dropdown-icon="suggestions.length === 0" |
| 20 | + @update:model-value="onChange" |
| 21 | + @focus="isFocused = true" |
| 22 | + @blur="isFocused = false" |
| 23 | + @new-value="createValue" |
| 24 | + filled multiple |
| 25 | + ) |
| 26 | + //- q-input( |
| 27 | + //- v-else |
| 28 | + //- :id="control.id + '-input'" |
| 29 | + //- :disable="!control.enabled" |
| 30 | + //- :placeholder="appliedOptions.placeholder" |
| 31 | + //- :label="computedLabel" |
| 32 | + //- :hint="control.description" |
| 33 | + //- :error="control.errors !== ''" |
| 34 | + //- :error-message="control.errors" |
| 35 | + //- :model-value="control.data" |
| 36 | + //- :maxlength="appliedOptions.restrict ? control.schema.maxLength : undefined" |
| 37 | + //- :clearable="true" |
| 38 | + //- @update:model-value="onChange" |
| 39 | + //- @focus="isFocused = true" |
| 40 | + //- @blur="isFocused = false" |
| 41 | + //- filled |
| 42 | + //- ) |
| 43 | +</template> |
| 44 | + |
| 45 | +<script lang="ts"> |
| 46 | +import { defineComponent } from 'vue'; |
| 47 | +import { rendererProps, useJsonFormsControl } from '@jsonforms/vue'; |
| 48 | +import { |
| 49 | + isControl, |
| 50 | + isStringControl, |
| 51 | + rankWith, |
| 52 | +} from '@jsonforms/core'; |
| 53 | +import type { JsonFormsRendererRegistryEntry } from '@jsonforms/core'; |
| 54 | +import { isArray, isObject, isString, iterate } from 'radash'; |
| 55 | +import type { RendererProps } from '@jsonforms/vue'; |
| 56 | +import type { ControlElement } from '@jsonforms/core'; |
| 57 | +import { useQuasarControl } from '../util'; |
| 58 | +import { ControlWrapper } from '@jsonforms/vue-vanilla'; |
| 59 | +
|
| 60 | +const QStringControlRenderer = defineComponent({ |
| 61 | + name: 'q-string-control-renderer', |
| 62 | + components: { |
| 63 | + ControlWrapper, |
| 64 | + }, |
| 65 | + props: { |
| 66 | + ...rendererProps<ControlElement>(), |
| 67 | + }, |
| 68 | + setup(props: RendererProps<ControlElement>) { |
| 69 | + return useQuasarControl( |
| 70 | + useJsonFormsControl(props), |
| 71 | + (value) => isObject(value) ? value.value : value || undefined |
| 72 | + ) |
| 73 | + }, |
| 74 | + methods: { |
| 75 | + createValue(val, done) { |
| 76 | + done(val, 'add-unique') |
| 77 | + }, |
| 78 | + isIterable(obj) { |
| 79 | + // checks for null and undefined |
| 80 | + if (obj == null) { |
| 81 | + return false; |
| 82 | + } |
| 83 | + return typeof obj[Symbol.iterator] === 'function'; |
| 84 | + } |
| 85 | + }, |
| 86 | + computed: { |
| 87 | + suggestions() { |
| 88 | + const suggestions = this.control.uischema.options?.suggestion; |
| 89 | +
|
| 90 | + let everyString = false |
| 91 | + if (this.isIterable(suggestions) && suggestions.length > 0) { |
| 92 | + for (const suggestion of suggestions) { |
| 93 | + if (typeof suggestion !== 'string') { |
| 94 | + everyString = false |
| 95 | + break |
| 96 | + } |
| 97 | + everyString = true |
| 98 | + } |
| 99 | + } |
| 100 | +
|
| 101 | + if ( |
| 102 | + suggestions === undefined || |
| 103 | + !isArray(suggestions) || |
| 104 | + !everyString |
| 105 | + ) { |
| 106 | + // check for incorrect data |
| 107 | + return []; |
| 108 | + } |
| 109 | + return suggestions; |
| 110 | + }, |
| 111 | + computedLabel() { |
| 112 | + return this.control.label === undefined ? this.control.schema.title : this.control.label; |
| 113 | + }, |
| 114 | + }, |
| 115 | +}); |
| 116 | +export default QStringControlRenderer; |
| 117 | +
|
| 118 | +</script> |
0 commit comments