Follow up #8001 by deriving Autocomplete and LabelGroup state during render#8005
Follow up #8001 by deriving Autocomplete and LabelGroup state during render#8005Copilot wants to merge 3 commits into
Conversation
|
Co-authored-by: mattcosta7 <8616962+mattcosta7@users.noreply.github.com>
Co-authored-by: mattcosta7 <8616962+mattcosta7@users.noreply.github.com>
|
There was a problem hiding this comment.
Pull request overview
This PR is a follow-up refactor to #8001 that narrows two previously “splittable later” effect sites by deriving state during render for derivable transitions (while keeping real side effects in effects). The focus is Autocomplete close-time reordering and LabelGroup numeric truncation.
Changes:
- AutocompleteMenu: Track
showMenutransitions during render and trigger close-time re-sorting on the open→closed transition; keeponOpenChangein an effect. - LabelGroup: For numeric
visibleChildCount, derive the truncationvisibilityMapfrom child indices (not committed DOM) and add a shallow equality guard; keep'auto'truncation effect-driven viaIntersectionObserver. - Tests: Strengthen coverage for
onOpenChange(false → true → false)and add rerender coverage for numeric LabelGroup truncation updates.
Show a summary per file
| File | Description |
|---|---|
| packages/react/src/LabelGroup/LabelGroup.tsx | Derives numeric truncation visibility from child indices and updates render path to use a cached childArray. |
| packages/react/src/LabelGroup/LabelGroup.test.tsx | Adds rerender assertions to ensure numeric truncation updates correctly when visibleChildCount changes. |
| packages/react/src/Autocomplete/AutocompleteMenu.tsx | Moves close-time sorting to a render-time transition check while keeping onOpenChange effect-driven. |
| packages/react/src/Autocomplete/Autocomplete.test.tsx | Tightens onOpenChange assertions to validate open/close sequencing. |
Copilot's findings
- Files reviewed: 4/4 changed files
- Comments generated: 2
| const itemIdSortResult = [...sortedItemIds].sort( | ||
| sortOnCloseFn ? sortOnCloseFn : getDefaultSortFn(itemId => isItemSelected(itemId, selectedItemIds)), | ||
| ) | ||
| const sortResultMatchesState = | ||
| itemIdSortResult.length === sortedItemIds.length && |
| const numericVisibilityMap = React.useMemo( | ||
| () => | ||
| typeof visibleChildCount === 'number' && !isOverflowShown | ||
| ? getVisibilityMapAfterIndex(childArray.length, visibleChildCount) | ||
| : undefined, | ||
| [childArray.length, isOverflowShown, visibleChildCount], |
Follow-up to #8001 for the two explicitly deferred “splittable later” sites only. This narrows two mixed-responsibility effects so derivable state updates happen during render, while real side effects remain in effects.
Changelog
New
Changed
AutocompleteMenuonOpenChange.showMenu: true -> falsetransition using the same previous-value pattern already used inSelectPanel.onOpenChangeremains inuseEffectas the actual side effect.LabelGroupvisibleChildCounttruncation now derivesvisibilityMapfrom child indices instead of reading committed DOM state.'auto'truncation remains effect-driven viaIntersectionObserver.Tests
Autocompletecoverage to assertonOpenChange(false -> true -> false)across open/close.LabelGrouprerender coverage for numeric truncation updates.Example of the render-time transition pattern used here:
Removed
LabelGroup.set-state-in-effect/ derived-state suppression around Autocomplete close-time sorting.Rollout strategy
Behavior-preserving internal refactor only; no API or visual change intended.
Testing & Reviewing
Review by change site:
Autocomplete
onOpenChangestill fires with the current open state and remains effect-driven.LabelGroup
visibleChildCounttruncation updates correctly across rerenders.'auto'truncation, inline expand/collapse, and overlay overflow behavior are unchanged.Validated with:
npm test -- --run packages/react/src/Autocomplete/Autocomplete.test.tsx packages/react/src/LabelGroup/LabelGroup.test.tsxnpx eslint --max-warnings=0 packages/react/src/Autocomplete/AutocompleteMenu.tsx packages/react/src/LabelGroup/LabelGroup.tsx packages/react/src/Autocomplete/Autocomplete.test.tsx packages/react/src/LabelGroup/LabelGroup.test.tsxnpm run type-check -w @primer/reactnpm run buildnpm testnpm run type-checknpm run lintnpm run lint:cssnpm run format:diffMerge checklist