-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(field-multiselect): setup syntax [khcp-11341] (#1885)
Convert KMultiselect to setup syntax for [KHCP-11341](https://konghq.atlassian.net/browse/KHCP-11341). Other fixes: - Add testing of style classes to other entities - add support for missing props to FieldMultiselect: - `id` - `classNames` - `disabled` - `name` - add `data-testid` to KPop --------- Co-authored-by: GU Yiling <[email protected]>
- Loading branch information
1 parent
cab3896
commit df4ffbb
Showing
9 changed files
with
336 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 71 additions & 23 deletions
94
packages/core/forms/src/components/fields/FieldMultiselect.vue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,89 @@ | ||
<template> | ||
<KMultiselect | ||
:id="getFieldID(schema)" | ||
v-model="inputValue" | ||
:aria-labelledby="getLabelId(schema)" | ||
:class="schema.fieldClasses" | ||
data-testid="field-multiselect" | ||
:disabled="disabled || undefined" | ||
:items="items" | ||
:kpop-attributes="{ 'data-testid': `${getFieldID(schema)}-items` }" | ||
:label-attributes="{ info: schema.help }" | ||
:model-value="value" | ||
:name="schema.inputName" | ||
:placeholder="schema.placeholder" | ||
:required="schema.required || undefined" | ||
width="100%" | ||
@update:model-value="onUpdate" | ||
/> | ||
</template> | ||
|
||
<script> | ||
import abstractField from './abstractField' | ||
<script lang="ts" setup> | ||
import { computed, toRef, type PropType } from 'vue' | ||
import type { MultiselectItem } from '@kong/kongponents' | ||
import composables from '../../composables' | ||
export default { | ||
mixins: [abstractField], | ||
const props = defineProps({ | ||
disabled: { | ||
type: Boolean, | ||
default: false, | ||
}, | ||
formOptions: { | ||
type: Object as PropType<Record<string, any>>, | ||
default: () => undefined, | ||
}, | ||
model: { | ||
type: Object as PropType<Record<string, any>>, | ||
default: () => undefined, | ||
}, | ||
schema: { | ||
type: Object as PropType<Record<string, any>>, | ||
required: true, | ||
}, | ||
vfg: { | ||
type: Object, | ||
required: true, | ||
}, | ||
/** | ||
* TODO: stronger type | ||
* TODO: pass this down to KInput error and errorMessage | ||
*/ | ||
errors: { | ||
type: Array, | ||
default: () => [], | ||
}, | ||
hint: { | ||
type: String, | ||
default: '', | ||
}, | ||
}) | ||
emits: ['model-updated'], | ||
const emit = defineEmits<{ | ||
(event: 'modelUpdated', value: any, model: Record<string, any>): void | ||
}>() | ||
computed: { | ||
items() { | ||
if (this.schema.values) { | ||
return this.schema.values | ||
} | ||
if (this.schema.elements?.one_of?.length) { | ||
return this.schema.elements.one_of.map(value => ({ label: value, value })) | ||
} | ||
return [] | ||
}, | ||
}, | ||
const modelRef = toRef(props, 'model') | ||
methods: { | ||
onUpdate(value) { | ||
this.$emit('model-updated', value, this.schema.model) | ||
}, | ||
const { getLabelId, getFieldID, clearValidationErrors, value: inputValue } = composables.useAbstractFields({ | ||
model: modelRef, | ||
schema: props.schema, | ||
formOptions: props.formOptions, | ||
emitModelUpdated: (data: { value: any, model: Record<string, any> }): void => { | ||
emit('modelUpdated', data.value, data.model) | ||
}, | ||
} | ||
}) | ||
defineExpose({ | ||
clearValidationErrors, | ||
}) | ||
const items = computed((): MultiselectItem[] => { | ||
if (props.schema.values) { | ||
return props.schema.values | ||
} | ||
if (props.schema.elements?.one_of?.length) { | ||
return props.schema.elements.one_of.map((value: string | number | boolean) => ({ label: String(value), value: String(value) } satisfies MultiselectItem)) | ||
} | ||
return [] | ||
}) | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.