1 | import _extends from "@babel/runtime/helpers/esm/extends";
|
2 | import * as React from 'react';
|
3 | function areEqual(a, b) {
|
4 | return a === b;
|
5 | }
|
6 | var EMPTY_OBJECT = {};
|
7 | var NOOP = function NOOP() {};
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 | function getControlledState(internalState, controlledProps) {
|
14 | var augmentedState = _extends({}, internalState);
|
15 | Object.keys(controlledProps).forEach(function (key) {
|
16 | if (controlledProps[key] !== undefined) {
|
17 | augmentedState[key] = controlledProps[key];
|
18 | }
|
19 | });
|
20 | return augmentedState;
|
21 | }
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 | function useStateChangeDetection(parameters) {
|
28 | var nextState = parameters.nextState,
|
29 | initialState = parameters.initialState,
|
30 | stateComparers = parameters.stateComparers,
|
31 | onStateChange = parameters.onStateChange,
|
32 | controlledProps = parameters.controlledProps,
|
33 | lastActionRef = parameters.lastActionRef;
|
34 | var internalPreviousStateRef = React.useRef(initialState);
|
35 | React.useEffect(function () {
|
36 | if (lastActionRef.current === null) {
|
37 |
|
38 | return;
|
39 | }
|
40 | var previousState = getControlledState(internalPreviousStateRef.current, controlledProps);
|
41 | Object.keys(nextState).forEach(function (key) {
|
42 | var _stateComparers$key;
|
43 |
|
44 | var stateComparer = (_stateComparers$key = stateComparers[key]) != null ? _stateComparers$key : areEqual;
|
45 | var nextStateItem = nextState[key];
|
46 | var previousStateItem = previousState[key];
|
47 | if (!stateComparer(nextStateItem, previousStateItem)) {
|
48 | var _event, _type;
|
49 | onStateChange == null ? void 0 : onStateChange((_event = lastActionRef.current.event) != null ? _event : null, key, nextStateItem, (_type = lastActionRef.current.type) != null ? _type : '', nextState);
|
50 | }
|
51 | });
|
52 | internalPreviousStateRef.current = nextState;
|
53 | lastActionRef.current = null;
|
54 | }, [internalPreviousStateRef, nextState, lastActionRef, onStateChange, stateComparers, controlledProps]);
|
55 | }
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 | export default function useControllableReducer(parameters) {
|
82 | var lastActionRef = React.useRef(null);
|
83 | var reducer = parameters.reducer,
|
84 | initialState = parameters.initialState,
|
85 | _parameters$controlle = parameters.controlledProps,
|
86 | controlledProps = _parameters$controlle === void 0 ? EMPTY_OBJECT : _parameters$controlle,
|
87 | _parameters$stateComp = parameters.stateComparers,
|
88 | stateComparers = _parameters$stateComp === void 0 ? EMPTY_OBJECT : _parameters$stateComp,
|
89 | _parameters$onStateCh = parameters.onStateChange,
|
90 | onStateChange = _parameters$onStateCh === void 0 ? NOOP : _parameters$onStateCh,
|
91 | actionContext = parameters.actionContext;
|
92 | var reducerWithControlledState = React.useCallback(function (state, action) {
|
93 | lastActionRef.current = action;
|
94 | var controlledState = getControlledState(state, controlledProps);
|
95 | return reducer(controlledState, action);
|
96 | }, [controlledProps, reducer]);
|
97 | var _React$useReducer = React.useReducer(reducerWithControlledState, initialState),
|
98 | nextState = _React$useReducer[0],
|
99 | dispatch = _React$useReducer[1];
|
100 | var dispatchWithContext = React.useCallback(function (action) {
|
101 | dispatch(_extends({}, action, {
|
102 | context: actionContext
|
103 | }));
|
104 | }, [actionContext]);
|
105 | useStateChangeDetection({
|
106 | nextState: nextState,
|
107 | initialState: initialState,
|
108 | stateComparers: stateComparers != null ? stateComparers : EMPTY_OBJECT,
|
109 | onStateChange: onStateChange != null ? onStateChange : NOOP,
|
110 | controlledProps: controlledProps,
|
111 | lastActionRef: lastActionRef
|
112 | });
|
113 | return [getControlledState(nextState, controlledProps), dispatchWithContext];
|
114 | } |
\ | No newline at end of file |