@aws-amplify/ui-react-core 3.0.15 → 3.0.17

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.
@@ -9,6 +9,7 @@ export { default as useForm } from './components/FormCore/useForm.mjs';
9
9
  export { default as FormProvider } from './components/FormCore/FormProvider.mjs';
10
10
  export { default as withFormProvider } from './components/FormCore/withFormProvider.mjs';
11
11
  export { default as RenderNothing } from './components/RenderNothing/RenderNothing.mjs';
12
+ import 'react';
12
13
  export { default as useDeprecationWarning } from './hooks/useDeprecationWarning.mjs';
13
14
  export { default as useGetUrl } from './hooks/useGetUrl.mjs';
14
15
  export { default as useHasValueUpdated } from './hooks/useHasValueUpdated.mjs';
@@ -1,3 +1,4 @@
1
+ export { default as useDataState } from './useDataState';
1
2
  export { default as useDeprecationWarning, UseDeprecationWarning, } from './useDeprecationWarning';
2
3
  export { default as useGetUrl } from './useGetUrl';
3
4
  export { default as useHasValueUpdated } from './useHasValueUpdated';
@@ -0,0 +1,7 @@
1
+ interface ActionState<T> {
2
+ data: T;
3
+ isLoading: boolean;
4
+ message: string | undefined;
5
+ }
6
+ export default function useDataState<T, K>(action: (prevData: Awaited<T>, ...input: K[]) => T | Promise<T>, initialData: Awaited<T>): [state: ActionState<Awaited<T>>, handleAction: (...input: K[]) => void];
7
+ export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aws-amplify/ui-react-core",
3
- "version": "3.0.15",
3
+ "version": "3.0.17",
4
4
  "main": "dist/index.js",
5
5
  "module": "dist/esm/index.mjs",
6
6
  "react-native": "src/index.ts",
@@ -33,14 +33,14 @@
33
33
  "typecheck": "tsc --noEmit"
34
34
  },
35
35
  "dependencies": {
36
- "@aws-amplify/ui": "6.0.15",
36
+ "@aws-amplify/ui": "6.0.17",
37
37
  "@xstate/react": "^3.2.2",
38
38
  "lodash": "4.17.21",
39
39
  "react-hook-form": "^7.43.5",
40
40
  "xstate": "^4.33.6"
41
41
  },
42
42
  "peerDependencies": {
43
- "aws-amplify": "^6.2.0",
43
+ "aws-amplify": "^6.3.2",
44
44
  "react": "^16.14.0 || ^17.0 || ^18.0"
45
45
  },
46
46
  "sideEffects": false
@@ -1,3 +1,4 @@
1
+ export { default as useDataState } from './useDataState';
1
2
  export {
2
3
  default as useDeprecationWarning,
3
4
  UseDeprecationWarning,
@@ -0,0 +1,42 @@
1
+ import React from 'react';
2
+
3
+ interface ActionState<T> {
4
+ data: T;
5
+ isLoading: boolean;
6
+ message: string | undefined;
7
+ }
8
+
9
+ const getActionState = <T>(data: T): ActionState<T> => ({
10
+ data,
11
+ isLoading: false,
12
+ message: undefined,
13
+ });
14
+
15
+ export default function useDataState<T, K>(
16
+ action: (prevData: Awaited<T>, ...input: K[]) => T | Promise<T>,
17
+ initialData: Awaited<T>
18
+ ): [state: ActionState<Awaited<T>>, handleAction: (...input: K[]) => void] {
19
+ const [actionState, setActionState] = React.useState<ActionState<Awaited<T>>>(
20
+ () => getActionState(initialData)
21
+ );
22
+
23
+ const prevData = React.useRef(initialData);
24
+
25
+ const handleAction: (...input: K[]) => void = React.useCallback(
26
+ (...input) => {
27
+ setActionState((prev) => ({ ...prev, isLoading: true }));
28
+
29
+ Promise.resolve(action(prevData.current, ...input))
30
+ .then((data) => {
31
+ prevData.current = data;
32
+ setActionState(getActionState(data));
33
+ })
34
+ .catch(({ message }: Error) => {
35
+ setActionState((prev) => ({ ...prev, isLoading: false, message }));
36
+ });
37
+ },
38
+ [action]
39
+ );
40
+
41
+ return [actionState, handleAction];
42
+ }