@@ -19,6 +19,7 @@ export interface FormKitPrimeAutoCompleteProps {
19
19
minLength? : AutoCompleteProps [' minLength' ]
20
20
placeholder? : AutoCompleteProps [' placeholder' ]
21
21
fluid? : AutoCompleteProps [' fluid' ]
22
+ separators? : string [] | []
22
23
}
23
24
24
25
const props = defineProps ({
@@ -34,6 +35,12 @@ const suggestions = ref(['', {}])
34
35
suggestions .value = []
35
36
const loading = ref (false )
36
37
38
+ /**
39
+ * Searches for suggestions based on the input query.
40
+ * If options and optionLabel are provided, it filters the options.
41
+ * Otherwise, it calls the complete function from the context to fetch suggestions.
42
+ * @param event - The AutoCompleteCompleteEvent containing the query.
43
+ */
37
44
async function search(event : AutoCompleteCompleteEvent ) {
38
45
if (props .context ?.options && props .context ?.optionLabel ) {
39
46
suggestions .value = props .context .options .filter ((option ) => {
@@ -54,6 +61,38 @@ async function search(event: AutoCompleteCompleteEvent) {
54
61
}
55
62
}
56
63
}
64
+
65
+ /**
66
+ * Handles paste event to transform a string separated by any of the provided separators into multiple items.
67
+ * @param event - The paste event from the input.
68
+ */
69
+ function handlePaste(event : ClipboardEvent ) {
70
+ if (! props .context ?.multiple )
71
+ return
72
+ const clipboardData = event .clipboardData
73
+ if (! clipboardData )
74
+ return
75
+ const pastedText = clipboardData .getData (' text' )
76
+ const separators = Array .isArray (props .context ?.separators ) && props .context .separators .length > 0
77
+ ? props .context .separators
78
+ : [' ,' ]
79
+ // Create a regex to split by any separator, escaping special regex characters
80
+ const regex = new RegExp (` [${separators .map (s => s .replace (/ [-/\\ ^$*+?. ()|[\] {}] / g , ' \\ $&' )).join (' ' )}] ` )
81
+ if (pastedText && regex .test (pastedText )) {
82
+ event .preventDefault ()
83
+ const items = pastedText
84
+ .split (regex )
85
+ .map (item => item .trim ())
86
+ .filter (item => item .length > 0 )
87
+ if (Array .isArray (props .context ._value )) {
88
+ props .context ._value .push (... items )
89
+ }
90
+ else {
91
+ props .context ._value = items
92
+ }
93
+ props .context ?.node ?.input ?.(props .context ?._value )
94
+ }
95
+ }
57
96
</script >
58
97
59
98
<template >
@@ -85,6 +124,7 @@ async function search(event: AutoCompleteCompleteEvent) {
85
124
@complete =" search"
86
125
@change =" handleInput"
87
126
@blur =" handleBlur"
127
+ @paste =" handlePaste"
88
128
>
89
129
<template v-for =" slotName in validSlotNames " :key =" slotName " #[slotName ]=" slotProps " >
90
130
<component :is =" context?.slots[slotName]" v-bind =" { ...context, ...slotProps }" />
0 commit comments