1
1
<script setup lang='ts'>
2
2
import type { FormKitFrameworkContext } from ' @formkit/core'
3
3
import type { AutoCompleteCompleteEvent , AutoCompleteProps } from ' primevue/autocomplete'
4
-
5
4
import type { PropType } from ' vue'
6
- import { ref } from ' vue'
5
+ import { ref , watch } from ' vue'
7
6
import { useFormKitInput } from ' ../composables'
8
7
9
8
export interface FormKitPrimeAutoCompleteProps {
@@ -35,11 +34,30 @@ const suggestions = ref(['', {}])
35
34
suggestions .value = []
36
35
const loading = ref (false )
37
36
37
+ // Local value for v-model
38
+ const localValue = ref (props .context ._value )
39
+
40
+ // Sync localValue with context._value
41
+ watch (
42
+ () => props .context ._value ,
43
+ (newVal ) => {
44
+ localValue .value = newVal
45
+ },
46
+ )
47
+
48
+ // Emit changes from localValue to context
49
+ watch (
50
+ localValue ,
51
+ (newVal ) => {
52
+ if (newVal !== props .context ._value ) {
53
+ props .context ._value = newVal
54
+ props .context ?.node ?.input ?.(newVal )
55
+ }
56
+ },
57
+ )
58
+
38
59
/**
39
60
* 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
61
*/
44
62
async function search(event : AutoCompleteCompleteEvent ) {
45
63
if (props .context ?.options && props .context ?.optionLabel ) {
@@ -64,7 +82,6 @@ async function search(event: AutoCompleteCompleteEvent) {
64
82
65
83
/**
66
84
* 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
85
*/
69
86
function handlePaste(event : ClipboardEvent ) {
70
87
if (! props .context ?.multiple )
@@ -76,21 +93,20 @@ function handlePaste(event: ClipboardEvent) {
76
93
const separators = Array .isArray (props .context ?.separators ) && props .context .separators .length > 0
77
94
? props .context .separators
78
95
: [' ,' ]
79
- // Create a regex to split by any separator, escaping special regex characters
80
96
const regex = new RegExp (` [${separators .map (s => s .replace (/ [-/\\ ^$*+?. ()|[\] {}] / g , ' \\ $&' )).join (' ' )}] ` )
81
97
if (pastedText && regex .test (pastedText )) {
82
98
event .preventDefault ()
83
99
const items = pastedText
84
100
.split (regex )
85
101
.map (item => item .trim ())
86
102
.filter (item => item .length > 0 )
87
- if (Array .isArray (props .context ._value )) {
88
- props .context ._value .push (... items )
103
+ // Use a local ref, never mutate context._value directly
104
+ if (Array .isArray (localValue .value )) {
105
+ localValue .value = [... localValue .value , ... items ]
89
106
}
90
107
else {
91
- props . context . _value = items
108
+ localValue . value = items
92
109
}
93
- props .context ?.node ?.input ?.(props .context ?._value )
94
110
}
95
111
}
96
112
</script >
@@ -99,7 +115,7 @@ function handlePaste(event: ClipboardEvent) {
99
115
<div class =" p-formkit" >
100
116
<AutoComplete
101
117
:id =" context.id"
102
- v-model =" context._value "
118
+ v-model =" localValue "
103
119
v-bind =" context?.attrs"
104
120
:disabled =" !!context?.disabled"
105
121
:class =" context?.attrs?.class"
0 commit comments