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