UNPKG

2.23 kBJavaScriptView Raw
1import _extends from "@babel/runtime/helpers/esm/extends";
2import { moveHighlight, listReducer, ListActionTypes } from '../useList';
3import { SelectActionTypes } from './useSelect.types';
4export default function selectReducer(state, action) {
5 var open = state.open;
6 var selectionMode = action.context.selectionMode;
7 if (action.type === SelectActionTypes.buttonClick) {
8 var _state$selectedValues;
9 var itemToHighlight = (_state$selectedValues = state.selectedValues[0]) != null ? _state$selectedValues : moveHighlight(null, 'start', action.context);
10 return _extends({}, state, {
11 open: !open,
12 highlightedValue: !open ? itemToHighlight : null
13 });
14 }
15 var newState = listReducer(state, action);
16 switch (action.type) {
17 case ListActionTypes.keyDown:
18 if (state.open) {
19 if (action.event.key === 'Escape') {
20 return _extends({}, newState, {
21 open: false
22 });
23 }
24 if (selectionMode === 'single' && (action.event.key === 'Enter' || action.event.key === ' ')) {
25 return _extends({}, newState, {
26 open: false
27 });
28 }
29 } else {
30 if (action.event.key === 'Enter' || action.event.key === ' ' || action.event.key === 'ArrowDown') {
31 var _state$selectedValues2;
32 return _extends({}, state, {
33 open: true,
34 highlightedValue: (_state$selectedValues2 = state.selectedValues[0]) != null ? _state$selectedValues2 : moveHighlight(null, 'start', action.context)
35 });
36 }
37 if (action.event.key === 'ArrowUp') {
38 var _state$selectedValues3;
39 return _extends({}, state, {
40 open: true,
41 highlightedValue: (_state$selectedValues3 = state.selectedValues[0]) != null ? _state$selectedValues3 : moveHighlight(null, 'end', action.context)
42 });
43 }
44 }
45 break;
46 case ListActionTypes.itemClick:
47 if (selectionMode === 'single') {
48 return _extends({}, newState, {
49 open: false
50 });
51 }
52 break;
53 case ListActionTypes.blur:
54 return _extends({}, newState, {
55 open: false
56 });
57 default:
58 return newState;
59 }
60 return newState;
61}
\No newline at end of file