@aws-amplify/ui-react-core 3.4.1 → 3.4.3
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/README.md +39 -0
- package/dist/esm/Authenticator/hooks/useAuthenticator/useAuthenticator.mjs +1 -1
- package/dist/esm/Authenticator/hooks/useAuthenticator/utils.mjs +1 -1
- package/dist/esm/hooks/useAsyncReducer.mjs +78 -0
- package/dist/esm/index.mjs +1 -1
- package/dist/index.js +75 -45
- package/dist/types/Authenticator/context/AuthenticatorContext.d.ts +1 -1
- package/dist/types/Authenticator/context/AuthenticatorProvider.d.ts +2 -1
- package/dist/types/Authenticator/hooks/constants.d.ts +1 -1
- package/dist/types/Authenticator/hooks/types.d.ts +3 -3
- package/dist/types/Authenticator/hooks/useAuthenticator/__mock__/useAuthenticator.d.ts +2 -2
- package/dist/types/Authenticator/hooks/useAuthenticator/index.d.ts +1 -1
- package/dist/types/Authenticator/hooks/useAuthenticator/types.d.ts +1 -1
- package/dist/types/Authenticator/hooks/useAuthenticator/useAuthenticator.d.ts +1 -1
- package/dist/types/Authenticator/hooks/useAuthenticator/utils.d.ts +3 -3
- package/dist/types/Authenticator/hooks/useAuthenticatorInitMachine/useAuthenticatorInitMachine.d.ts +2 -2
- package/dist/types/Authenticator/hooks/useAuthenticatorRoute/constants.d.ts +2 -2
- package/dist/types/Authenticator/hooks/useAuthenticatorRoute/types.d.ts +1 -1
- package/dist/types/Authenticator/hooks/useAuthenticatorRoute/useAuthenticatorRoute.d.ts +1 -1
- package/dist/types/Authenticator/hooks/useAuthenticatorRoute/utils.d.ts +4 -4
- package/dist/types/Authenticator/hooks/utils.d.ts +2 -2
- package/dist/types/Authenticator/index.d.ts +4 -2
- package/dist/types/components/FormCore/FormProvider.d.ts +1 -1
- package/dist/types/components/FormCore/index.d.ts +1 -1
- package/dist/types/components/FormCore/types.d.ts +2 -2
- package/dist/types/components/FormCore/useControlledField.d.ts +1 -1
- package/dist/types/components/FormCore/useField.d.ts +1 -1
- package/dist/types/components/FormCore/useForm.d.ts +1 -1
- package/dist/types/components/FormCore/withFormProvider.d.ts +2 -2
- package/dist/types/components/index.d.ts +2 -1
- package/dist/types/elements/ElementsContext.d.ts +1 -1
- package/dist/types/elements/defineBaseElement.d.ts +1 -1
- package/dist/types/elements/types.d.ts +1 -1
- package/dist/types/elements/utils.d.ts +1 -1
- package/dist/types/elements/withBaseElementProps.d.ts +1 -1
- package/dist/types/hooks/index.d.ts +6 -3
- package/dist/types/hooks/useAsyncReducer.d.ts +55 -0
- package/dist/types/hooks/useGetUrl.d.ts +1 -1
- package/dist/types/hooks/useSetUserAgent.d.ts +1 -1
- package/dist/types/index.d.ts +7 -4
- package/dist/types/types/index.d.ts +1 -1
- package/package.json +3 -3
- package/src/Authenticator/context/AuthenticatorContext.tsx +1 -1
- package/src/Authenticator/context/AuthenticatorProvider.tsx +3 -3
- package/src/Authenticator/hooks/constants.ts +1 -1
- package/src/Authenticator/hooks/types.ts +3 -3
- package/src/Authenticator/hooks/useAuthenticator/__mock__/useAuthenticator.ts +2 -2
- package/src/Authenticator/hooks/useAuthenticator/index.ts +1 -1
- package/src/Authenticator/hooks/useAuthenticator/types.ts +1 -1
- package/src/Authenticator/hooks/useAuthenticator/useAuthenticator.ts +3 -2
- package/src/Authenticator/hooks/useAuthenticator/utils.ts +12 -7
- package/src/Authenticator/hooks/useAuthenticatorInitMachine/useAuthenticatorInitMachine.tsx +3 -5
- package/src/Authenticator/hooks/useAuthenticatorRoute/constants.ts +2 -2
- package/src/Authenticator/hooks/useAuthenticatorRoute/types.ts +1 -1
- package/src/Authenticator/hooks/useAuthenticatorRoute/useAuthenticatorRoute.ts +1 -1
- package/src/Authenticator/hooks/useAuthenticatorRoute/utils.ts +4 -4
- package/src/Authenticator/hooks/utils.ts +6 -2
- package/src/Authenticator/index.ts +3 -4
- package/src/components/FormCore/FormProvider.tsx +1 -1
- package/src/components/FormCore/index.ts +1 -1
- package/src/components/FormCore/types.ts +5 -5
- package/src/components/FormCore/useControlledField.ts +1 -1
- package/src/components/FormCore/useField.ts +7 -2
- package/src/components/FormCore/useForm.ts +2 -2
- package/src/components/FormCore/withFormProvider.tsx +2 -2
- package/src/components/index.ts +2 -5
- package/src/elements/ElementsContext.tsx +1 -1
- package/src/elements/defineBaseElement.tsx +1 -1
- package/src/elements/types.ts +1 -1
- package/src/elements/utils.ts +1 -1
- package/src/elements/withBaseElementProps.tsx +1 -1
- package/src/hooks/index.ts +6 -12
- package/src/hooks/useAsyncReducer.ts +111 -0
- package/src/hooks/useGetUrl.ts +2 -2
- package/src/hooks/useSetUserAgent.ts +2 -1
- package/src/index.ts +22 -17
- package/src/types/index.ts +1 -1
- package/dist/esm/hooks/useDataState/useDataState.mjs +0 -48
- package/dist/types/hooks/useDataState/index.d.ts +0 -2
- package/dist/types/hooks/useDataState/types.d.ts +0 -8
- package/dist/types/hooks/useDataState/useDataState.d.ts +0 -8
- package/src/hooks/useDataState/index.ts +0 -2
- package/src/hooks/useDataState/types.ts +0 -13
- package/src/hooks/useDataState/useDataState.ts +0 -70
|
@@ -1 +1 @@
|
|
|
1
|
-
export { AnyComponent, MergeProps } from './types';
|
|
1
|
+
export type { AnyComponent, MergeProps } from './types';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aws-amplify/ui-react-core",
|
|
3
|
-
"version": "3.4.
|
|
3
|
+
"version": "3.4.3",
|
|
4
4
|
"main": "dist/index.js",
|
|
5
5
|
"module": "dist/esm/index.mjs",
|
|
6
6
|
"react-native": "src/index.ts",
|
|
@@ -40,14 +40,14 @@
|
|
|
40
40
|
"typecheck": "tsc --noEmit"
|
|
41
41
|
},
|
|
42
42
|
"dependencies": {
|
|
43
|
-
"@aws-amplify/ui": "6.10.
|
|
43
|
+
"@aws-amplify/ui": "6.10.3",
|
|
44
44
|
"@xstate/react": "^3.2.2",
|
|
45
45
|
"lodash": "4.17.21",
|
|
46
46
|
"react-hook-form": "^7.53.2",
|
|
47
47
|
"xstate": "^4.33.6"
|
|
48
48
|
},
|
|
49
49
|
"peerDependencies": {
|
|
50
|
-
"aws-amplify": "^6.
|
|
50
|
+
"aws-amplify": "^6.14.3",
|
|
51
51
|
"react": "^16.14 || ^17 || ^18 || ^19"
|
|
52
52
|
},
|
|
53
53
|
"sideEffects": false
|
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import
|
|
1
|
+
import type { ReactNode } from 'react';
|
|
2
|
+
import React, { useContext, useEffect, useMemo } from 'react';
|
|
2
3
|
import { useInterpret } from '@xstate/react';
|
|
3
4
|
|
|
4
5
|
import { getCurrentUser } from 'aws-amplify/auth';
|
|
6
|
+
import type { AuthStatus, AuthMachineHubHandler } from '@aws-amplify/ui';
|
|
5
7
|
import {
|
|
6
|
-
AuthStatus,
|
|
7
|
-
AuthMachineHubHandler,
|
|
8
8
|
createAuthenticatorMachine,
|
|
9
9
|
defaultAuthHubHandler,
|
|
10
10
|
listenToAuthHub,
|
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
import React from 'react';
|
|
1
|
+
import type React from 'react';
|
|
2
2
|
|
|
3
|
-
import {
|
|
3
|
+
import type {
|
|
4
4
|
ChallengeName,
|
|
5
5
|
AuthenticatorServiceFacade,
|
|
6
6
|
LegacyFormFieldOptions,
|
|
7
7
|
} from '@aws-amplify/ui';
|
|
8
8
|
|
|
9
|
-
import { UseAuthenticator } from './useAuthenticator';
|
|
9
|
+
import type { UseAuthenticator } from './useAuthenticator';
|
|
10
10
|
|
|
11
11
|
export type AuthenticatorRouteComponentKey =
|
|
12
12
|
| 'confirmResetPassword'
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
export { default as useAuthenticator } from './useAuthenticator';
|
|
2
|
-
export { UseAuthenticator, UseAuthenticatorSelector } from './types';
|
|
2
|
+
export type { UseAuthenticator, UseAuthenticatorSelector } from './types';
|
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
import React, { useCallback } from 'react';
|
|
2
2
|
import { useSelector } from '@xstate/react';
|
|
3
|
-
import { AuthMachineState
|
|
3
|
+
import type { AuthMachineState } from '@aws-amplify/ui';
|
|
4
|
+
import { getServiceFacade } from '@aws-amplify/ui';
|
|
4
5
|
|
|
5
6
|
import { AuthenticatorContext } from '../../context';
|
|
6
7
|
|
|
7
8
|
import { USE_AUTHENTICATOR_ERROR } from './constants';
|
|
8
|
-
import { UseAuthenticatorSelector, UseAuthenticator } from './types';
|
|
9
|
+
import type { UseAuthenticatorSelector, UseAuthenticator } from './types';
|
|
9
10
|
import {
|
|
10
11
|
defaultComparator,
|
|
11
12
|
getComparator,
|
|
@@ -1,21 +1,26 @@
|
|
|
1
|
-
import {
|
|
2
|
-
areEmptyArrays,
|
|
3
|
-
areEmptyObjects,
|
|
1
|
+
import type {
|
|
4
2
|
AuthenticatorRoute,
|
|
5
3
|
AuthMachineState,
|
|
6
4
|
FormFieldsArray,
|
|
7
|
-
getSortedFormFields,
|
|
8
5
|
UnverifiedUserAttributes,
|
|
6
|
+
AuthMFAType,
|
|
7
|
+
} from '@aws-amplify/ui';
|
|
8
|
+
import {
|
|
9
|
+
areEmptyArrays,
|
|
10
|
+
areEmptyObjects,
|
|
11
|
+
getSortedFormFields,
|
|
9
12
|
getActorContext,
|
|
10
13
|
isString,
|
|
11
|
-
AuthMFAType,
|
|
12
14
|
authenticatorTextUtil,
|
|
13
15
|
} from '@aws-amplify/ui';
|
|
14
16
|
|
|
15
|
-
import {
|
|
17
|
+
import type {
|
|
18
|
+
AuthenticatorLegacyField,
|
|
19
|
+
AuthenticatorLegacyFields,
|
|
20
|
+
} from '../types';
|
|
16
21
|
import { isComponentRouteKey } from '../utils';
|
|
17
22
|
|
|
18
|
-
import { Comparator, UseAuthenticatorSelector } from './types';
|
|
23
|
+
import type { Comparator, UseAuthenticatorSelector } from './types';
|
|
19
24
|
|
|
20
25
|
export const defaultComparator = (): false => false;
|
|
21
26
|
|
|
@@ -1,10 +1,8 @@
|
|
|
1
1
|
import React from 'react';
|
|
2
|
-
import { AuthenticatorMachineOptions } from '@aws-amplify/ui';
|
|
2
|
+
import type { AuthenticatorMachineOptions } from '@aws-amplify/ui';
|
|
3
3
|
|
|
4
|
-
import {
|
|
5
|
-
|
|
6
|
-
UseAuthenticatorSelector,
|
|
7
|
-
} from '../useAuthenticator';
|
|
4
|
+
import type { UseAuthenticatorSelector } from '../useAuthenticator';
|
|
5
|
+
import { useAuthenticator } from '../useAuthenticator';
|
|
8
6
|
|
|
9
7
|
// only select `route` from machine context
|
|
10
8
|
export const routeSelector: UseAuthenticatorSelector = ({ route }) => [route];
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { AuthenticatorRoute } from '@aws-amplify/ui';
|
|
1
|
+
import type { AuthenticatorRoute } from '@aws-amplify/ui';
|
|
2
2
|
|
|
3
3
|
import { RenderNothing } from '../../../components';
|
|
4
|
-
import {
|
|
4
|
+
import type {
|
|
5
5
|
AuthenticatorMachineContext,
|
|
6
6
|
AuthenticatorMachineContextKey,
|
|
7
7
|
AuthenticatorRouteComponentKey,
|
|
@@ -10,13 +10,13 @@ import {
|
|
|
10
10
|
Defaults,
|
|
11
11
|
} from '../types';
|
|
12
12
|
|
|
13
|
-
import {
|
|
13
|
+
import type {
|
|
14
14
|
UseAuthenticator,
|
|
15
15
|
UseAuthenticatorSelector,
|
|
16
16
|
} from '../useAuthenticator';
|
|
17
17
|
import { isComponentRouteKey } from '../utils';
|
|
18
18
|
import { MACHINE_PROP_KEYS, EVENT_HANDLER_KEY_MAP } from './constants';
|
|
19
|
-
import {
|
|
19
|
+
import type {
|
|
20
20
|
ConvertedMachineProps,
|
|
21
21
|
FormEventHandlerMachineKey,
|
|
22
22
|
FormEventHandlerPropKey,
|
|
@@ -1,7 +1,11 @@
|
|
|
1
|
-
import { AuthenticatorRoute } from '@aws-amplify/ui';
|
|
1
|
+
import type { AuthenticatorRoute } from '@aws-amplify/ui';
|
|
2
2
|
|
|
3
3
|
import { COMPONENT_ROUTE_KEYS, COMPONENT_ROUTE_NAMES } from './constants';
|
|
4
|
-
import {
|
|
4
|
+
import type {
|
|
5
|
+
AuthenticatorRouteComponentKey,
|
|
6
|
+
Defaults,
|
|
7
|
+
Overrides,
|
|
8
|
+
} from './types';
|
|
5
9
|
|
|
6
10
|
export const isComponentRouteKey = (
|
|
7
11
|
route: AuthenticatorRoute
|
|
@@ -1,23 +1,22 @@
|
|
|
1
1
|
export { AuthenticatorProvider, AuthenticatorContext } from './context';
|
|
2
|
+
export type { UseAuthenticator, UseAuthenticatorRoute } from './hooks';
|
|
2
3
|
export {
|
|
3
4
|
resolveAuthenticatorComponents,
|
|
4
5
|
useAuthenticator,
|
|
5
6
|
useAuthenticatorRoute,
|
|
6
|
-
UseAuthenticator,
|
|
7
7
|
useAuthenticatorInitMachine,
|
|
8
|
-
UseAuthenticatorRoute,
|
|
9
8
|
} from './hooks';
|
|
10
|
-
export {
|
|
9
|
+
export type {
|
|
11
10
|
Overrides as AuthenticatorComponentOverrides,
|
|
12
11
|
Defaults as AuthenticatorComponentDefaults,
|
|
13
12
|
DefaultProps as AuthenticatorComponentDefaultProps,
|
|
14
13
|
FooterComponent as AuthenticatorFooterComponent,
|
|
15
14
|
FormFieldsComponent as AuthenticatorFormFieldsComponent,
|
|
16
15
|
HeaderComponent as AuthenticatorHeaderComponent,
|
|
17
|
-
isComponentRouteKey as isAuthenticatorComponentRouteKey,
|
|
18
16
|
AuthenticatorRouteComponentKey,
|
|
19
17
|
AuthenticatorRouteComponentName,
|
|
20
18
|
AuthenticatorLegacyField,
|
|
21
19
|
AuthenticatorMachineContext,
|
|
22
20
|
FormFieldsComponent,
|
|
23
21
|
} from './hooks';
|
|
22
|
+
export { isComponentRouteKey as isAuthenticatorComponentRouteKey } from './hooks';
|
|
@@ -4,7 +4,7 @@ import {
|
|
|
4
4
|
FormProvider as ReactHookFormProvider,
|
|
5
5
|
} from 'react-hook-form';
|
|
6
6
|
|
|
7
|
-
import { FormValues, FormHandle, FormProviderProps } from './types';
|
|
7
|
+
import type { FormValues, FormHandle, FormProviderProps } from './types';
|
|
8
8
|
|
|
9
9
|
const DEFAULT_MODE = 'onTouched';
|
|
10
10
|
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import React from 'react';
|
|
2
|
-
import { DefaultValues } from 'react-hook-form';
|
|
1
|
+
import type React from 'react';
|
|
2
|
+
import type { DefaultValues } from 'react-hook-form';
|
|
3
3
|
|
|
4
4
|
export interface FormValues {
|
|
5
5
|
[k: string]: string;
|
|
@@ -63,7 +63,7 @@ export interface RegisterFieldParams<Values extends FormValues = FormValues> {
|
|
|
63
63
|
|
|
64
64
|
export interface UseFormParams<
|
|
65
65
|
Values extends FormValues = FormValues,
|
|
66
|
-
OnSubmit extends SubmitHandler<Values> = SubmitHandler<Values
|
|
66
|
+
OnSubmit extends SubmitHandler<Values> = SubmitHandler<Values>,
|
|
67
67
|
> {
|
|
68
68
|
/**
|
|
69
69
|
* Custom error message provided to `Error` on call to `useForm` outside `FormProvider`
|
|
@@ -193,7 +193,7 @@ export interface UseField<Name extends string = string>
|
|
|
193
193
|
export interface UseFieldParams<
|
|
194
194
|
OnBlur extends FocusHandler | undefined,
|
|
195
195
|
OnChange extends ChangeHandler | undefined,
|
|
196
|
-
Values extends FormValues = FormValues
|
|
196
|
+
Values extends FormValues = FormValues,
|
|
197
197
|
> {
|
|
198
198
|
/**
|
|
199
199
|
* Controlled `disabled` state
|
|
@@ -252,7 +252,7 @@ export interface UseFieldParams<
|
|
|
252
252
|
*/
|
|
253
253
|
export interface UseControlledFieldParams<
|
|
254
254
|
OnBlur extends FocusHandler | undefined,
|
|
255
|
-
Values extends FormValues = FormValues
|
|
255
|
+
Values extends FormValues = FormValues,
|
|
256
256
|
> extends Omit<UseFieldParams<OnBlur, undefined, Values>, 'onChange'> {
|
|
257
257
|
/**
|
|
258
258
|
* Controlled text change event handler
|
|
@@ -1,5 +1,10 @@
|
|
|
1
1
|
import useForm from './useForm';
|
|
2
|
-
import {
|
|
2
|
+
import type {
|
|
3
|
+
ChangeHandler,
|
|
4
|
+
FocusHandler,
|
|
5
|
+
UseField,
|
|
6
|
+
UseFieldParams,
|
|
7
|
+
} from './types';
|
|
3
8
|
|
|
4
9
|
export const DEFAULT_ERROR_MESSAGE =
|
|
5
10
|
'`useField` must be used within a `FormProvider`';
|
|
@@ -12,7 +17,7 @@ export const DEFAULT_ERROR_MESSAGE =
|
|
|
12
17
|
*/
|
|
13
18
|
export default function useField<
|
|
14
19
|
OnBlur extends FocusHandler,
|
|
15
|
-
OnChange extends ChangeHandler
|
|
20
|
+
OnChange extends ChangeHandler,
|
|
16
21
|
>(params: UseFieldParams<OnBlur, OnChange>): UseField {
|
|
17
22
|
const { getFieldState, registerField } = useForm({
|
|
18
23
|
errorMessage: DEFAULT_ERROR_MESSAGE,
|
|
@@ -2,7 +2,7 @@ import React from 'react';
|
|
|
2
2
|
import { useFormContext } from 'react-hook-form';
|
|
3
3
|
import { noop } from '@aws-amplify/ui';
|
|
4
4
|
|
|
5
|
-
import {
|
|
5
|
+
import type {
|
|
6
6
|
FormValues,
|
|
7
7
|
RegisterFieldParams,
|
|
8
8
|
SetFormValueParams,
|
|
@@ -25,7 +25,7 @@ export const DEFAULT_ERROR_MESSAGE =
|
|
|
25
25
|
*/
|
|
26
26
|
export default function useForm<
|
|
27
27
|
Values extends FormValues = FormValues,
|
|
28
|
-
OnSubmit extends SubmitHandler = SubmitHandler
|
|
28
|
+
OnSubmit extends SubmitHandler = SubmitHandler,
|
|
29
29
|
>(options: UseFormParams<Values, OnSubmit> = {}): UseForm<Values> {
|
|
30
30
|
const formContext = useFormContext();
|
|
31
31
|
const { errorMessage, onSubmit: _onSubmit } = options;
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import React from 'react';
|
|
2
2
|
|
|
3
|
-
import { AnyComponent } from '../../types';
|
|
3
|
+
import type { AnyComponent } from '../../types';
|
|
4
4
|
|
|
5
5
|
import FormProvider from './FormProvider';
|
|
6
|
-
import { FormHandle, FormProviderProps } from './types';
|
|
6
|
+
import type { FormHandle, FormProviderProps } from './types';
|
|
7
7
|
|
|
8
8
|
/**
|
|
9
9
|
* @param Child `Form` base component wrapped inside `FormProvider`
|
package/src/components/index.ts
CHANGED
|
@@ -1,15 +1,12 @@
|
|
|
1
|
-
export {
|
|
2
|
-
FormProvider,
|
|
1
|
+
export type {
|
|
3
2
|
FormProviderProps,
|
|
4
3
|
FormValues,
|
|
5
4
|
FormHandle,
|
|
6
5
|
SubmitHandler,
|
|
7
|
-
useField,
|
|
8
|
-
useForm,
|
|
9
6
|
UseForm,
|
|
10
7
|
Validate,
|
|
11
8
|
Validator,
|
|
12
|
-
withFormProvider,
|
|
13
9
|
} from './FormCore';
|
|
10
|
+
export { FormProvider, useField, useForm, withFormProvider } from './FormCore';
|
|
14
11
|
|
|
15
12
|
export { RenderNothing } from './RenderNothing';
|
package/src/elements/types.ts
CHANGED
package/src/elements/utils.ts
CHANGED
package/src/hooks/index.ts
CHANGED
|
@@ -1,16 +1,10 @@
|
|
|
1
|
-
export {
|
|
2
|
-
useDataState,
|
|
3
|
-
AsyncDataAction,
|
|
4
|
-
DataAction,
|
|
5
|
-
DataState,
|
|
6
|
-
} from './useDataState';
|
|
7
|
-
|
|
8
1
|
export { default as useControlledReducer } from './useControlledReducer';
|
|
9
|
-
export {
|
|
10
|
-
export {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
} from './useDeprecationWarning';
|
|
2
|
+
export type { UseDropZoneParams } from './useDropZone';
|
|
3
|
+
export { default as useDropZone } from './useDropZone';
|
|
4
|
+
export type { AsyncReducer, AsyncReducerState } from './useAsyncReducer';
|
|
5
|
+
export { default as useAsyncReducer } from './useAsyncReducer';
|
|
6
|
+
export type { UseDeprecationWarning } from './useDeprecationWarning';
|
|
7
|
+
export { default as useDeprecationWarning } from './useDeprecationWarning';
|
|
14
8
|
export { default as useGetUrl } from './useGetUrl';
|
|
15
9
|
export { default as useHasValueUpdated } from './useHasValueUpdated';
|
|
16
10
|
export { default as usePreviousValue } from './usePreviousValue';
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
|
|
3
|
+
import { isFunction } from '@aws-amplify/ui';
|
|
4
|
+
|
|
5
|
+
// async state constants
|
|
6
|
+
const INITIAL = { hasError: false, isLoading: false, message: undefined };
|
|
7
|
+
const LOADING = { hasError: false, isLoading: true, message: undefined };
|
|
8
|
+
const ERROR = { hasError: true, isLoading: false };
|
|
9
|
+
|
|
10
|
+
export interface AsyncReducerState<T> {
|
|
11
|
+
/**
|
|
12
|
+
* current value
|
|
13
|
+
*/
|
|
14
|
+
value: T;
|
|
15
|
+
hasError: boolean;
|
|
16
|
+
isLoading: boolean;
|
|
17
|
+
/**
|
|
18
|
+
* error message, if any
|
|
19
|
+
*/
|
|
20
|
+
message: string | undefined;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export type AsyncReducer<S, A> = (prevValue: S, action: A) => Promise<S>;
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* @internal may be updated in future versions
|
|
27
|
+
*
|
|
28
|
+
* @description like `useReducer` but make it async
|
|
29
|
+
*
|
|
30
|
+
* @example
|
|
31
|
+
* ```ts
|
|
32
|
+
* import fetchData from './fetchData';
|
|
33
|
+
*
|
|
34
|
+
* type MyState = { data: string[] | undefined }
|
|
35
|
+
* const initialState: MyState = { data: undefined }
|
|
36
|
+
*
|
|
37
|
+
* type MyAction = { type: 'fetch' | 'clear' }
|
|
38
|
+
*
|
|
39
|
+
* const asyncReducer = async (state: MyState, action: MyAction): Promise<MyState> => {
|
|
40
|
+
* switch(action.type) {
|
|
41
|
+
* case 'fetch':
|
|
42
|
+
* const data = await fetchData();
|
|
43
|
+
* return { data: state.data ? state.data.concat(data) : data }
|
|
44
|
+
* case 'clear':
|
|
45
|
+
* return { data: undefined }
|
|
46
|
+
* }
|
|
47
|
+
* }
|
|
48
|
+
*
|
|
49
|
+
* const FetchDataButton = () => {
|
|
50
|
+
* const [state, dispatch] = useAsyncReducer(asyncReducer, initialState);
|
|
51
|
+
*
|
|
52
|
+
* const { value: { data }, isLoading } = state;
|
|
53
|
+
*
|
|
54
|
+
* return (
|
|
55
|
+
* <button onClick={() => isLoading ? null : dispatch({ type: 'fetch'})}>
|
|
56
|
+
* Fetch Data
|
|
57
|
+
* </button>
|
|
58
|
+
* )
|
|
59
|
+
* }
|
|
60
|
+
* ```
|
|
61
|
+
*/
|
|
62
|
+
export default function useAsyncReducer<T, K>(
|
|
63
|
+
reducer: AsyncReducer<T, K>,
|
|
64
|
+
initialValue: T,
|
|
65
|
+
options?: {
|
|
66
|
+
onSuccess?: (data: T) => void;
|
|
67
|
+
onError?: (error: Error) => void;
|
|
68
|
+
}
|
|
69
|
+
): [AsyncReducerState<T>, React.Dispatch<K>] {
|
|
70
|
+
const [state, setAsyncState] = React.useState<AsyncReducerState<T>>(() => ({
|
|
71
|
+
...INITIAL,
|
|
72
|
+
value: initialValue,
|
|
73
|
+
}));
|
|
74
|
+
|
|
75
|
+
const prevValue = React.useRef(initialValue);
|
|
76
|
+
const pendingId = React.useRef<Symbol | undefined>();
|
|
77
|
+
|
|
78
|
+
const { onSuccess, onError } = options ?? {};
|
|
79
|
+
|
|
80
|
+
const dispatch: React.Dispatch<K> = React.useCallback(
|
|
81
|
+
(input) => {
|
|
82
|
+
const id = Symbol();
|
|
83
|
+
pendingId.current = id;
|
|
84
|
+
|
|
85
|
+
setAsyncState(({ value }) => ({ ...LOADING, value }));
|
|
86
|
+
|
|
87
|
+
reducer(prevValue.current, input)
|
|
88
|
+
.then((value: T) => {
|
|
89
|
+
if (pendingId.current !== id) return;
|
|
90
|
+
|
|
91
|
+
prevValue.current = value;
|
|
92
|
+
|
|
93
|
+
if (isFunction(onSuccess)) onSuccess(value);
|
|
94
|
+
|
|
95
|
+
setAsyncState({ ...INITIAL, value });
|
|
96
|
+
})
|
|
97
|
+
.catch((error: Error) => {
|
|
98
|
+
if (pendingId.current !== id) return;
|
|
99
|
+
|
|
100
|
+
if (isFunction(onError)) onError(error);
|
|
101
|
+
|
|
102
|
+
const { message } = error;
|
|
103
|
+
|
|
104
|
+
setAsyncState(({ value }) => ({ ...ERROR, value, message }));
|
|
105
|
+
});
|
|
106
|
+
},
|
|
107
|
+
[onError, onSuccess, reducer]
|
|
108
|
+
);
|
|
109
|
+
|
|
110
|
+
return [state, dispatch];
|
|
111
|
+
}
|
package/src/hooks/useGetUrl.ts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import * as React from 'react';
|
|
2
2
|
|
|
3
|
-
import {
|
|
4
|
-
getUrl,
|
|
3
|
+
import type {
|
|
5
4
|
GetUrlInput,
|
|
6
5
|
GetUrlOutput,
|
|
7
6
|
GetUrlWithPathInput,
|
|
8
7
|
} from 'aws-amplify/storage';
|
|
8
|
+
import { getUrl } from 'aws-amplify/storage';
|
|
9
9
|
|
|
10
10
|
import { isFunction } from '@aws-amplify/ui';
|
|
11
11
|
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { useEffect } from 'react';
|
|
2
|
-
import {
|
|
2
|
+
import type { SetUserAgentOptions } from '@aws-amplify/ui';
|
|
3
|
+
import { setUserAgent } from '@aws-amplify/ui';
|
|
3
4
|
|
|
4
5
|
export default function useSetUserAgent({
|
|
5
6
|
componentName,
|