Skip to content

Commit a07b91e

Browse files
[select] Consider space as part of the search query (@silviuaavram) (#49946)
Co-authored-by: Silviu Alexandru Avram <silviu.avram91@gmail.com>
1 parent 0f58cc5 commit a07b91e

5 files changed

Lines changed: 422 additions & 64 deletions

File tree

‎packages/mui-material/src/Menu/Menu.js‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ const Menu = React.forwardRef(function Menu(inProps, ref) {
6969
children,
7070
className,
7171
disableAutoFocusItem = false,
72+
// Private callback for standalone Space on a Select option.
73+
// eslint-disable-next-line react/prop-types
74+
internalOnOptionSpaceKeyDown,
7275
onClose,
7376
open,
7477
PopoverClasses,
@@ -148,6 +151,10 @@ const Menu = React.forwardRef(function Menu(inProps, ref) {
148151
onKeyDown: (event) => {
149152
handleListKeyDown(event);
150153
handlers.onKeyDown?.(event);
154+
// Run Select's fallback last so both typeahead and the list slot's onKeyDown can consume Space.
155+
if (event.key === ' ' && !event.defaultPrevented) {
156+
internalOnOptionSpaceKeyDown?.(event);
157+
}
151158
},
152159
}),
153160
ownerState,

‎packages/mui-material/src/MenuList/MenuList.js‎

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,13 @@ const MenuList = React.forwardRef(function MenuList(inProps, ref) {
244244
[focusInitialTarget],
245245
);
246246

247-
const rovingContainerProps = getContainerProps(undefined, other.onFocus);
247+
const rovingContainerProps = getContainerProps(undefined, (event) => {
248+
// Reopening a kept-mounted menu starts a fresh query. Moving focus between items continues it.
249+
if (!contains(event.currentTarget, event.relatedTarget)) {
250+
textCriteriaRef.current.keys = [];
251+
}
252+
other.onFocus?.(event);
253+
});
248254
const handleRef = useForkRef(listRef, rovingContainerProps.ref, ref);
249255
const menuListContextValue = React.useMemo(
250256
() => ({
@@ -268,22 +274,27 @@ const MenuList = React.forwardRef(function MenuList(inProps, ref) {
268274
return;
269275
}
270276

271-
rovingContainerProps.onKeyDown(event);
277+
const criteria = textCriteriaRef.current;
278+
if (!event.defaultPrevented) {
279+
rovingContainerProps.onKeyDown(event);
280+
// Handling ArrowUp, ArrowDown, Home, or End ends the current typeahead query.
281+
if (event.defaultPrevented) {
282+
criteria.keys = [];
283+
}
284+
}
272285

273-
if (event.key.length === 1) {
274-
const criteria = textCriteriaRef.current;
286+
const currTime = performance.now();
287+
const hasActiveQuery = criteria.keys.length > 0 && currTime - criteria.lastTime <= 500;
288+
// Leave a standalone Space to the list's keydown handler for option activation.
289+
if (event.key.length === 1 && (event.key !== ' ' || hasActiveQuery)) {
275290
const lowerKey = event.key.toLowerCase();
276-
const currTime = performance.now();
277-
278-
if (criteria.keys.length > 0) {
279-
// Reset
280-
if (currTime - criteria.lastTime > 500) {
281-
criteria.keys = [];
282-
criteria.repeating = true;
283-
criteria.previousKeyMatched = true;
284-
} else if (criteria.repeating && lowerKey !== criteria.keys[0]) {
285-
criteria.repeating = false;
286-
}
291+
292+
if (!hasActiveQuery) {
293+
criteria.keys = [];
294+
criteria.repeating = true;
295+
criteria.previousKeyMatched = true;
296+
} else if (criteria.repeating && lowerKey !== criteria.keys[0]) {
297+
criteria.repeating = false;
287298
}
288299

289300
criteria.lastTime = currTime;
@@ -293,14 +304,14 @@ const MenuList = React.forwardRef(function MenuList(inProps, ref) {
293304
const keepFocusOnCurrent =
294305
currentFocus && !criteria.repeating && textCriteriaMatches(currentFocus, criteria);
295306

296-
if (
307+
criteria.previousKeyMatched =
297308
criteria.previousKeyMatched &&
298309
(keepFocusOnCurrent ||
299-
focusNext((item) => isItemFocusableWithTextCriteria(item, criteria)) != null)
300-
) {
310+
focusNext((item) => isItemFocusableWithTextCriteria(item, criteria)) != null);
311+
312+
// A space in the query belongs to typeahead even when the prefix has no match.
313+
if (criteria.previousKeyMatched || event.key === ' ') {
301314
event.preventDefault();
302-
} else {
303-
criteria.previousKeyMatched = false;
304315
}
305316
}
306317

0 commit comments

Comments
 (0)