UNPKG

1.75 kBJavaScriptView Raw
1import _extends from "@babel/runtime/helpers/esm/extends";
2import { ListActionTypes, listReducer } from '../useList';
3export function menuReducer(state, action) {
4 if (action.type === ListActionTypes.itemHover) {
5 return _extends({}, state, {
6 highlightedValue: action.item
7 });
8 }
9 const newState = listReducer(state, action);
10
11 // make sure an item is always highlighted
12 if (newState.highlightedValue === null && action.context.items.length > 0) {
13 return _extends({}, newState, {
14 highlightedValue: action.context.items[0]
15 });
16 }
17 if (action.type === ListActionTypes.keyDown) {
18 if (action.event.key === 'Escape') {
19 return _extends({}, newState, {
20 open: false
21 });
22 }
23 }
24 if (action.type === ListActionTypes.blur) {
25 var _action$context$listb;
26 if (!((_action$context$listb = action.context.listboxRef.current) != null && _action$context$listb.contains(action.event.relatedTarget))) {
27 var _action$context$listb2, _action$event$related;
28 // To prevent the menu from closing when the focus leaves the menu to the button.
29 // For more details, see https://github.com/mui/material-ui/pull/36917#issuecomment-1566992698
30 const listboxId = (_action$context$listb2 = action.context.listboxRef.current) == null ? void 0 : _action$context$listb2.getAttribute('id');
31 const controlledBy = (_action$event$related = action.event.relatedTarget) == null ? void 0 : _action$event$related.getAttribute('aria-controls');
32 if (listboxId && controlledBy && listboxId === controlledBy) {
33 return newState;
34 }
35 return _extends({}, newState, {
36 open: false,
37 highlightedValue: action.context.items[0]
38 });
39 }
40 }
41 return newState;
42}
\No newline at end of file