@@ -30,13 +30,24 @@ export function FormAutocomplete({
30
30
const { isOpen, open, close } = useOpenState ( ) ;
31
31
const [ ignoreBlur , setIgnoreBlur ] = useState ( false ) ;
32
32
const [ isFocused , setFocus ] = useState ( false ) ;
33
- const registerRef = useCallback ( register , [ register ] ) ;
34
33
const searchInputRef = useRef ( null ) ;
35
34
35
+ const registerRef = useCallback ( register , [ register ] ) ;
36
36
const disabled = normalizeDisabled ( _disabled , getFormData ( ) ) ;
37
37
38
38
const controlFeedback = getFormSubmitedAttempted ( ) ? ( isValid ( ) ? 'is-valid' : 'is-invalid' ) : '' ;
39
39
40
+ const updateSearchInputValidation = useCallback ( ( ) => {
41
+ searchInputRef . current . setCustomValidity ( controlFeedback === 'is-invalid' ? 'invalid' : '' ) ;
42
+ } , [ controlFeedback ] ) ;
43
+
44
+ const clearSearchValue = useCallback ( ( ) => {
45
+ if ( isEmpty ( value ) && ! isFocused ) {
46
+ setSearchValue ( '' ) ;
47
+ setSelectedItem ( null ) ;
48
+ }
49
+ } , [ isFocused , value ] ) ;
50
+
40
51
const onSearchInputType = useCallback (
41
52
( _ , nextSearchValue ) => {
42
53
setSearchValue ( nextSearchValue ) ;
@@ -70,6 +81,7 @@ export function FormAutocomplete({
70
81
if ( isEmpty ( searchValue ) && value ) {
71
82
setValue ( '' ) ;
72
83
setSelectedItem ( null ) ;
84
+ updateSearchInputValidation ( ) ;
73
85
}
74
86
75
87
if ( ignoreBlur ) {
@@ -78,7 +90,7 @@ export function FormAutocomplete({
78
90
close ( ) ;
79
91
setFocus ( false ) ;
80
92
}
81
- } , [ close , ignoreBlur , searchValue , setValue , value ] ) ;
93
+ } , [ close , ignoreBlur , searchValue , setValue , updateSearchInputValidation , value ] ) ;
82
94
83
95
const enableSearchInput = useCallback ( ( ) => {
84
96
if ( disabled ) {
@@ -105,17 +117,6 @@ export function FormAutocomplete({
105
117
[ close , setValue ]
106
118
) ;
107
119
108
- const updateSearchInputValidation = useCallback ( ( ) => {
109
- searchInputRef . current . setCustomValidity ( controlFeedback === 'is-invalid' ? 'invalid' : '' ) ;
110
- } , [ controlFeedback ] ) ;
111
-
112
- const clearSearchValue = useCallback ( ( ) => {
113
- if ( isEmpty ( value ) && ! isFocused ) {
114
- setSearchValue ( '' ) ;
115
- setSelectedItem ( null ) ;
116
- }
117
- } , [ isFocused , value ] ) ;
118
-
119
120
useEffect ( updateSearchInputValidation , [ updateSearchInputValidation ] ) ;
120
121
useEffect ( clearSearchValue , [ clearSearchValue ] ) ;
121
122
0 commit comments