UNPKG

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