1 | import _extends from "@babel/runtime/helpers/esm/extends";
|
2 | import { ListActionTypes, listReducer } from '../useList';
|
3 | export 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 |
|
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 |
|
29 |
|
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 |