1
1
/* eslint-disable react-hooks/exhaustive-deps */
2
- import React from 'react' ;
2
+ import React , { useMemo } from 'react' ;
3
3
4
4
import PropTypes from 'prop-types' ;
5
5
import clsx from 'clsx' ;
@@ -47,6 +47,12 @@ const Select = ({
47
47
} ) ;
48
48
49
49
const renderNoOptionsMessage = ( ) => ( Object . values ( state . promises ) . some ( ( value ) => value ) ? ( ) => updatingMessage : ( ) => noOptionsMessage ) ;
50
+ // When isMulti is true, the "getSelect" always creates new value array, we need to memoize it to not create new array instance
51
+ // Memo is required to fix https://github.com/data-driven-forms/react-forms/issues/1366
52
+ // Keeping prev values in ref and calling lodash.isEqual is not reliable as it ca return false positive beucase it only has true/false result.
53
+ // If we have multiple updates during one reconciliation pahse the search input reset will trigger on initial key stroke
54
+ // JSON.stringify is expensive but seems to be working better.
55
+ const selectValueInternal = useMemo ( ( ) => selectValue , [ JSON . stringify ( selectValue ) ] ) ;
50
56
51
57
if ( state . isLoading ) {
52
58
return (
@@ -58,6 +64,9 @@ const Select = ({
58
64
placeholder = { loadingMessage }
59
65
options = { state . options }
60
66
onChange = { ( ) => { } }
67
+ onInputChange = { onInputChange }
68
+ value = { selectValueInternal }
69
+ isMulti = { isMulti }
61
70
{ ...loadingProps }
62
71
noOptionsMessage = { renderNoOptionsMessage ( ) }
63
72
{ ...( state . originalOptions && { originalOptions : state . originalOptions } ) }
@@ -75,7 +84,7 @@ const Select = ({
75
84
options = { state . options }
76
85
classNamePrefix = { classNamePrefix }
77
86
isMulti = { isMulti }
78
- value = { selectValue }
87
+ value = { selectValueInternal }
79
88
onChange = { selectOnChange }
80
89
onInputChange = { onInputChange }
81
90
isFetching = { isFetching }
0 commit comments