File tree Expand file tree Collapse file tree
static/js/publisher/components/ComboBox Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -169,19 +169,22 @@ const ComboBox: FC<ComboBoxProps> = ({
169169 } ) ;
170170 } , [ value , options ] ) ;
171171
172- const firstRenderRef = useRef ( true ) ;
172+ // we don't run onChange until after we actually render some real options to choose from, this
173+ // avoids running it multiple times with and empty value during init (e.g. when fetching the
174+ // options from an API)
175+ const isOnchangeDisabledRef = useRef ( true ) ;
173176
174177 // Run the onChange callback when we change the selectedItem. We don't pass the callback as a
175178 // Downshift prop because it wouldn't run when we set the selectedItem inside the state reducer
176179 useEffect ( ( ) => {
177- if ( firstRenderRef . current ) {
178- firstRenderRef . current = false ;
180+ if ( isOnchangeDisabledRef . current ) {
181+ if ( options ?. length > 0 ) {
182+ isOnchangeDisabledRef . current = false ;
183+ }
179184 return ;
180185 }
181186
182- if ( onChange ) {
183- onChange ?.( comboBoxState . selectedItem ?. value ?? null ) ;
184- }
187+ onChange ?.( comboBoxState . selectedItem ?. value ?? null ) ;
185188 } , [ comboBoxState . selectedItem ?. value ] ) ;
186189
187190 return (
You can’t perform that action at this time.
0 commit comments