@@ -18,20 +18,23 @@ export function FormAutocomplete({
18
18
filter,
19
19
disabled : _disabled ,
20
20
} ) {
21
+ const { getValue, setValue, register, isValid, getFormSubmitedAttempted, getFormData } = useFormControl ( name ) ;
22
+ const value = getValue ( ) ;
23
+
21
24
const [ searchValue , setSearchValue ] = useState ( '' ) ;
25
+ const items = normalizeOptions ( options , getFormData ( ) , searchValue ) ;
26
+ const _selectedItem = items . find ( ( item ) => item . value === value ) ;
27
+
28
+ const [ selectedItem , setSelectedItem ] = useState ( _selectedItem ) ;
22
29
const { isOpen, open, close } = useOpenState ( ) ;
23
30
const [ ignoreBlur , setIgnoreBlur ] = useState ( false ) ;
24
31
const [ isFocused , setFocus ] = useState ( false ) ;
25
- const { getValue, setValue, register, isValid, getFormSubmitedAttempted, getFormData } = useFormControl ( name ) ;
26
32
const registerRef = useCallback ( register , [ ] ) ;
27
33
const searchInputRef = useRef ( null ) ;
28
34
29
- const items = normalizeOptions ( options , getFormData ( ) , searchValue ) ;
30
- const value = getValue ( ) ;
31
- const selectedItem = items . find ( ( item ) => item . value === value ) ;
32
35
const disabled = normalizeDisabled ( _disabled , getFormData ( ) ) ;
33
36
34
- const controlFeedback = ` ${ getFormSubmitedAttempted ( ) ? ( isValid ( ) ? 'is-valid' : 'is-invalid' ) : '' } ` ;
37
+ const controlFeedback = getFormSubmitedAttempted ( ) ? ( isValid ( ) ? 'is-valid' : 'is-invalid' ) : '' ;
35
38
36
39
useEffect ( ( ) => {
37
40
searchInputRef . current . setCustomValidity ( controlFeedback === 'is-invalid' ? 'invalid' : '' ) ;
@@ -110,6 +113,7 @@ export function FormAutocomplete({
110
113
onSelect = { ( { value, label } ) => {
111
114
setValue ( value ) ;
112
115
setSearchValue ( label ) ;
116
+ setSelectedItem ( { value, label } ) ;
113
117
setIgnoreBlur ( false ) ;
114
118
setTimeout ( ( ) => {
115
119
setFocus ( false ) ;
@@ -135,6 +139,7 @@ FormAutocomplete.defaultProps = {
135
139
136
140
return itemValue . includes ( searchValue ) ;
137
141
} ,
142
+ template : ( x ) => x ,
138
143
} ;
139
144
140
145
FormAutocomplete . propTypes = {
0 commit comments