1 | /// <reference types="react" />
|
2 | /**
|
3 | * Functions that compare fields of a given State object
|
4 | */
|
5 | export type StateComparers<State> = {
|
6 | [key in keyof State]?: (value1: State[key], value2: State[key]) => boolean;
|
7 | };
|
8 | export type StateChangeCallback<State> = <StateKey extends keyof State>(event: React.SyntheticEvent | null, field: StateKey, value: State[StateKey], reason: string, state: State) => void;
|
9 | export type ActionWithContext<Action, ContextValue> = Action & {
|
10 | context: ContextValue;
|
11 | };
|
12 | /**
|
13 | * Parameters of the useControllableReducer hook.
|
14 | */
|
15 | export interface ControllableReducerParameters<State, Action extends ControllableReducerAction, ActionContext = undefined> {
|
16 | /**
|
17 | * The reducer function.
|
18 | *
|
19 | * @param state The previous state. If controlledProps are defined, the state will contain the values of the controlledProps.
|
20 | * @param action The action to dispatch. The action can be augmented with the `actionContext`.
|
21 | * @returns The updated state.
|
22 | */
|
23 | reducer: (state: State, action: ActionWithContext<Action, ActionContext>) => State;
|
24 | /**
|
25 | * The controlled props that will override the state items.
|
26 | */
|
27 | controlledProps?: Partial<State>;
|
28 | /**
|
29 | * The initial state.
|
30 | */
|
31 | initialState: State;
|
32 | /**
|
33 | * Comparers for each state item. If a state item has a corresponding comparer, it will be used to determine if the state has changed.
|
34 | * This is taken into consideration when firing the `onStateChange` event.
|
35 | * If no comparer is defined, the default equality comparison will be used.
|
36 | */
|
37 | stateComparers?: StateComparers<State>;
|
38 | /**
|
39 | * The event handler called whenever a field of the state changes.
|
40 | */
|
41 | onStateChange?: StateChangeCallback<State>;
|
42 | /**
|
43 | * Additional properties that will be added to the action.
|
44 | */
|
45 | actionContext?: ActionContext;
|
46 | /**
|
47 | * The name of the component using useControllableReducer.
|
48 | * For debugging purposes.
|
49 | */
|
50 | componentName?: string;
|
51 | }
|
52 | export type ControllableReducerAction = {
|
53 | /**
|
54 | * The type of the action.
|
55 | */
|
56 | type?: string;
|
57 | /**
|
58 | * The event that triggered the action.
|
59 | */
|
60 | event?: React.SyntheticEvent | null;
|
61 | };
|