From 4606a7a4339bbb7044096a0afdb2fd6baef41008 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Chalifour?= Date: Sat, 10 Oct 2020 13:18:49 +0200 Subject: [PATCH] refactor(core): update state reducer signature --- packages/autocomplete-core/src/createStore.ts | 15 +++++--- packages/autocomplete-core/src/onKeyDown.ts | 2 +- .../autocomplete-core/src/stateReducer.ts | 34 ++++++++++--------- packages/autocomplete-core/src/types/store.ts | 9 +++-- 4 files changed, 33 insertions(+), 27 deletions(-) diff --git a/packages/autocomplete-core/src/createStore.ts b/packages/autocomplete-core/src/createStore.ts index 6e52290ed..bffcefde6 100644 --- a/packages/autocomplete-core/src/createStore.ts +++ b/packages/autocomplete-core/src/createStore.ts @@ -10,18 +10,23 @@ export function createStore( reducer: Reducer, props: InternalAutocompleteOptions ): AutocompleteStore { + let state = props.initialState; + return { - state: props.initialState, getState() { - return this.state; + return state; }, send(action, payload) { - this.state = withCompletion( - reducer({ type: action, value: payload }, this.state, props), + state = withCompletion( + reducer(state, { + type: action, + props, + payload, + }), props ); - props.onStateChange({ state: this.state }); + props.onStateChange({ state }); }, }; } diff --git a/packages/autocomplete-core/src/onKeyDown.ts b/packages/autocomplete-core/src/onKeyDown.ts index 74bef2078..95f2c4b30 100644 --- a/packages/autocomplete-core/src/onKeyDown.ts +++ b/packages/autocomplete-core/src/onKeyDown.ts @@ -32,7 +32,7 @@ export function onKeyDown({ // Arrow down. event.preventDefault(); - store.send(event.key, { shiftKey: event.shiftKey }); + store.send(event.key, null); const nodeItem = props.environment.document.getElementById( `${props.id}-item-${store.getState().highlightedIndex}` diff --git a/packages/autocomplete-core/src/stateReducer.ts b/packages/autocomplete-core/src/stateReducer.ts index 10c5a6749..90e3377ae 100644 --- a/packages/autocomplete-core/src/stateReducer.ts +++ b/packages/autocomplete-core/src/stateReducer.ts @@ -1,40 +1,40 @@ import { Reducer } from './types'; import { getItemsCount, getNextHighlightedIndex } from './utils'; -export const stateReducer: Reducer = (action, state, props) => { +export const stateReducer: Reducer = (state, action) => { switch (action.type) { case 'setHighlightedIndex': { return { ...state, - highlightedIndex: action.value, + highlightedIndex: action.payload, }; } case 'setQuery': { return { ...state, - query: action.value, + query: action.payload, }; } case 'setSuggestions': { return { ...state, - suggestions: action.value, + suggestions: action.payload, }; } case 'setIsOpen': { return { ...state, - isOpen: action.value, + isOpen: action.payload, }; } case 'setStatus': { return { ...state, - status: action.value, + status: action.payload, }; } @@ -43,7 +43,7 @@ export const stateReducer: Reducer = (action, state, props) => { ...state, context: { ...state.context, - ...action.value, + ...action.payload, }, }; } @@ -55,7 +55,7 @@ export const stateReducer: Reducer = (action, state, props) => { 1, state.highlightedIndex, getItemsCount(state), - props.defaultHighlightedIndex + action.props.defaultHighlightedIndex ), }; } @@ -67,7 +67,7 @@ export const stateReducer: Reducer = (action, state, props) => { -1, state.highlightedIndex, getItemsCount(state), - props.defaultHighlightedIndex + action.props.defaultHighlightedIndex ), }; } @@ -108,8 +108,10 @@ export const stateReducer: Reducer = (action, state, props) => { // Since we close the menu when openOnFocus=false // we lose track of the highlighted index. (Query-suggestions use-case) - props.openOnFocus === true ? props.defaultHighlightedIndex : null, - isOpen: props.openOnFocus, // @TODO: Check with UX team if we want to close the menu on reset. + action.props.openOnFocus === true + ? action.props.defaultHighlightedIndex + : null, + isOpen: action.props.openOnFocus, // @TODO: Check with UX team if we want to close the menu on reset. status: 'idle', statusContext: {}, query: '', @@ -119,13 +121,13 @@ export const stateReducer: Reducer = (action, state, props) => { case 'focus': { return { ...state, - highlightedIndex: props.defaultHighlightedIndex, - isOpen: props.openOnFocus || state.query.length > 0, + highlightedIndex: action.props.defaultHighlightedIndex, + isOpen: action.props.openOnFocus || state.query.length > 0, }; } case 'blur': { - if (props.debug) { + if (action.props.debug) { return state; } @@ -139,14 +141,14 @@ export const stateReducer: Reducer = (action, state, props) => { case 'mousemove': { return { ...state, - highlightedIndex: action.value, + highlightedIndex: action.payload, }; } case 'mouseleave': { return { ...state, - highlightedIndex: props.defaultHighlightedIndex, + highlightedIndex: action.props.defaultHighlightedIndex, }; } diff --git a/packages/autocomplete-core/src/types/store.ts b/packages/autocomplete-core/src/types/store.ts index e1e82df87..40d6f0469 100644 --- a/packages/autocomplete-core/src/types/store.ts +++ b/packages/autocomplete-core/src/types/store.ts @@ -2,20 +2,19 @@ import { InternalAutocompleteOptions } from './api'; import { AutocompleteState } from './state'; export interface AutocompleteStore { - state: AutocompleteState; getState(): AutocompleteState; send(action: ActionType, payload: any): void; } export type Reducer = ( - action: Action, state: AutocompleteState, - props: InternalAutocompleteOptions + action: Action ) => AutocompleteState; -type Action = { +type Action = { type: ActionType; - value: any; + props: InternalAutocompleteOptions; + payload: TPayload; }; type ActionType =