@@ -23,6 +23,7 @@ export default class Autocomplete extends Component {
23
23
this . onBlur = this . onBlur . bind ( this ) ;
24
24
25
25
this . onClick = this . onClick . bind ( this ) ;
26
+ this . onMouseDown = this . onMouseDown . bind ( this ) ;
26
27
this . onChange = this . onChange . bind ( this ) ;
27
28
this . onKeyDown = this . onKeyDown . bind ( this ) ;
28
29
this . onInputClick = this . onInputClick . bind ( this ) ;
@@ -46,10 +47,11 @@ export default class Autocomplete extends Component {
46
47
} ;
47
48
48
49
this . state = {
50
+ valueFromSuggestion : props . strict ? props . value ?? props . suggestions [ 0 ] : '' ,
49
51
activeSuggestion : 0 ,
50
52
filteredSuggestions : [ ] ,
51
53
showSuggestions : false ,
52
- userInput : '' ,
54
+ userInput : props . strict ? props . value ?? props . suggestions [ 0 ] : '' ,
53
55
label : props . label ,
54
56
position : null
55
57
} ;
@@ -60,6 +62,7 @@ export default class Autocomplete extends Component {
60
62
this . fieldRef . current . addEventListener ( 'scroll' , this . handleScroll ) ;
61
63
this . recalculatePosition ( ) ;
62
64
this . _ignoreBlur = false ;
65
+ this . _suggestionClicked = false ;
63
66
}
64
67
65
68
componentWillUnmount ( ) {
@@ -120,11 +123,12 @@ export default class Autocomplete extends Component {
120
123
error : undefined
121
124
} ) ;
122
125
123
- this . props . onChange && this . props . onChange ( userInput ) ;
126
+ if ( ! this . props . strict ) this . props . onChange && this . props . onChange ( userInput ) ;
124
127
}
125
128
126
129
onClick ( e ) {
127
130
const userInput = e . currentTarget . innerText ;
131
+ if ( this . props . strict ) this . props . onChange && this . props . onChange ( userInput ) ;
128
132
const label = this . props . label || this . props . buildLabel ( userInput ) ;
129
133
130
134
this . inputRef . current . focus ( ) ;
@@ -144,15 +148,33 @@ export default class Autocomplete extends Component {
144
148
) ;
145
149
}
146
150
151
+ onMouseDown ( e ) {
152
+ this . _suggestionClicked = true ;
153
+ this . props . onMouseDown && this . props . onMouseDown ( e ) ;
154
+ }
155
+
147
156
onFocus ( e ) {
148
157
if ( ! this . _ignoreBlur && ! this . state . showSuggestions ) {
149
158
this . _ignoreBlur = true ;
150
159
}
151
-
160
+ if ( this . props . strict ) e . target . select ( ) ;
152
161
this . activate ( e ) ;
153
162
}
154
163
155
164
onBlur ( e ) {
165
+ if ( this . props . strict ) {
166
+ if ( ! this . _suggestionClicked ) {
167
+ if ( ! this . props . suggestions . includes ( this . state . userInput ) ) {
168
+ this . setState ( { userInput : this . state . valueFromSuggestion } ) ;
169
+ this . props . onChange &&
170
+ this . props . onChange ( this . state . valueFromSuggestion ) ;
171
+ } else {
172
+ this . setState ( { valueFromSuggestion : this . state . userInput } ) ;
173
+ this . props . onChange && this . props . onChange ( this . state . userInput ) ;
174
+ }
175
+ }
176
+ this . _suggestionClicked = false ;
177
+ }
156
178
this . props . onBlur && this . props . onBlur ( e ) ;
157
179
}
158
180
@@ -279,9 +301,10 @@ export default class Autocomplete extends Component {
279
301
onChange,
280
302
onClick,
281
303
onBlur,
304
+ onMouseDown,
282
305
onFocus,
283
306
onKeyDown,
284
- props : { suggestionsStyle, inputStyle, placeholder, error } ,
307
+ props : { suggestionsStyle, suggestionsItemStyle , inputStyle, containerStyle , placeholder, error } ,
285
308
state : {
286
309
activeSuggestion,
287
310
filteredSuggestions,
@@ -314,19 +337,21 @@ export default class Autocomplete extends Component {
314
337
onExternalClick = { onExternalClick }
315
338
suggestions = { filteredSuggestions }
316
339
suggestionsStyle = { suggestionsStyle }
340
+ suggestionsItemStyle = { suggestionsItemStyle }
317
341
activeSuggestion = { activeSuggestion }
318
342
onClick = { onClick }
343
+ onMouseDown = { onMouseDown }
319
344
/>
320
345
) ;
321
346
}
322
347
323
348
return (
324
349
< React . Fragment >
325
- < div className = { fieldClassName } ref = { this . fieldRef } >
350
+ < div style = { containerStyle } className = { fieldClassName } ref = { this . fieldRef } >
326
351
< input
327
352
id = { 1 }
328
353
role = { 'combobox' }
329
- autoComplete = 'off'
354
+ autoComplete = "new-password"
330
355
className = { inputClasses }
331
356
placeholder = { placeholder }
332
357
ref = { this . inputRef }
0 commit comments