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