@aws-amplify/ui-react-core 2.1.32 → 3.0.0
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/esm/Authenticator/context/AuthenticatorProvider.mjs +5 -6
- package/dist/esm/Authenticator/hooks/constants.mjs +4 -4
- package/dist/esm/Authenticator/hooks/useAuthenticator/useAuthenticator.mjs +14 -11
- package/dist/esm/Authenticator/hooks/useAuthenticator/utils.mjs +10 -18
- package/dist/esm/Authenticator/hooks/useAuthenticatorRoute/constants.mjs +7 -4
- package/dist/esm/Authenticator/hooks/useAuthenticatorRoute/useAuthenticatorRoute.mjs +6 -6
- package/dist/esm/Authenticator/hooks/useAuthenticatorRoute/utils.mjs +47 -18
- package/dist/esm/Authenticator/hooks/utils.mjs +2 -2
- package/dist/esm/components/FormCore/FormProvider.mjs +15 -0
- package/dist/esm/components/FormCore/useField.mjs +20 -0
- package/dist/esm/components/FormCore/useForm.mjs +51 -0
- package/dist/esm/components/FormCore/withFormProvider.mjs +15 -0
- package/dist/esm/hooks/useSetUserAgent.mjs +15 -0
- package/dist/esm/hooks/useTimeout.mjs +22 -0
- package/dist/esm/index.mjs +7 -0
- package/dist/esm/utils/createContextUtilities.mjs +80 -0
- package/dist/index.js +284 -96
- package/dist/types/Authenticator/hooks/types.d.ts +10 -7
- package/dist/types/Authenticator/hooks/useAuthenticator/types.d.ts +1 -3
- package/dist/types/Authenticator/hooks/useAuthenticator/utils.d.ts +2 -3
- package/dist/types/Authenticator/hooks/useAuthenticatorRoute/types.d.ts +3 -3
- package/dist/types/Authenticator/hooks/useAuthenticatorRoute/useAuthenticatorRoute.d.ts +2 -2
- package/dist/types/Authenticator/hooks/useAuthenticatorRoute/utils.d.ts +2 -2
- package/dist/types/components/FormCore/FormProvider.d.ts +4 -0
- package/dist/types/components/FormCore/index.d.ts +5 -0
- package/dist/types/components/FormCore/types.d.ts +217 -0
- package/dist/types/components/FormCore/useControlledField.d.ts +9 -0
- package/dist/types/components/FormCore/useField.d.ts +9 -0
- package/dist/types/components/FormCore/useForm.d.ts +12 -0
- package/dist/types/components/FormCore/withFormProvider.d.ts +8 -0
- package/dist/types/components/index.d.ts +1 -0
- package/dist/types/hooks/index.d.ts +2 -0
- package/dist/types/hooks/useSetUserAgent.d.ts +2 -0
- package/dist/types/hooks/useTimeout.d.ts +4 -0
- package/dist/types/index.d.ts +3 -2
- package/dist/types/types/index.d.ts +1 -1
- package/dist/types/types/types.d.ts +2 -0
- package/dist/types/utils/createContextUtilities.d.ts +26 -18
- package/package.json +11 -29
- package/src/Authenticator/context/AuthenticatorContext.tsx +17 -0
- package/src/Authenticator/context/AuthenticatorProvider.tsx +82 -0
- package/src/Authenticator/context/index.ts +2 -0
- package/src/Authenticator/hooks/constants.ts +30 -0
- package/src/Authenticator/hooks/index.ts +5 -0
- package/src/Authenticator/hooks/types.ts +218 -0
- package/src/Authenticator/hooks/useAuthenticator/__mock__/useAuthenticator.ts +66 -0
- package/src/Authenticator/hooks/useAuthenticator/constants.ts +2 -0
- package/src/Authenticator/hooks/useAuthenticator/index.ts +2 -0
- package/src/Authenticator/hooks/useAuthenticator/types.ts +48 -0
- package/src/Authenticator/hooks/useAuthenticator/useAuthenticator.ts +72 -0
- package/src/Authenticator/hooks/useAuthenticator/utils.ts +97 -0
- package/src/Authenticator/hooks/useAuthenticatorInitMachine/index.ts +1 -0
- package/src/Authenticator/hooks/useAuthenticatorInitMachine/useAuthenticatorInitMachine.tsx +25 -0
- package/src/Authenticator/hooks/useAuthenticatorRoute/constants.ts +107 -0
- package/src/Authenticator/hooks/useAuthenticatorRoute/index.ts +2 -0
- package/src/Authenticator/hooks/useAuthenticatorRoute/types.ts +111 -0
- package/src/Authenticator/hooks/useAuthenticatorRoute/useAuthenticatorRoute.ts +126 -0
- package/src/Authenticator/hooks/useAuthenticatorRoute/utils.ts +204 -0
- package/src/Authenticator/hooks/utils.ts +38 -0
- package/src/Authenticator/index.ts +23 -0
- package/src/components/FormCore/FormProvider.tsx +37 -0
- package/src/components/FormCore/index.ts +13 -0
- package/src/components/FormCore/types.ts +277 -0
- package/src/components/FormCore/useControlledField.ts +73 -0
- package/src/components/FormCore/useField.ts +25 -0
- package/src/components/FormCore/useForm.ts +84 -0
- package/src/components/FormCore/withFormProvider.tsx +31 -0
- package/src/components/RenderNothing/RenderNothing.tsx +6 -0
- package/src/components/RenderNothing/index.ts +1 -0
- package/src/components/index.ts +15 -0
- package/src/hooks/index.ts +8 -0
- package/src/hooks/useDeprecationWarning.ts +27 -0
- package/src/hooks/useHasValueUpdated.ts +28 -0
- package/src/hooks/usePreviousValue.ts +15 -0
- package/src/hooks/useSetUserAgent.ts +18 -0
- package/src/hooks/useTimeout.ts +30 -0
- package/src/index.ts +48 -0
- package/src/types/index.ts +1 -0
- package/src/types/types.ts +3 -0
- package/src/utils/createContextUtilities.tsx +131 -0
- package/src/utils/index.ts +1 -0
- package/dist/esm/node_modules/tslib/tslib.es6.mjs +0 -38
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import {
|
|
2
|
+
AuthenticatorMachineContext,
|
|
3
|
+
AuthenticatorMachineContextKey,
|
|
4
|
+
AuthenticatorRouteComponentName,
|
|
5
|
+
CommonRouteProps,
|
|
6
|
+
ConfirmResetPasswordBaseProps,
|
|
7
|
+
ConfirmSignInBaseProps,
|
|
8
|
+
ConfirmSignUpBaseProps,
|
|
9
|
+
Defaults,
|
|
10
|
+
DefaultProps,
|
|
11
|
+
ForceResetPasswordBaseProps,
|
|
12
|
+
ResetPasswordBaseProps,
|
|
13
|
+
SetupTotpBaseProps,
|
|
14
|
+
SignInBaseProps,
|
|
15
|
+
SignUpBaseProps,
|
|
16
|
+
VerifyUserProps,
|
|
17
|
+
ConfirmVerifyUserProps,
|
|
18
|
+
} from '../types';
|
|
19
|
+
|
|
20
|
+
export type UseAuthenticatorRouteParams<FieldType> = {
|
|
21
|
+
components: Defaults<FieldType>;
|
|
22
|
+
};
|
|
23
|
+
export type UseAuthenticatorRoute<
|
|
24
|
+
ComponentName extends AuthenticatorRouteComponentName,
|
|
25
|
+
FieldType = {},
|
|
26
|
+
> = {
|
|
27
|
+
Component: Defaults<FieldType>[ComponentName];
|
|
28
|
+
props: DefaultProps<FieldType>[ComponentName];
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
export type UseAuthenticatorRouteDefault<FieldType> = {
|
|
32
|
+
Component: Defaults<FieldType>[AuthenticatorRouteComponentName];
|
|
33
|
+
props: DefaultProps<FieldType>[AuthenticatorRouteComponentName];
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
// extract machine prop keys required for a sub-component route
|
|
37
|
+
type ExtractMachineKey<RouteProps> = Extract<
|
|
38
|
+
AuthenticatorMachineContextKey,
|
|
39
|
+
keyof RouteProps
|
|
40
|
+
>;
|
|
41
|
+
|
|
42
|
+
// map to `handleBlur`, `handleChange`, and `handleSubmit` props
|
|
43
|
+
export type FormEventHandlerMachineKey =
|
|
44
|
+
| 'updateBlur'
|
|
45
|
+
| 'updateForm'
|
|
46
|
+
| 'submitForm';
|
|
47
|
+
|
|
48
|
+
export type FormEventHandlerPropKey =
|
|
49
|
+
| `handleBlur`
|
|
50
|
+
| `handleChange`
|
|
51
|
+
| `handleSubmit`;
|
|
52
|
+
|
|
53
|
+
// common route keys shared by all routes
|
|
54
|
+
export type CommonRouteMachineKey =
|
|
55
|
+
| ExtractMachineKey<CommonRouteProps>
|
|
56
|
+
| FormEventHandlerMachineKey;
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* `route` sub-component machine selector key types
|
|
60
|
+
*/
|
|
61
|
+
export type ConfirmResetPasswordMachineKey =
|
|
62
|
+
| ExtractMachineKey<ConfirmResetPasswordBaseProps>
|
|
63
|
+
| CommonRouteMachineKey;
|
|
64
|
+
|
|
65
|
+
export type ConfirmSignInMachineKey =
|
|
66
|
+
| ExtractMachineKey<ConfirmSignInBaseProps>
|
|
67
|
+
| CommonRouteMachineKey;
|
|
68
|
+
|
|
69
|
+
export type ConfirmSignUpMachineKey =
|
|
70
|
+
| ExtractMachineKey<ConfirmSignUpBaseProps>
|
|
71
|
+
| CommonRouteMachineKey;
|
|
72
|
+
|
|
73
|
+
export type ConfirmVerifyUserMachineKey =
|
|
74
|
+
| ExtractMachineKey<ConfirmVerifyUserProps>
|
|
75
|
+
| CommonRouteMachineKey;
|
|
76
|
+
|
|
77
|
+
export type ForceNewPasswordMachineKey =
|
|
78
|
+
| ExtractMachineKey<ForceResetPasswordBaseProps>
|
|
79
|
+
| CommonRouteMachineKey;
|
|
80
|
+
|
|
81
|
+
export type ResetPasswordMachineKey =
|
|
82
|
+
| ExtractMachineKey<ResetPasswordBaseProps>
|
|
83
|
+
| CommonRouteMachineKey;
|
|
84
|
+
|
|
85
|
+
export type SetupTotpMachineKey =
|
|
86
|
+
| ExtractMachineKey<SetupTotpBaseProps>
|
|
87
|
+
| CommonRouteMachineKey;
|
|
88
|
+
|
|
89
|
+
export type SignInMachineKey =
|
|
90
|
+
| ExtractMachineKey<SignInBaseProps>
|
|
91
|
+
| CommonRouteMachineKey;
|
|
92
|
+
|
|
93
|
+
export type SignUpMachineKey =
|
|
94
|
+
| ExtractMachineKey<SignUpBaseProps>
|
|
95
|
+
| CommonRouteMachineKey;
|
|
96
|
+
|
|
97
|
+
export type VerifyUserMachineKey =
|
|
98
|
+
| ExtractMachineKey<VerifyUserProps>
|
|
99
|
+
| CommonRouteMachineKey;
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* machine values with machine form event handlers keys mapped to UI form event handlers
|
|
103
|
+
*/
|
|
104
|
+
export type ConvertedMachineProps = Omit<
|
|
105
|
+
AuthenticatorMachineContext,
|
|
106
|
+
FormEventHandlerMachineKey
|
|
107
|
+
> & {
|
|
108
|
+
handleBlur: AuthenticatorMachineContext['updateBlur'];
|
|
109
|
+
handleChange: AuthenticatorMachineContext['updateForm'];
|
|
110
|
+
handleSubmit: AuthenticatorMachineContext['submitForm'];
|
|
111
|
+
};
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import { useMemo } from 'react';
|
|
2
|
+
import { useAuthenticator } from '../useAuthenticator';
|
|
3
|
+
|
|
4
|
+
import {
|
|
5
|
+
UseAuthenticatorRoute,
|
|
6
|
+
UseAuthenticatorRouteDefault,
|
|
7
|
+
UseAuthenticatorRouteParams,
|
|
8
|
+
} from './types';
|
|
9
|
+
import {
|
|
10
|
+
getRouteMachineSelector,
|
|
11
|
+
routeSelector,
|
|
12
|
+
resolveConfirmResetPasswordRoute,
|
|
13
|
+
resolveConfirmSignInRoute,
|
|
14
|
+
resolveConfirmSignUpRoute,
|
|
15
|
+
resolveConfirmVerifyUserRoute,
|
|
16
|
+
resolveDefault,
|
|
17
|
+
resolveForceNewPasswordRoute,
|
|
18
|
+
resolveForgotPasswordRoute,
|
|
19
|
+
resolveSetupTotpRoute,
|
|
20
|
+
resolveSignInRoute,
|
|
21
|
+
resolveSignUpRoute,
|
|
22
|
+
resolveVerifyUserRoute,
|
|
23
|
+
} from './utils';
|
|
24
|
+
|
|
25
|
+
export default function useAuthenticatorRoute<FieldType>(
|
|
26
|
+
params: UseAuthenticatorRouteParams<FieldType>
|
|
27
|
+
): UseAuthenticatorRoute<'ConfirmResetPassword'>;
|
|
28
|
+
export default function useAuthenticatorRoute<FieldType>(
|
|
29
|
+
params: UseAuthenticatorRouteParams<FieldType>
|
|
30
|
+
): UseAuthenticatorRoute<'ConfirmSignIn'>;
|
|
31
|
+
export default function useAuthenticatorRoute<FieldType>(
|
|
32
|
+
params: UseAuthenticatorRouteParams<FieldType>
|
|
33
|
+
): UseAuthenticatorRoute<'ConfirmSignUp'>;
|
|
34
|
+
export default function useAuthenticatorRoute<FieldType>(
|
|
35
|
+
params: UseAuthenticatorRouteParams<FieldType>
|
|
36
|
+
): UseAuthenticatorRoute<'ConfirmVerifyUser'>;
|
|
37
|
+
export default function useAuthenticatorRoute<FieldType>(
|
|
38
|
+
params: UseAuthenticatorRouteParams<FieldType>
|
|
39
|
+
): UseAuthenticatorRoute<'ForceNewPassword'>;
|
|
40
|
+
export default function useAuthenticatorRoute<FieldType>(
|
|
41
|
+
params: UseAuthenticatorRouteParams<FieldType>
|
|
42
|
+
): UseAuthenticatorRoute<'ForgotPassword'>;
|
|
43
|
+
export default function useAuthenticatorRoute<FieldType>(
|
|
44
|
+
params: UseAuthenticatorRouteParams<FieldType>
|
|
45
|
+
): UseAuthenticatorRoute<'SetupTotp'>;
|
|
46
|
+
export default function useAuthenticatorRoute<FieldType>(
|
|
47
|
+
params: UseAuthenticatorRouteParams<FieldType>
|
|
48
|
+
): UseAuthenticatorRoute<'SignIn'>;
|
|
49
|
+
export default function useAuthenticatorRoute<FieldType>(
|
|
50
|
+
params: UseAuthenticatorRouteParams<FieldType>
|
|
51
|
+
): UseAuthenticatorRoute<'SignUp'>;
|
|
52
|
+
export default function useAuthenticatorRoute<FieldType>(
|
|
53
|
+
params: UseAuthenticatorRouteParams<FieldType>
|
|
54
|
+
): UseAuthenticatorRoute<'VerifyUser'>;
|
|
55
|
+
export default function useAuthenticatorRoute<FieldType>({
|
|
56
|
+
components,
|
|
57
|
+
}: UseAuthenticatorRouteParams<FieldType>): UseAuthenticatorRouteDefault<FieldType> {
|
|
58
|
+
const { route } = useAuthenticator(routeSelector);
|
|
59
|
+
|
|
60
|
+
const routeMachineSelector = useMemo(
|
|
61
|
+
() => getRouteMachineSelector(route),
|
|
62
|
+
[route]
|
|
63
|
+
);
|
|
64
|
+
|
|
65
|
+
// `useAuthenticator` exposes both state machine (example: `toSignIn`) and non-state machine
|
|
66
|
+
// props (example: `getTotpSecretCode`). `routeSelector` specifies which state machine props
|
|
67
|
+
// should be returned for a specific route.
|
|
68
|
+
// Only state machine props specified by the current `routeSelector` will have their current value
|
|
69
|
+
// returned by `useAuthenticator`, non-machine props returned will always be the current value
|
|
70
|
+
const routeSelectorProps = useAuthenticator(routeMachineSelector);
|
|
71
|
+
|
|
72
|
+
const {
|
|
73
|
+
ConfirmResetPassword,
|
|
74
|
+
ConfirmSignIn,
|
|
75
|
+
ConfirmSignUp,
|
|
76
|
+
ConfirmVerifyUser,
|
|
77
|
+
ForceNewPassword,
|
|
78
|
+
ForgotPassword,
|
|
79
|
+
SetupTotp,
|
|
80
|
+
SignIn,
|
|
81
|
+
SignUp,
|
|
82
|
+
VerifyUser,
|
|
83
|
+
} = components;
|
|
84
|
+
|
|
85
|
+
switch (route) {
|
|
86
|
+
case 'confirmResetPassword': {
|
|
87
|
+
return resolveConfirmResetPasswordRoute(
|
|
88
|
+
ConfirmResetPassword,
|
|
89
|
+
routeSelectorProps
|
|
90
|
+
);
|
|
91
|
+
}
|
|
92
|
+
case 'confirmSignIn': {
|
|
93
|
+
return resolveConfirmSignInRoute(ConfirmSignIn, routeSelectorProps);
|
|
94
|
+
}
|
|
95
|
+
case 'confirmSignUp': {
|
|
96
|
+
return resolveConfirmSignUpRoute(ConfirmSignUp, routeSelectorProps);
|
|
97
|
+
}
|
|
98
|
+
case 'confirmVerifyUser': {
|
|
99
|
+
return resolveConfirmVerifyUserRoute(
|
|
100
|
+
ConfirmVerifyUser,
|
|
101
|
+
routeSelectorProps
|
|
102
|
+
);
|
|
103
|
+
}
|
|
104
|
+
case 'forceNewPassword': {
|
|
105
|
+
return resolveForceNewPasswordRoute(ForceNewPassword, routeSelectorProps);
|
|
106
|
+
}
|
|
107
|
+
case 'forgotPassword': {
|
|
108
|
+
return resolveForgotPasswordRoute(ForgotPassword, routeSelectorProps);
|
|
109
|
+
}
|
|
110
|
+
case 'setupTotp': {
|
|
111
|
+
return resolveSetupTotpRoute(SetupTotp, routeSelectorProps);
|
|
112
|
+
}
|
|
113
|
+
case 'signIn': {
|
|
114
|
+
return resolveSignInRoute(SignIn, routeSelectorProps);
|
|
115
|
+
}
|
|
116
|
+
case 'signUp': {
|
|
117
|
+
return resolveSignUpRoute(SignUp, routeSelectorProps);
|
|
118
|
+
}
|
|
119
|
+
case 'verifyUser': {
|
|
120
|
+
return resolveVerifyUserRoute(VerifyUser, routeSelectorProps);
|
|
121
|
+
}
|
|
122
|
+
default: {
|
|
123
|
+
return resolveDefault();
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
}
|
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
import { AuthenticatorRoute } from '@aws-amplify/ui';
|
|
2
|
+
|
|
3
|
+
import { RenderNothing } from '../../../components';
|
|
4
|
+
import {
|
|
5
|
+
AuthenticatorMachineContext,
|
|
6
|
+
AuthenticatorMachineContextKey,
|
|
7
|
+
AuthenticatorRouteComponentKey,
|
|
8
|
+
DefaultComponentType,
|
|
9
|
+
DefaultPropsType,
|
|
10
|
+
Defaults,
|
|
11
|
+
} from '../types';
|
|
12
|
+
|
|
13
|
+
import {
|
|
14
|
+
UseAuthenticator,
|
|
15
|
+
UseAuthenticatorSelector,
|
|
16
|
+
} from '../useAuthenticator';
|
|
17
|
+
import { isComponentRouteKey } from '../utils';
|
|
18
|
+
import { MACHINE_PROP_KEYS, EVENT_HANDLER_KEY_MAP } from './constants';
|
|
19
|
+
import {
|
|
20
|
+
ConvertedMachineProps,
|
|
21
|
+
FormEventHandlerMachineKey,
|
|
22
|
+
FormEventHandlerPropKey,
|
|
23
|
+
UseAuthenticatorRoute,
|
|
24
|
+
UseAuthenticatorRouteDefault,
|
|
25
|
+
} from './types';
|
|
26
|
+
|
|
27
|
+
// only select `route` from machine context
|
|
28
|
+
export const routeSelector: UseAuthenticatorSelector = ({ route }) => [route];
|
|
29
|
+
|
|
30
|
+
const createSelector =
|
|
31
|
+
(selectorKeys: AuthenticatorMachineContextKey[]): UseAuthenticatorSelector =>
|
|
32
|
+
(context) => {
|
|
33
|
+
const dependencies = selectorKeys.map((key) => context[key]);
|
|
34
|
+
// route should always be part of deps, so hook knows when route changes.
|
|
35
|
+
return [...dependencies, context.route];
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
export const getRouteMachineSelector = (
|
|
39
|
+
route: AuthenticatorRoute
|
|
40
|
+
): UseAuthenticatorSelector =>
|
|
41
|
+
isComponentRouteKey(route)
|
|
42
|
+
? createSelector(MACHINE_PROP_KEYS[route])
|
|
43
|
+
: routeSelector;
|
|
44
|
+
|
|
45
|
+
const isFormEventHandlerKey = (
|
|
46
|
+
key: AuthenticatorMachineContextKey
|
|
47
|
+
): key is FormEventHandlerMachineKey =>
|
|
48
|
+
['updateBlur', 'updateForm', 'submitForm'].includes(key);
|
|
49
|
+
|
|
50
|
+
const convertEventHandlerKey = (
|
|
51
|
+
key: FormEventHandlerMachineKey
|
|
52
|
+
): FormEventHandlerPropKey => EVENT_HANDLER_KEY_MAP[key];
|
|
53
|
+
|
|
54
|
+
const getConvertedMachineProps = (
|
|
55
|
+
route: AuthenticatorRouteComponentKey,
|
|
56
|
+
context: AuthenticatorMachineContext
|
|
57
|
+
): ConvertedMachineProps =>
|
|
58
|
+
MACHINE_PROP_KEYS[route].reduce(
|
|
59
|
+
(acc, key) => ({
|
|
60
|
+
...acc,
|
|
61
|
+
[isFormEventHandlerKey(key) ? convertEventHandlerKey(key) : key]:
|
|
62
|
+
context[key],
|
|
63
|
+
}),
|
|
64
|
+
{} as ConvertedMachineProps
|
|
65
|
+
);
|
|
66
|
+
|
|
67
|
+
export function resolveConfirmResetPasswordRoute<FieldType = {}>(
|
|
68
|
+
Component: Defaults<FieldType>['ConfirmResetPassword'],
|
|
69
|
+
props: UseAuthenticator
|
|
70
|
+
): UseAuthenticatorRoute<'ConfirmResetPassword', FieldType> {
|
|
71
|
+
return {
|
|
72
|
+
Component,
|
|
73
|
+
props: {
|
|
74
|
+
...Component,
|
|
75
|
+
...getConvertedMachineProps('confirmResetPassword', props),
|
|
76
|
+
},
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export function resolveConfirmSignInRoute<FieldType = {}>(
|
|
81
|
+
Component: Defaults<FieldType>['ConfirmSignIn'],
|
|
82
|
+
props: UseAuthenticator
|
|
83
|
+
): UseAuthenticatorRoute<'ConfirmSignIn', FieldType> {
|
|
84
|
+
const { challengeName, ...machineProps } = getConvertedMachineProps(
|
|
85
|
+
'confirmSignIn',
|
|
86
|
+
props
|
|
87
|
+
);
|
|
88
|
+
|
|
89
|
+
return { Component, props: { ...Component, ...machineProps, challengeName } };
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
export function resolveConfirmSignUpRoute<FieldType = {}>(
|
|
93
|
+
Component: Defaults<FieldType>['ConfirmSignUp'],
|
|
94
|
+
props: UseAuthenticator
|
|
95
|
+
): UseAuthenticatorRoute<'ConfirmSignUp', FieldType> {
|
|
96
|
+
return {
|
|
97
|
+
Component,
|
|
98
|
+
props: {
|
|
99
|
+
...Component,
|
|
100
|
+
...getConvertedMachineProps('confirmSignUp', props),
|
|
101
|
+
},
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
export function resolveConfirmVerifyUserRoute<FieldType = {}>(
|
|
106
|
+
Component: Defaults<FieldType>['ConfirmVerifyUser'],
|
|
107
|
+
props: UseAuthenticator
|
|
108
|
+
): UseAuthenticatorRoute<'ConfirmVerifyUser', FieldType> {
|
|
109
|
+
return {
|
|
110
|
+
Component,
|
|
111
|
+
props: {
|
|
112
|
+
...Component,
|
|
113
|
+
...getConvertedMachineProps('confirmVerifyUser', props),
|
|
114
|
+
},
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
export function resolveForceNewPasswordRoute<FieldType = {}>(
|
|
119
|
+
Component: Defaults<FieldType>['ForceNewPassword'],
|
|
120
|
+
props: UseAuthenticator
|
|
121
|
+
): UseAuthenticatorRoute<'ForceNewPassword', FieldType> {
|
|
122
|
+
return {
|
|
123
|
+
Component,
|
|
124
|
+
props: {
|
|
125
|
+
...Component,
|
|
126
|
+
...getConvertedMachineProps('forceNewPassword', props),
|
|
127
|
+
},
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
export function resolveForgotPasswordRoute<FieldType = {}>(
|
|
132
|
+
Component: Defaults<FieldType>['ForgotPassword'],
|
|
133
|
+
props: UseAuthenticator
|
|
134
|
+
): UseAuthenticatorRoute<'ForgotPassword', FieldType> {
|
|
135
|
+
return {
|
|
136
|
+
Component,
|
|
137
|
+
props: {
|
|
138
|
+
...Component,
|
|
139
|
+
...getConvertedMachineProps('forgotPassword', props),
|
|
140
|
+
},
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
export function resolveSetupTotpRoute<FieldType = {}>(
|
|
145
|
+
Component: Defaults<FieldType>['SetupTotp'],
|
|
146
|
+
props: UseAuthenticator
|
|
147
|
+
): UseAuthenticatorRoute<'SetupTotp', FieldType> {
|
|
148
|
+
return {
|
|
149
|
+
Component,
|
|
150
|
+
props: {
|
|
151
|
+
...Component,
|
|
152
|
+
...getConvertedMachineProps('setupTotp', props),
|
|
153
|
+
},
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
export function resolveSignInRoute<FieldType = {}>(
|
|
158
|
+
Component: Defaults<FieldType>['SignIn'],
|
|
159
|
+
props: UseAuthenticator
|
|
160
|
+
): UseAuthenticatorRoute<'SignIn', FieldType> {
|
|
161
|
+
// default `hideSignUp` to false
|
|
162
|
+
const hideSignUp = false;
|
|
163
|
+
|
|
164
|
+
return {
|
|
165
|
+
Component,
|
|
166
|
+
props: {
|
|
167
|
+
...Component,
|
|
168
|
+
...getConvertedMachineProps('signIn', props),
|
|
169
|
+
hideSignUp,
|
|
170
|
+
},
|
|
171
|
+
};
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
export function resolveSignUpRoute<FieldType = {}>(
|
|
175
|
+
Component: Defaults<FieldType>['SignUp'],
|
|
176
|
+
props: UseAuthenticator
|
|
177
|
+
): UseAuthenticatorRoute<'SignUp', FieldType> {
|
|
178
|
+
return {
|
|
179
|
+
Component,
|
|
180
|
+
props: { ...Component, ...getConvertedMachineProps('signUp', props) },
|
|
181
|
+
};
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
export function resolveVerifyUserRoute<FieldType = {}>(
|
|
185
|
+
Component: Defaults<FieldType>['VerifyUser'],
|
|
186
|
+
props: UseAuthenticator
|
|
187
|
+
): UseAuthenticatorRoute<'VerifyUser', FieldType> {
|
|
188
|
+
return {
|
|
189
|
+
Component,
|
|
190
|
+
props: {
|
|
191
|
+
...Component,
|
|
192
|
+
...getConvertedMachineProps('verifyUser', props),
|
|
193
|
+
},
|
|
194
|
+
};
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
export function resolveDefault<
|
|
198
|
+
FieldType = {},
|
|
199
|
+
>(): UseAuthenticatorRouteDefault<FieldType> {
|
|
200
|
+
return {
|
|
201
|
+
Component: RenderNothing as DefaultComponentType<FieldType>,
|
|
202
|
+
props: {} as DefaultPropsType<FieldType>,
|
|
203
|
+
};
|
|
204
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { AuthenticatorRoute } from '@aws-amplify/ui';
|
|
2
|
+
|
|
3
|
+
import { COMPONENT_ROUTE_KEYS, COMPONENT_ROUTE_NAMES } from './constants';
|
|
4
|
+
import { AuthenticatorRouteComponentKey, Defaults, Overrides } from './types';
|
|
5
|
+
|
|
6
|
+
export const isComponentRouteKey = (
|
|
7
|
+
route: AuthenticatorRoute
|
|
8
|
+
): route is AuthenticatorRouteComponentKey =>
|
|
9
|
+
COMPONENT_ROUTE_KEYS.some((componentRoute) => componentRoute === route);
|
|
10
|
+
|
|
11
|
+
export function resolveAuthenticatorComponents<FieldType>(
|
|
12
|
+
defaults: Defaults<FieldType>,
|
|
13
|
+
overrides?: Overrides<FieldType>
|
|
14
|
+
): Defaults<FieldType> {
|
|
15
|
+
if (!overrides) {
|
|
16
|
+
return defaults;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
return COMPONENT_ROUTE_NAMES.reduce((components, route) => {
|
|
20
|
+
const Default = defaults[route];
|
|
21
|
+
const Override = overrides[route];
|
|
22
|
+
|
|
23
|
+
if (typeof Override !== 'function') {
|
|
24
|
+
return { ...components, [route]: Default };
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const { Footer, FormFields, Header } = Default;
|
|
28
|
+
|
|
29
|
+
// cast to allow assigning of component slots
|
|
30
|
+
const Component = Override as typeof Default;
|
|
31
|
+
|
|
32
|
+
Component.Footer = Footer;
|
|
33
|
+
Component.FormFields = FormFields;
|
|
34
|
+
Component.Header = Header;
|
|
35
|
+
|
|
36
|
+
return { ...components, [route]: Component };
|
|
37
|
+
}, {} as Defaults<FieldType>);
|
|
38
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
export { AuthenticatorProvider, AuthenticatorContext } from './context';
|
|
2
|
+
export {
|
|
3
|
+
resolveAuthenticatorComponents,
|
|
4
|
+
useAuthenticator,
|
|
5
|
+
useAuthenticatorRoute,
|
|
6
|
+
UseAuthenticator,
|
|
7
|
+
useAuthenticatorInitMachine,
|
|
8
|
+
UseAuthenticatorRoute,
|
|
9
|
+
} from './hooks';
|
|
10
|
+
export {
|
|
11
|
+
Overrides as AuthenticatorComponentOverrides,
|
|
12
|
+
Defaults as AuthenticatorComponentDefaults,
|
|
13
|
+
DefaultProps as AuthenticatorComponentDefaultProps,
|
|
14
|
+
FooterComponent as AuthenticatorFooterComponent,
|
|
15
|
+
FormFieldsComponent as AuthenticatorFormFieldsComponent,
|
|
16
|
+
HeaderComponent as AuthenticatorHeaderComponent,
|
|
17
|
+
isComponentRouteKey as isAuthenticatorComponentRouteKey,
|
|
18
|
+
AuthenticatorRouteComponentKey,
|
|
19
|
+
AuthenticatorRouteComponentName,
|
|
20
|
+
AuthenticatorLegacyField,
|
|
21
|
+
AuthenticatorMachineContext,
|
|
22
|
+
FormFieldsComponent,
|
|
23
|
+
} from './hooks';
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import {
|
|
3
|
+
useForm as useReactHookForm,
|
|
4
|
+
FormProvider as ReactHookFormProvider,
|
|
5
|
+
} from 'react-hook-form';
|
|
6
|
+
|
|
7
|
+
import { FormValues, FormHandle, FormProviderProps } from './types';
|
|
8
|
+
|
|
9
|
+
const DEFAULT_MODE = 'onTouched';
|
|
10
|
+
|
|
11
|
+
const FormProvider = React.forwardRef<
|
|
12
|
+
FormHandle,
|
|
13
|
+
FormProviderProps<FormValues>
|
|
14
|
+
>(function FormProvider<Values extends FormValues>(
|
|
15
|
+
{ children, defaultValues, mode = DEFAULT_MODE }: FormProviderProps<Values>,
|
|
16
|
+
ref: React.ForwardedRef<FormHandle<Values>>
|
|
17
|
+
) {
|
|
18
|
+
const formProviderProps = useReactHookForm<Values>({
|
|
19
|
+
defaultValues,
|
|
20
|
+
mode,
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
const { getValues, reset } = formProviderProps;
|
|
24
|
+
React.useImperativeHandle(
|
|
25
|
+
ref,
|
|
26
|
+
() => ({ getValues, reset: () => reset(defaultValues) }),
|
|
27
|
+
[defaultValues, getValues, reset]
|
|
28
|
+
);
|
|
29
|
+
|
|
30
|
+
return (
|
|
31
|
+
<ReactHookFormProvider {...formProviderProps}>
|
|
32
|
+
{children}
|
|
33
|
+
</ReactHookFormProvider>
|
|
34
|
+
);
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
export default FormProvider;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export {
|
|
2
|
+
FormProviderProps,
|
|
3
|
+
FormValues,
|
|
4
|
+
FormHandle,
|
|
5
|
+
SubmitHandler,
|
|
6
|
+
UseForm,
|
|
7
|
+
Validate,
|
|
8
|
+
Validator,
|
|
9
|
+
} from './types';
|
|
10
|
+
export { default as useField } from './useField';
|
|
11
|
+
export { default as useForm } from './useForm';
|
|
12
|
+
export { default as FormProvider } from './FormProvider';
|
|
13
|
+
export { default as withFormProvider } from './withFormProvider';
|