-
-
Notifications
You must be signed in to change notification settings - Fork 484
fix: option position announcement for virtual mode #1226
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Pareder
wants to merge
1
commit into
react-component:master
Choose a base branch
from
Pareder:fix/option-position
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+195
−35
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -275,6 +275,12 @@ const OptionList: React.ForwardRefRenderFunction<RefOptionListProps, {}> = (_, r | |
|
|
||
| const getLabel = (item: Record<string, any>) => item.label; | ||
|
|
||
| // Skip group headers to match native <select> announcements with <optgroup> | ||
| const optionCount = memoFlattenOptions.filter((option) => !option.group).length; | ||
|
|
||
| const getOptionPosition = (index: number) => | ||
| memoFlattenOptions.slice(0, index + 1).filter((option) => !option.group).length; | ||
|
|
||
| function getItemAriaProps(item: FlattenOptionData<BaseOptionType>, index: number) { | ||
| const { group } = item; | ||
|
|
||
|
|
@@ -291,12 +297,13 @@ const OptionList: React.ForwardRefRenderFunction<RefOptionListProps, {}> = (_, r | |
| } | ||
| const itemData = item.data || {}; | ||
| const { value, disabled } = itemData; | ||
| const { group } = item; | ||
| const attrs = pickAttrs(itemData, true); | ||
| const mergedLabel = getLabel(item); | ||
| return item ? ( | ||
| <div | ||
| aria-label={typeof mergedLabel === 'string' && !group ? mergedLabel : null} | ||
| aria-label={typeof mergedLabel === 'string' ? mergedLabel : null} | ||
| aria-setsize={optionCount} | ||
| aria-posinset={getOptionPosition(index)} | ||
|
Comment on lines
+304
to
+306
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 建议将数字标签也映射到 当前仅在 💡 建议修改- aria-label={typeof mergedLabel === 'string' ? mergedLabel : null}
+ aria-label={isTitleType(mergedLabel) ? String(mergedLabel) : null}
...
- aria-label={typeof groupLabel === 'string' ? groupLabel : null}
+ aria-label={isTitleType(groupLabel) ? String(groupLabel) : null}Also applies to: 361-362 🤖 Prompt for AI Agents |
||
| {...attrs} | ||
| key={index} | ||
| {...getItemAriaProps(item, index)} | ||
|
|
@@ -308,6 +315,57 @@ const OptionList: React.ForwardRefRenderFunction<RefOptionListProps, {}> = (_, r | |
| ) : null; | ||
| }; | ||
|
|
||
| const getGroupItem = (index: number) => { | ||
| for (let i = index; i >= 0; i -= 1) { | ||
| const current = memoFlattenOptions[i]; | ||
| if (current?.group) { | ||
| return current; | ||
| } | ||
| } | ||
| return null; | ||
| }; | ||
|
|
||
| // Nest options inside `role="group"` wrappers | ||
| const renderHiddenItems = () => { | ||
| const segments: { | ||
| group: FlattenOptionData<BaseOptionType> | null; | ||
| indexes: number[]; | ||
| }[] = []; | ||
|
|
||
| [activeIndex - 1, activeIndex, activeIndex + 1].forEach((index) => { | ||
| const item = memoFlattenOptions[index]; | ||
| if (!item || item.group) { | ||
| return; | ||
| } | ||
|
|
||
| const groupItem = item.groupOption ? getGroupItem(index) : null; | ||
| const lastSegment = segments[segments.length - 1]; | ||
|
|
||
| if (lastSegment && lastSegment.group === groupItem) { | ||
| lastSegment.indexes.push(index); | ||
| } else { | ||
| segments.push({ group: groupItem, indexes: [index] }); | ||
| } | ||
| }); | ||
|
|
||
| return segments.map(({ group, indexes }) => { | ||
| if (!group) { | ||
| return indexes.map(renderItem); | ||
| } | ||
|
|
||
| const groupLabel = getLabel(group); | ||
| return ( | ||
| <div | ||
| key={group.key} | ||
| role="group" | ||
| aria-label={typeof groupLabel === 'string' ? groupLabel : null} | ||
| > | ||
| {indexes.map(renderItem)} | ||
| </div> | ||
| ); | ||
| }); | ||
| }; | ||
|
|
||
| const a11yProps = { | ||
| role: 'listbox', | ||
| id: `${id}_list`, | ||
|
|
@@ -317,9 +375,7 @@ const OptionList: React.ForwardRefRenderFunction<RefOptionListProps, {}> = (_, r | |
| <> | ||
| {virtual && ( | ||
| <div {...a11yProps} style={{ height: 0, width: 0, overflow: 'hidden' }}> | ||
| {renderItem(activeIndex - 1)} | ||
| {renderItem(activeIndex)} | ||
| {renderItem(activeIndex + 1)} | ||
| {renderHiddenItems()} | ||
| </div> | ||
| )} | ||
| <List<FlattenOptionData<BaseOptionType>> | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Calculating
optionCountandgetOptionPositionon every render performs array filtering and slicing operations ($O(N)$) over the entirememoFlattenOptionslist. SincegetOptionPositionis called up to three times per render (foractiveIndex - 1,activeIndex, andactiveIndex + 1), and renders occur on every keyboard navigation step (asactiveIndexchanges), this can cause noticeable lag/performance degradation in large virtualized lists.Since$O(N)$ pass using $O(1)$ and avoids redundant array allocations and filtering on every render.
memoFlattenOptionsis already memoized, we can precompute the option count and positions map in a singleReact.useMemo. This reduces the lookup ingetOptionPositionto