Autocomplete: Refactor useAutocomplete to use useReducer#77020
Autocomplete: Refactor useAutocomplete to use useReducer#77020
Conversation
Consolidates four related useState calls (selectedIndex, filteredOptions, filterValue, autocompleter) into a single useReducer, making state transitions atomic. Derives AutocompleterUI via useMemo instead of storing it as separate state, since it's purely a function of autocompleter. Moves new types to the shared types file.
| const AutocompleterUI = useMemo( | ||
| () => ( autocompleter ? getAutoCompleterUI( autocompleter ) : null ), | ||
| [ autocompleter ] |
There was a problem hiding this comment.
I think we can improve this further and get rid of the component factory, but it's a large refactoring on its own, so we'll handle it separately.
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message. To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
| case 'Escape': | ||
| setAutocompleter( null ); | ||
| setAutocompleterUI( null ); | ||
| dispatch( { type: 'RESET' } ); |
There was a problem hiding this comment.
Calling the RESET seems like a better approach here, and I couldn't spot any regressions.
|
Size Change: +124 B (0%) Total Size: 7.74 MB
ℹ️ View Unchanged
|
|
Flaky tests detected in c95c891. 🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/23938399425
|
What?
PR contains the following code quality improvements for the
useAutocompletehook:useStatecalls with a singleuseReducerto simplify related state updates.AutocompleterUIcomponent viauseMemoinstead of storing it in state, since it's just a function ofautocompleter. This removes the awkwardsetState(() => component)workaround needed to store a function in state.ArrowUp/ArrowDownkey handlers — they only differed in index calculation, now unified with modular arithmetic.getCompletionObject.Testing Instructions
All CI checks should pass. Autocomplete has good e2e test coverage.
Use of AI Tools
Used Claude to help me with type definitions.