Skip to content

Commit f62c084

Browse files
refactor(core): update state reducer signature (#337)
1 parent b130b8e commit f62c084

File tree

4 files changed

+33
-27
lines changed

4 files changed

+33
-27
lines changed

packages/autocomplete-core/src/createStore.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,23 @@ export function createStore<TItem>(
1010
reducer: Reducer,
1111
props: InternalAutocompleteOptions<TItem>
1212
): AutocompleteStore<TItem> {
13+
let state = props.initialState;
14+
1315
return {
14-
state: props.initialState,
1516
getState() {
16-
return this.state;
17+
return state;
1718
},
1819
send(action, payload) {
19-
this.state = withCompletion(
20-
reducer({ type: action, value: payload }, this.state, props),
20+
state = withCompletion(
21+
reducer(state, {
22+
type: action,
23+
props,
24+
payload,
25+
}),
2126
props
2227
);
2328

24-
props.onStateChange({ state: this.state });
29+
props.onStateChange({ state });
2530
},
2631
};
2732
}

packages/autocomplete-core/src/onKeyDown.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export function onKeyDown<TItem>({
3232
// Arrow down.
3333
event.preventDefault();
3434

35-
store.send(event.key, { shiftKey: event.shiftKey });
35+
store.send(event.key, null);
3636

3737
const nodeItem = props.environment.document.getElementById(
3838
`${props.id}-item-${store.getState().highlightedIndex}`

packages/autocomplete-core/src/stateReducer.ts

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,40 @@
11
import { Reducer } from './types';
22
import { getItemsCount, getNextHighlightedIndex } from './utils';
33

4-
export const stateReducer: Reducer = (action, state, props) => {
4+
export const stateReducer: Reducer = (state, action) => {
55
switch (action.type) {
66
case 'setHighlightedIndex': {
77
return {
88
...state,
9-
highlightedIndex: action.value,
9+
highlightedIndex: action.payload,
1010
};
1111
}
1212

1313
case 'setQuery': {
1414
return {
1515
...state,
16-
query: action.value,
16+
query: action.payload,
1717
};
1818
}
1919

2020
case 'setSuggestions': {
2121
return {
2222
...state,
23-
suggestions: action.value,
23+
suggestions: action.payload,
2424
};
2525
}
2626

2727
case 'setIsOpen': {
2828
return {
2929
...state,
30-
isOpen: action.value,
30+
isOpen: action.payload,
3131
};
3232
}
3333

3434
case 'setStatus': {
3535
return {
3636
...state,
37-
status: action.value,
37+
status: action.payload,
3838
};
3939
}
4040

@@ -43,7 +43,7 @@ export const stateReducer: Reducer = (action, state, props) => {
4343
...state,
4444
context: {
4545
...state.context,
46-
...action.value,
46+
...action.payload,
4747
},
4848
};
4949
}
@@ -55,7 +55,7 @@ export const stateReducer: Reducer = (action, state, props) => {
5555
1,
5656
state.highlightedIndex,
5757
getItemsCount(state),
58-
props.defaultHighlightedIndex
58+
action.props.defaultHighlightedIndex
5959
),
6060
};
6161
}
@@ -67,7 +67,7 @@ export const stateReducer: Reducer = (action, state, props) => {
6767
-1,
6868
state.highlightedIndex,
6969
getItemsCount(state),
70-
props.defaultHighlightedIndex
70+
action.props.defaultHighlightedIndex
7171
),
7272
};
7373
}
@@ -108,8 +108,10 @@ export const stateReducer: Reducer = (action, state, props) => {
108108

109109
// Since we close the menu when openOnFocus=false
110110
// we lose track of the highlighted index. (Query-suggestions use-case)
111-
props.openOnFocus === true ? props.defaultHighlightedIndex : null,
112-
isOpen: props.openOnFocus, // @TODO: Check with UX team if we want to close the menu on reset.
111+
action.props.openOnFocus === true
112+
? action.props.defaultHighlightedIndex
113+
: null,
114+
isOpen: action.props.openOnFocus, // @TODO: Check with UX team if we want to close the menu on reset.
113115
status: 'idle',
114116
statusContext: {},
115117
query: '',
@@ -119,13 +121,13 @@ export const stateReducer: Reducer = (action, state, props) => {
119121
case 'focus': {
120122
return {
121123
...state,
122-
highlightedIndex: props.defaultHighlightedIndex,
123-
isOpen: props.openOnFocus || state.query.length > 0,
124+
highlightedIndex: action.props.defaultHighlightedIndex,
125+
isOpen: action.props.openOnFocus || state.query.length > 0,
124126
};
125127
}
126128

127129
case 'blur': {
128-
if (props.debug) {
130+
if (action.props.debug) {
129131
return state;
130132
}
131133

@@ -139,14 +141,14 @@ export const stateReducer: Reducer = (action, state, props) => {
139141
case 'mousemove': {
140142
return {
141143
...state,
142-
highlightedIndex: action.value,
144+
highlightedIndex: action.payload,
143145
};
144146
}
145147

146148
case 'mouseleave': {
147149
return {
148150
...state,
149-
highlightedIndex: props.defaultHighlightedIndex,
151+
highlightedIndex: action.props.defaultHighlightedIndex,
150152
};
151153
}
152154

packages/autocomplete-core/src/types/store.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,19 @@ import { InternalAutocompleteOptions } from './api';
22
import { AutocompleteState } from './state';
33

44
export interface AutocompleteStore<TItem> {
5-
state: AutocompleteState<TItem>;
65
getState(): AutocompleteState<TItem>;
76
send(action: ActionType, payload: any): void;
87
}
98

109
export type Reducer = <TItem>(
11-
action: Action,
1210
state: AutocompleteState<TItem>,
13-
props: InternalAutocompleteOptions<TItem>
11+
action: Action<TItem, any>
1412
) => AutocompleteState<TItem>;
1513

16-
type Action = {
14+
type Action<TItem, TPayload> = {
1715
type: ActionType;
18-
value: any;
16+
props: InternalAutocompleteOptions<TItem>;
17+
payload: TPayload;
1918
};
2019

2120
type ActionType =

0 commit comments

Comments
 (0)