[select] Support typeahead when closed#48563
Merged
Merged
Conversation
Deploy previewhttps://deploy-preview-48563--material-ui.netlify.app/ Bundle size
Check out the code infra dashboard for more information about this PR. |
a12cc41 to
63fc101
Compare
63fc101 to
6c489c3
Compare
silviuaavram
approved these changes
Jun 1, 2026
Comment on lines
+28
to
+97
| const TYPEAHEAD_RESET_MS = 750; | ||
| const SPACE = ' '; | ||
| const ARROW_UP = 'ArrowUp'; | ||
| const ARROW_DOWN = 'ArrowDown'; | ||
| const ENTER = 'Enter'; | ||
|
|
||
| function hasOwnValueProp(child) { | ||
| return Object.prototype.hasOwnProperty.call(child.props, 'value'); | ||
| } | ||
|
|
||
| function getTextFromReactNode(node) { | ||
| if (typeof node === 'string' || typeof node === 'number') { | ||
| return String(node); | ||
| } | ||
|
|
||
| let text = ''; | ||
|
|
||
| React.Children.forEach(node, (child) => { | ||
| if (typeof child === 'string' || typeof child === 'number') { | ||
| text += String(child); | ||
| } else if (React.isValidElement(child)) { | ||
| text += getTextFromReactNode(child.props.children); | ||
| } | ||
| }); | ||
|
|
||
| return text; | ||
| } | ||
|
|
||
| function getMatchingOptionIndex(options, search, startIndex = 0) { | ||
| if (options.length === 0) { | ||
| return -1; | ||
| } | ||
|
|
||
| const normalizedStartIndex = ((startIndex % options.length) + options.length) % options.length; | ||
|
|
||
| for (let offset = 0; offset < options.length; offset += 1) { | ||
| const index = (normalizedStartIndex + offset) % options.length; | ||
|
|
||
| if (options[index].label.startsWith(search)) { | ||
| return index; | ||
| } | ||
| } | ||
|
|
||
| return -1; | ||
| } | ||
|
|
||
| function canCycleRepeatedCharacter(options, key) { | ||
| return !options.some((option) => option.label[0] === key && option.label[1] === key); | ||
| } | ||
|
|
||
| function getTypeaheadOptions(childrenArray, value) { | ||
| const options = []; | ||
| let selectedIndex = -1; | ||
|
|
||
| for (let index = 0; index < childrenArray.length; index += 1) { | ||
| const child = childrenArray[index]; | ||
|
|
||
| if (!React.isValidElement(child) || !hasOwnValueProp(child) || child.props.disabled) { | ||
| continue; | ||
| } | ||
|
|
||
| // Closed typeahead cannot exclude CSS-hidden text because no option DOM is mounted. | ||
| const label = getTextFromReactNode(child.props.children).trim().toLowerCase(); | ||
|
|
||
| if (label === '') { | ||
| continue; | ||
| } | ||
|
|
||
| if (selectedIndex === -1 && areEqualValues(value, child.props.value)) { | ||
| selectedIndex = options.length; |
Member
There was a problem hiding this comment.
Should we add all of these in the local utils folder?
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Preview: https://deploy-preview-48563--material-ui.netlify.app/material-ui/react-select/
Closes #48467