@@ -5,6 +5,7 @@ import { Dropdown } from '../mixed/Dropdown';
5
5
import { useOpenState } from '../utils/useOpenState' ;
6
6
import { formatClasses } from '../utils/attributes' ;
7
7
import { useFormControl } from './helpers/useFormControl' ;
8
+ import { isEmpty } from '../utils/types' ;
8
9
9
10
export function FormAutocomplete ( {
10
11
onSearch,
@@ -40,6 +41,68 @@ export function FormAutocomplete({
40
41
searchInputRef . current . setCustomValidity ( controlFeedback === 'is-invalid' ? 'invalid' : '' ) ;
41
42
} , [ controlFeedback ] ) ;
42
43
44
+ useEffect ( ( ) => {
45
+ if ( isEmpty ( value ) && ! isFocused ) {
46
+ setSearchValue ( '' ) ;
47
+ setSelectedItem ( null ) ;
48
+ }
49
+ } , [ isFocused , value ] ) ;
50
+
51
+ const onSearchInputType = useCallback (
52
+ ( _ , nextSearchValue ) => {
53
+ setSearchValue ( nextSearchValue ) ;
54
+ onSearch ( nextSearchValue ) ;
55
+ open ( ) ;
56
+
57
+ if ( nextSearchValue && value ) {
58
+ setValue ( null ) ;
59
+ }
60
+ } ,
61
+ [ onSearch , open , setValue , value ]
62
+ ) ;
63
+
64
+ const onSearchInputFocus = useCallback ( ( ) => {
65
+ if ( openOnFocus ) {
66
+ setTimeout ( ( ) => {
67
+ open ( ) ;
68
+ } , 100 ) ;
69
+ }
70
+ } , [ ] ) ;
71
+
72
+ const onSearchInputBlur = useCallback ( ( ) => {
73
+ if ( ignoreBlur ) {
74
+ searchInputRef . current . focus ( ) ;
75
+ } else {
76
+ close ( ) ;
77
+ setFocus ( false ) ;
78
+ }
79
+ } , [ close , ignoreBlur ] ) ;
80
+
81
+ const enableSearchInput = useCallback ( ( ) => {
82
+ if ( disabled ) {
83
+ return ;
84
+ }
85
+
86
+ setFocus ( true ) ;
87
+ setTimeout ( ( ) => {
88
+ searchInputRef . current . focus ( ) ;
89
+ } ) ;
90
+ } , [ disabled ] ) ;
91
+
92
+ const onSelectItem = useCallback (
93
+ ( { value, label } ) => {
94
+ setValue ( value ) ;
95
+ setSearchValue ( label ) ;
96
+ setSelectedItem ( { value, label } ) ;
97
+ setIgnoreBlur ( false ) ;
98
+ setTimeout ( ( ) => {
99
+ setFocus ( false ) ;
100
+ close ( ) ;
101
+ } , 60 ) ;
102
+ } ,
103
+ [ close , setValue ]
104
+ ) ;
105
+
43
106
return (
44
107
< >
45
108
< input
@@ -48,31 +111,10 @@ export function FormAutocomplete({
48
111
ref = { searchInputRef }
49
112
className = { `form-control ${ isFocused ? '' : 'd-none' } ${ controlFeedback } ` }
50
113
onChange = { handleInputChange . bind ( null , {
51
- update ( _ , nextSearchValue ) {
52
- setSearchValue ( nextSearchValue ) ;
53
- onSearch ( nextSearchValue ) ;
54
- open ( ) ;
55
-
56
- if ( nextSearchValue && value ) {
57
- setValue ( null ) ;
58
- }
59
- } ,
114
+ update : onSearchInputType ,
60
115
} ) }
61
- onFocus = { ( ) => {
62
- if ( openOnFocus ) {
63
- setTimeout ( ( ) => {
64
- open ( ) ;
65
- } , 100 ) ;
66
- }
67
- } }
68
- onBlur = { ( ) => {
69
- if ( ignoreBlur ) {
70
- searchInputRef . current . focus ( ) ;
71
- } else {
72
- close ( ) ;
73
- setFocus ( false ) ;
74
- }
75
- } }
116
+ onFocus = { onSearchInputFocus }
117
+ onBlur = { onSearchInputBlur }
76
118
value = { searchValue }
77
119
role = "combobox"
78
120
aria-autocomplete = "list"
@@ -84,16 +126,7 @@ export function FormAutocomplete({
84
126
< div
85
127
className = { formatClasses ( [ 'form-control' , controlFeedback ] ) }
86
128
disabled = { disabled }
87
- onClick = { ( ) => {
88
- if ( disabled ) {
89
- return ;
90
- }
91
-
92
- setFocus ( true ) ;
93
- setTimeout ( ( ) => {
94
- searchInputRef . current . focus ( ) ;
95
- } ) ;
96
- } }
129
+ onClick = { enableSearchInput }
97
130
>
98
131
{ selectedItem ? ( template ? template ( selectedItem . label ) : selectedItem . label ) : placeholder }
99
132
</ div >
@@ -110,16 +143,7 @@ export function FormAutocomplete({
110
143
< Dropdown
111
144
isOpen = { isOpen ( ) }
112
145
items = { items . filter ( filter ( searchValue ) ) }
113
- onSelect = { ( { value, label } ) => {
114
- setValue ( value ) ;
115
- setSearchValue ( label ) ;
116
- setSelectedItem ( { value, label } ) ;
117
- setIgnoreBlur ( false ) ;
118
- setTimeout ( ( ) => {
119
- setFocus ( false ) ;
120
- close ( ) ;
121
- } , 60 ) ;
122
- } }
146
+ onSelect = { onSelectItem }
123
147
template = { template }
124
148
onClick = { ( e ) => e . stopPropation ( ) }
125
149
onTouchStart = { ( ) => setIgnoreBlur ( true ) }
0 commit comments