@aws-amplify/ui-react-core 3.0.20 → 3.0.22

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.
@@ -0,0 +1,31 @@
1
+ import React__default from 'react';
2
+
3
+ // default state
4
+ const INITIAL_STATE = { hasError: false, isLoading: false, message: undefined };
5
+ const LOADING_STATE = { hasError: false, isLoading: true, message: undefined };
6
+ const ERROR_STATE = { hasError: true, isLoading: false };
7
+ const resolveMaybeAsync = async (value) => {
8
+ const awaited = await value;
9
+ return awaited;
10
+ };
11
+ function useDataState(action, initialData) {
12
+ const [dataState, setDataState] = React__default.useState(() => ({
13
+ ...INITIAL_STATE,
14
+ data: initialData,
15
+ }));
16
+ const prevData = React__default.useRef(initialData);
17
+ const handleAction = React__default.useCallback((...input) => {
18
+ setDataState(({ data }) => ({ ...LOADING_STATE, data }));
19
+ resolveMaybeAsync(action(prevData.current, ...input))
20
+ .then((data) => {
21
+ prevData.current = data;
22
+ setDataState({ ...INITIAL_STATE, data });
23
+ })
24
+ .catch(({ message }) => {
25
+ setDataState(({ data }) => ({ ...ERROR_STATE, data, message }));
26
+ });
27
+ }, [action]);
28
+ return [dataState, handleAction];
29
+ }
30
+
31
+ export { useDataState as default };
@@ -9,7 +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
+ export { default as useDataState } from './hooks/useDataState.mjs';
13
13
  export { default as useDeprecationWarning } from './hooks/useDeprecationWarning.mjs';
14
14
  export { default as useGetUrl } from './hooks/useGetUrl.mjs';
15
15
  export { default as useHasValueUpdated } from './hooks/useHasValueUpdated.mjs';
package/dist/index.js CHANGED
@@ -558,6 +558,34 @@ function useAuthenticatorInitMachine(data) {
558
558
  }, [initializeMachine, route, data]);
559
559
  }
560
560
 
561
+ // default state
562
+ const INITIAL_STATE = { hasError: false, isLoading: false, message: undefined };
563
+ const LOADING_STATE = { hasError: false, isLoading: true, message: undefined };
564
+ const ERROR_STATE = { hasError: true, isLoading: false };
565
+ const resolveMaybeAsync = async (value) => {
566
+ const awaited = await value;
567
+ return awaited;
568
+ };
569
+ function useDataState(action, initialData) {
570
+ const [dataState, setDataState] = React__default["default"].useState(() => ({
571
+ ...INITIAL_STATE,
572
+ data: initialData,
573
+ }));
574
+ const prevData = React__default["default"].useRef(initialData);
575
+ const handleAction = React__default["default"].useCallback((...input) => {
576
+ setDataState(({ data }) => ({ ...LOADING_STATE, data }));
577
+ resolveMaybeAsync(action(prevData.current, ...input))
578
+ .then((data) => {
579
+ prevData.current = data;
580
+ setDataState({ ...INITIAL_STATE, data });
581
+ })
582
+ .catch(({ message }) => {
583
+ setDataState(({ data }) => ({ ...ERROR_STATE, data, message }));
584
+ });
585
+ }, [action]);
586
+ return [dataState, handleAction];
587
+ }
588
+
561
589
  /**
562
590
  * Logs a deprecation warning message.
563
591
  *
@@ -753,6 +781,7 @@ exports.resolveAuthenticatorComponents = resolveAuthenticatorComponents;
753
781
  exports.useAuthenticator = useAuthenticator;
754
782
  exports.useAuthenticatorInitMachine = useAuthenticatorInitMachine;
755
783
  exports.useAuthenticatorRoute = useAuthenticatorRoute;
784
+ exports.useDataState = useDataState;
756
785
  exports.useDeprecationWarning = useDeprecationWarning;
757
786
  exports.useField = useField;
758
787
  exports.useForm = useForm;
@@ -22,7 +22,7 @@ type TableElementDisplayName = 'Table' | `Table${TableElementSubType}`;
22
22
  *
23
23
  * allowed values of `displayName` of `BaseElement` and `ElemebtsContext` keys
24
24
  */
25
- export type ElementDisplayName = 'Button' | 'Divider' | 'Heading' | 'Icon' | 'Input' | 'Label' | 'ListItem' | 'Nav' | 'ProgressBar' | 'Span' | 'Text' | 'Title' | 'View' | ListElementDisplayName | TableElementDisplayName;
25
+ export type ElementDisplayName = 'Button' | 'Divider' | 'Heading' | 'Icon' | 'Image' | 'Input' | 'Label' | 'ListItem' | 'Nav' | 'ProgressBar' | 'Span' | 'Text' | 'TextArea' | 'Title' | 'View' | ListElementDisplayName | TableElementDisplayName;
26
26
  /**
27
27
  * @internal @unstable
28
28
  */
@@ -1,5 +1,5 @@
1
1
  export { AuthenticatorComponentDefaults, AuthenticatorComponentDefaultProps, AuthenticatorComponentOverrides, AuthenticatorFooterComponent, AuthenticatorFormFieldsComponent, AuthenticatorHeaderComponent, AuthenticatorLegacyField, AuthenticatorMachineContext, AuthenticatorProvider, AuthenticatorRouteComponentKey, AuthenticatorRouteComponentName, isAuthenticatorComponentRouteKey, resolveAuthenticatorComponents, useAuthenticator, useAuthenticatorRoute, UseAuthenticator, useAuthenticatorInitMachine, UseAuthenticatorRoute, } from './Authenticator';
2
2
  export { FormProvider, FormProviderProps, RenderNothing, FormValues, FormHandle, useField, useForm, UseForm, Validate, Validator, withFormProvider, } from './components';
3
- export { useDeprecationWarning, UseDeprecationWarning, useGetUrl, useHasValueUpdated, usePreviousValue, useSetUserAgent, useTimeout, } from './hooks';
3
+ export { useDeprecationWarning, UseDeprecationWarning, useGetUrl, useHasValueUpdated, usePreviousValue, useSetUserAgent, useTimeout, useDataState, DataState, } from './hooks';
4
4
  export { MergeProps } from './types';
5
5
  export { createContextUtilities } from './utils';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aws-amplify/ui-react-core",
3
- "version": "3.0.20",
3
+ "version": "3.0.22",
4
4
  "main": "dist/index.js",
5
5
  "module": "dist/esm/index.mjs",
6
6
  "react-native": "src/index.ts",
@@ -40,7 +40,7 @@
40
40
  "typecheck": "tsc --noEmit"
41
41
  },
42
42
  "dependencies": {
43
- "@aws-amplify/ui": "6.3.0",
43
+ "@aws-amplify/ui": "6.4.1",
44
44
  "@xstate/react": "^3.2.2",
45
45
  "lodash": "4.17.21",
46
46
  "react-hook-form": "^7.43.5",
@@ -33,6 +33,7 @@ export type ElementDisplayName =
33
33
  | 'Divider'
34
34
  | 'Heading' // h1, h2, etc
35
35
  | 'Icon'
36
+ | 'Image'
36
37
  | 'Input'
37
38
  | 'Label'
38
39
  | 'ListItem'
@@ -40,6 +41,7 @@ export type ElementDisplayName =
40
41
  | 'ProgressBar'
41
42
  | 'Span'
42
43
  | 'Text'
44
+ | 'TextArea'
43
45
  | 'Title'
44
46
  | 'View'
45
47
  | ListElementDisplayName
package/src/index.ts CHANGED
@@ -42,6 +42,8 @@ export {
42
42
  usePreviousValue,
43
43
  useSetUserAgent,
44
44
  useTimeout,
45
+ useDataState,
46
+ DataState,
45
47
  } from './hooks';
46
48
 
47
49
  export { MergeProps } from './types';