@aws-amplify/ui-react-core 3.0.30 → 3.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/elements.js +116 -3
- package/dist/esm/elements/ControlsContext.mjs +71 -0
- package/dist/esm/elements/ElementsContext.mjs +3 -2
- package/dist/esm/elements/defineBaseElement.mjs +30 -1
- package/dist/esm/elements/utils.mjs +11 -0
- package/dist/esm/elements/withBaseElementProps.mjs +1 -1
- package/dist/esm/elements.mjs +3 -1
- package/dist/esm/hooks/useDataState.mjs +10 -4
- package/dist/esm/utils/createContextUtilities.mjs +6 -3
- package/dist/index.js +15 -7
- package/dist/types/elements/ControlsContext.d.ts +63 -0
- package/dist/types/elements/ElementsContext.d.ts +3 -2
- package/dist/types/elements/defineBaseElement.d.ts +19 -2
- package/dist/types/elements/index.d.ts +4 -1
- package/dist/types/elements/types.d.ts +88 -6
- package/dist/types/elements/utils.d.ts +3 -0
- package/dist/types/elements/withBaseElementProps.d.ts +3 -3
- package/dist/types/hooks/index.d.ts +1 -1
- package/dist/types/hooks/useDataState.d.ts +6 -1
- package/dist/types/index.d.ts +1 -1
- package/dist/types/utils/createContextUtilities.d.ts +1 -1
- package/package.json +3 -3
- package/src/elements/ControlsContext.tsx +89 -0
- package/src/elements/ElementsContext.tsx +3 -2
- package/src/elements/defineBaseElement.tsx +50 -2
- package/src/elements/index.ts +7 -1
- package/src/elements/types.ts +114 -6
- package/src/elements/utils.ts +20 -0
- package/src/elements/withBaseElementProps.tsx +3 -3
- package/src/hooks/index.ts +6 -1
- package/src/hooks/useDataState.ts +25 -7
- package/src/index.ts +2 -0
- package/src/utils/createContextUtilities.tsx +9 -6
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { isFunction } from '@aws-amplify/ui';
|
|
1
2
|
import React from 'react';
|
|
2
3
|
|
|
3
4
|
export interface DataState<T> {
|
|
@@ -7,6 +8,13 @@ export interface DataState<T> {
|
|
|
7
8
|
message: string | undefined;
|
|
8
9
|
}
|
|
9
10
|
|
|
11
|
+
export type DataAction<T = any, K = any> = (prevData: T, input: K) => T;
|
|
12
|
+
|
|
13
|
+
export type AsyncDataAction<T = any, K = any> = (
|
|
14
|
+
prevData: T,
|
|
15
|
+
input: K
|
|
16
|
+
) => Promise<T>;
|
|
17
|
+
|
|
10
18
|
// default state
|
|
11
19
|
const INITIAL_STATE = { hasError: false, isLoading: false, message: undefined };
|
|
12
20
|
const LOADING_STATE = { hasError: false, isLoading: true, message: undefined };
|
|
@@ -20,9 +28,13 @@ const resolveMaybeAsync = async <T>(
|
|
|
20
28
|
};
|
|
21
29
|
|
|
22
30
|
export default function useDataState<T, K>(
|
|
23
|
-
action:
|
|
24
|
-
initialData: T
|
|
25
|
-
|
|
31
|
+
action: DataAction<T, K> | AsyncDataAction<T, K>,
|
|
32
|
+
initialData: T,
|
|
33
|
+
options?: {
|
|
34
|
+
onSuccess?: (data: T) => void;
|
|
35
|
+
onError?: (message: string) => void;
|
|
36
|
+
}
|
|
37
|
+
): [state: DataState<T>, handleAction: (input: K) => void] {
|
|
26
38
|
const [dataState, setDataState] = React.useState<DataState<T>>(() => ({
|
|
27
39
|
...INITIAL_STATE,
|
|
28
40
|
data: initialData,
|
|
@@ -30,20 +42,26 @@ export default function useDataState<T, K>(
|
|
|
30
42
|
|
|
31
43
|
const prevData = React.useRef(initialData);
|
|
32
44
|
|
|
33
|
-
const
|
|
34
|
-
|
|
45
|
+
const { onSuccess, onError } = options ?? {};
|
|
46
|
+
|
|
47
|
+
const handleAction: (input: K) => void = React.useCallback(
|
|
48
|
+
(input) => {
|
|
35
49
|
setDataState(({ data }) => ({ ...LOADING_STATE, data }));
|
|
36
50
|
|
|
37
|
-
resolveMaybeAsync(action(prevData.current,
|
|
51
|
+
resolveMaybeAsync(action(prevData.current, input))
|
|
38
52
|
.then((data: T) => {
|
|
53
|
+
if (isFunction(onSuccess)) onSuccess(data);
|
|
54
|
+
|
|
39
55
|
prevData.current = data;
|
|
40
56
|
setDataState({ ...INITIAL_STATE, data });
|
|
41
57
|
})
|
|
42
58
|
.catch(({ message }: Error) => {
|
|
59
|
+
if (isFunction(onError)) onError(message);
|
|
60
|
+
|
|
43
61
|
setDataState(({ data }) => ({ ...ERROR_STATE, data, message }));
|
|
44
62
|
});
|
|
45
63
|
},
|
|
46
|
-
[action]
|
|
64
|
+
[action, onError, onSuccess]
|
|
47
65
|
);
|
|
48
66
|
|
|
49
67
|
return [dataState, handleAction];
|
package/src/index.ts
CHANGED
|
@@ -36,7 +36,7 @@ type CreateContextUtilitiesReturn<ContextType, ContextName extends string> = {
|
|
|
36
36
|
: Key extends `use${string}`
|
|
37
37
|
? (params?: HookParams) => ContextType
|
|
38
38
|
: Key extends `${string}Context`
|
|
39
|
-
? React.Context<ContextType
|
|
39
|
+
? React.Context<ContextType>
|
|
40
40
|
: never;
|
|
41
41
|
};
|
|
42
42
|
|
|
@@ -89,7 +89,7 @@ type CreateContextUtilitiesReturn<ContextType, ContextName extends string> = {
|
|
|
89
89
|
export default function createContextUtilities<
|
|
90
90
|
ContextType,
|
|
91
91
|
ContextName extends string = string,
|
|
92
|
-
Message extends string | undefined = string | undefined
|
|
92
|
+
Message extends string | undefined = string | undefined,
|
|
93
93
|
>(
|
|
94
94
|
options: ContextOptions<ContextType, ContextName, Message>
|
|
95
95
|
): CreateContextUtilitiesReturn<ContextType, ContextName> {
|
|
@@ -99,7 +99,11 @@ export default function createContextUtilities<
|
|
|
99
99
|
throw new Error(INVALID_OPTIONS_MESSAGE);
|
|
100
100
|
}
|
|
101
101
|
|
|
102
|
+
const contextDisplayName = `${contextName}Context`;
|
|
103
|
+
const providerDisplayName = `${contextName}Provider`;
|
|
104
|
+
|
|
102
105
|
const Context = React.createContext<ContextType | undefined>(defaultValue);
|
|
106
|
+
Context.displayName = contextDisplayName;
|
|
103
107
|
|
|
104
108
|
function Provider(props: React.PropsWithChildren<ContextType>) {
|
|
105
109
|
const { children, ...context } = props;
|
|
@@ -113,8 +117,7 @@ export default function createContextUtilities<
|
|
|
113
117
|
return <Context.Provider value={value}>{children}</Context.Provider>;
|
|
114
118
|
}
|
|
115
119
|
|
|
116
|
-
Provider.displayName =
|
|
117
|
-
|
|
120
|
+
Provider.displayName = providerDisplayName;
|
|
118
121
|
return {
|
|
119
122
|
[`use${contextName}`]: function (params?: HookParams) {
|
|
120
123
|
const context = React.useContext(Context);
|
|
@@ -125,7 +128,7 @@ export default function createContextUtilities<
|
|
|
125
128
|
|
|
126
129
|
return context;
|
|
127
130
|
},
|
|
128
|
-
[
|
|
129
|
-
[
|
|
131
|
+
[providerDisplayName]: Provider,
|
|
132
|
+
[contextDisplayName]: Context,
|
|
130
133
|
} as CreateContextUtilitiesReturn<ContextType, ContextName>;
|
|
131
134
|
}
|