@@ -23,6 +23,7 @@ export default class Autocomplete extends Component {
2323 this . onBlur = this . onBlur . bind ( this ) ;
2424
2525 this . onClick = this . onClick . bind ( this ) ;
26+ this . onMouseDown = this . onMouseDown . bind ( this ) ;
2627 this . onChange = this . onChange . bind ( this ) ;
2728 this . onKeyDown = this . onKeyDown . bind ( this ) ;
2829 this . onInputClick = this . onInputClick . bind ( this ) ;
@@ -46,10 +47,11 @@ export default class Autocomplete extends Component {
4647 } ;
4748
4849 this . state = {
50+ valueFromSuggestion : props . strict ? props . value ?? props . suggestions [ 0 ] : '' ,
4951 activeSuggestion : 0 ,
5052 filteredSuggestions : [ ] ,
5153 showSuggestions : false ,
52- userInput : '' ,
54+ userInput : props . strict ? props . value ?? props . suggestions [ 0 ] : '' ,
5355 label : props . label ,
5456 position : null
5557 } ;
@@ -60,6 +62,7 @@ export default class Autocomplete extends Component {
6062 this . fieldRef . current . addEventListener ( 'scroll' , this . handleScroll ) ;
6163 this . recalculatePosition ( ) ;
6264 this . _ignoreBlur = false ;
65+ this . _suggestionClicked = false ;
6366 }
6467
6568 componentWillUnmount ( ) {
@@ -120,11 +123,12 @@ export default class Autocomplete extends Component {
120123 error : undefined
121124 } ) ;
122125
123- this . props . onChange && this . props . onChange ( userInput ) ;
126+ if ( ! this . props . strict ) this . props . onChange && this . props . onChange ( userInput ) ;
124127 }
125128
126129 onClick ( e ) {
127130 const userInput = e . currentTarget . innerText ;
131+ if ( this . props . strict ) this . props . onChange && this . props . onChange ( userInput ) ;
128132 const label = this . props . label || this . props . buildLabel ( userInput ) ;
129133
130134 this . inputRef . current . focus ( ) ;
@@ -144,15 +148,33 @@ export default class Autocomplete extends Component {
144148 ) ;
145149 }
146150
151+ onMouseDown ( e ) {
152+ this . _suggestionClicked = true ;
153+ this . props . onMouseDown && this . props . onMouseDown ( e ) ;
154+ }
155+
147156 onFocus ( e ) {
148157 if ( ! this . _ignoreBlur && ! this . state . showSuggestions ) {
149158 this . _ignoreBlur = true ;
150159 }
151-
160+ if ( this . props . strict ) e . target . select ( ) ;
152161 this . activate ( e ) ;
153162 }
154163
155164 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+ }
156178 this . props . onBlur && this . props . onBlur ( e ) ;
157179 }
158180
@@ -279,9 +301,10 @@ export default class Autocomplete extends Component {
279301 onChange,
280302 onClick,
281303 onBlur,
304+ onMouseDown,
282305 onFocus,
283306 onKeyDown,
284- props : { suggestionsStyle, inputStyle, placeholder, error } ,
307+ props : { suggestionsStyle, suggestionsItemStyle , inputStyle, containerStyle , placeholder, error } ,
285308 state : {
286309 activeSuggestion,
287310 filteredSuggestions,
@@ -314,19 +337,21 @@ export default class Autocomplete extends Component {
314337 onExternalClick = { onExternalClick }
315338 suggestions = { filteredSuggestions }
316339 suggestionsStyle = { suggestionsStyle }
340+ suggestionsItemStyle = { suggestionsItemStyle }
317341 activeSuggestion = { activeSuggestion }
318342 onClick = { onClick }
343+ onMouseDown = { onMouseDown }
319344 />
320345 ) ;
321346 }
322347
323348 return (
324349 < React . Fragment >
325- < div className = { fieldClassName } ref = { this . fieldRef } >
350+ < div style = { containerStyle } className = { fieldClassName } ref = { this . fieldRef } >
326351 < input
327352 id = { 1 }
328353 role = { 'combobox' }
329- autoComplete = 'off'
354+ autoComplete = "new-password"
330355 className = { inputClasses }
331356 placeholder = { placeholder }
332357 ref = { this . inputRef }
0 commit comments