11/* eslint-disable react-hooks/exhaustive-deps */
2- import React from 'react' ;
2+ import React , { useMemo } from 'react' ;
33
44import PropTypes from 'prop-types' ;
55import clsx from 'clsx' ;
@@ -47,6 +47,12 @@ const Select = ({
4747 } ) ;
4848
4949 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 ) ] ) ;
5056
5157 if ( state . isLoading ) {
5258 return (
@@ -58,6 +64,9 @@ const Select = ({
5864 placeholder = { loadingMessage }
5965 options = { state . options }
6066 onChange = { ( ) => { } }
67+ onInputChange = { onInputChange }
68+ value = { selectValueInternal }
69+ isMulti = { isMulti }
6170 { ...loadingProps }
6271 noOptionsMessage = { renderNoOptionsMessage ( ) }
6372 { ...( state . originalOptions && { originalOptions : state . originalOptions } ) }
@@ -75,7 +84,7 @@ const Select = ({
7584 options = { state . options }
7685 classNamePrefix = { classNamePrefix }
7786 isMulti = { isMulti }
78- value = { selectValue }
87+ value = { selectValueInternal }
7988 onChange = { selectOnChange }
8089 onInputChange = { onInputChange }
8190 isFetching = { isFetching }
0 commit comments