Skip to content

Commit c66b6a3

Browse files
committed
feat(PrimeAutoComplete): Add options support with sample #61
1 parent d5a312a commit c66b6a3

File tree

3 files changed

+27
-3
lines changed

3 files changed

+27
-3
lines changed

dev/pages/inputs/AutoComplete.vue

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ function search(query: string) {
77
return [...list.filter(i => i.toLowerCase().includes(query.toLowerCase())), query]
88
}
99
10+
const userList = [
11+
{ id: '1', name: 'Tom', value: '123' },
12+
{ id: '2', name: 'Tim', value: '124' },
13+
]
14+
1015
const schema
1116
= [
1217
{
@@ -17,6 +22,15 @@ const schema
1722
dropdown: true,
1823
label: 'Basic AutoComplete - Use [h]ello',
1924
},
25+
{
26+
$formkit: 'primeAutoComplete',
27+
id: 'basic',
28+
name: 'id',
29+
dropdown: true,
30+
label: 'Object AutoComplete - Use [t]om',
31+
options: userList,
32+
optionLabel: 'name',
33+
},
2034
{
2135
$formkit: 'primeAutoComplete',
2236
id: 'chips',
@@ -27,7 +41,7 @@ const schema
2741
},
2842
]
2943
30-
const data = { }
44+
const data = { id: { id: '1', name: 'Tom', value: '123' } }
3145
</script>
3246

3347
<template>

src/components/PrimeAutoComplete.vue

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ export interface FormKitPrimeAutoCompleteProps {
1212
dropdown?: AutoCompleteProps['dropdown']
1313
multiple?: AutoCompleteProps['multiple']
1414
typeahead?: AutoCompleteProps['typeahead']
15+
optionLabel?: AutoCompleteProps['optionLabel']
16+
options?: any[] | undefined
1517
}
1618
1719
const props = defineProps({
@@ -26,7 +28,14 @@ const { validSlotNames, unstyled, isInvalid, handleInput, handleBlur } = useForm
2628
const suggestions = ref([])
2729
2830
function search(event: AutoCompleteCompleteEvent) {
29-
suggestions.value = props.context?.attrs.complete(event.query)
31+
if (props.context?.options && props.context?.optionLabel) {
32+
suggestions.value = props.context.options.filter((option) => {
33+
return option[`${props.context.optionLabel}`].toString().toLowerCase().includes(event.query.toLowerCase())
34+
})
35+
}
36+
else {
37+
suggestions.value = props.context?.attrs.complete(event.query)
38+
}
3039
}
3140
</script>
3241

@@ -43,6 +52,7 @@ function search(event: AutoCompleteCompleteEvent) {
4352
:aria-label="context?.attrs.ariaLabel"
4453
:aria-labelledby="context?.attrs.ariaLabelledby"
4554
:suggestions="suggestions"
55+
:option-label="context?.optionLabel"
4656
:dropdown="context?.dropdown ?? false"
4757
:multiple="context?.multiple ?? false"
4858
:typeahead="context?.typeahead ?? true"

src/definitions/input.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import PrimeToggleSwitch from '../components/PrimeToggleSwitch.vue'
2626
import PrimeTreeSelect from '../components/PrimeTreeSelect.vue'
2727

2828
export const primeAutoCompleteDefinition: FormKitTypeDefinition = createInput(PrimeAutoComplete, {
29-
props: ['pt', 'ptOptions', 'unstyled', 'Select', 'multiple', 'typeahead'],
29+
props: ['pt', 'ptOptions', 'unstyled', 'Select', 'multiple', 'typeahead', 'optionLabel', 'options'],
3030
})
3131
export const primeInputTextDefinition: FormKitTypeDefinition = createInput(PrimeInputText, {
3232
props: ['pt', 'ptOptions', 'unstyled', 'placeholder', 'iconPrefix', 'iconSuffix'],

0 commit comments

Comments
 (0)