From 21739b446dd7d5105dabb67adfff0937cbe06162 Mon Sep 17 00:00:00 2001 From: Eunjae Lee Date: Mon, 12 Oct 2020 14:33:26 +0200 Subject: [PATCH] feat(onStateChange): provide prevState (#335) --- packages/autocomplete-core/src/createStore.ts | 3 ++- packages/autocomplete-core/src/types/api.ts | 10 ++++++++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/packages/autocomplete-core/src/createStore.ts b/packages/autocomplete-core/src/createStore.ts index e2330b161..f09d6415c 100644 --- a/packages/autocomplete-core/src/createStore.ts +++ b/packages/autocomplete-core/src/createStore.ts @@ -25,6 +25,7 @@ export function createStore( return state; }, send(action, payload) { + const prevState = { ...state }; state = enhanceState( reducer(state, { type: action, @@ -33,7 +34,7 @@ export function createStore( }) ); - props.onStateChange({ state }); + props.onStateChange({ state, prevState }); }, }; } diff --git a/packages/autocomplete-core/src/types/api.ts b/packages/autocomplete-core/src/types/api.ts index 2dd98d828..487e7876d 100644 --- a/packages/autocomplete-core/src/types/api.ts +++ b/packages/autocomplete-core/src/types/api.ts @@ -198,7 +198,10 @@ export interface AutocompleteOptions { /** * Function called when the internal state changes. */ - onStateChange?(props: { state: AutocompleteState }): void; + onStateChange?(props: { + state: AutocompleteState; + prevState: AutocompleteState; + }): void; /** * The text that appears in the search box input when there is no query. */ @@ -282,7 +285,10 @@ export interface InternalAutocompleteOptions extends AutocompleteOptions { debug: boolean; id: string; - onStateChange(props: { state: AutocompleteState }): void; + onStateChange(props: { + state: AutocompleteState; + prevState: AutocompleteState; + }): void; placeholder: string; autoFocus: boolean; defaultHighlightedIndex: number | null;