@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
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { AuthenticatorMachineContext, AuthenticatorMachineContextKey, AuthenticatorRouteComponentName, CommonRouteProps, ConfirmResetPasswordBaseProps, ConfirmSignInBaseProps, ConfirmSignUpBaseProps, Defaults, DefaultProps, ForceResetPasswordBaseProps, ResetPasswordBaseProps,
|
|
1
|
+
import { AuthenticatorMachineContext, AuthenticatorMachineContextKey, AuthenticatorRouteComponentName, CommonRouteProps, ConfirmResetPasswordBaseProps, ConfirmSignInBaseProps, ConfirmSignUpBaseProps, Defaults, DefaultProps, ForceResetPasswordBaseProps, ResetPasswordBaseProps, SetupTotpBaseProps, SignInBaseProps, SignUpBaseProps, VerifyUserProps, ConfirmVerifyUserProps } from '../types';
|
|
2
2
|
export type UseAuthenticatorRouteParams<FieldType> = {
|
|
3
3
|
components: Defaults<FieldType>;
|
|
4
4
|
};
|
|
@@ -18,12 +18,12 @@ export type CommonRouteMachineKey = ExtractMachineKey<CommonRouteProps> | FormEv
|
|
|
18
18
|
* `route` sub-component machine selector key types
|
|
19
19
|
*/
|
|
20
20
|
export type ConfirmResetPasswordMachineKey = ExtractMachineKey<ConfirmResetPasswordBaseProps> | CommonRouteMachineKey;
|
|
21
|
-
export type ConfirmSignInMachineKey = ExtractMachineKey<ConfirmSignInBaseProps> | CommonRouteMachineKey
|
|
21
|
+
export type ConfirmSignInMachineKey = ExtractMachineKey<ConfirmSignInBaseProps> | CommonRouteMachineKey;
|
|
22
22
|
export type ConfirmSignUpMachineKey = ExtractMachineKey<ConfirmSignUpBaseProps> | CommonRouteMachineKey;
|
|
23
23
|
export type ConfirmVerifyUserMachineKey = ExtractMachineKey<ConfirmVerifyUserProps> | CommonRouteMachineKey;
|
|
24
24
|
export type ForceNewPasswordMachineKey = ExtractMachineKey<ForceResetPasswordBaseProps> | CommonRouteMachineKey;
|
|
25
25
|
export type ResetPasswordMachineKey = ExtractMachineKey<ResetPasswordBaseProps> | CommonRouteMachineKey;
|
|
26
|
-
export type
|
|
26
|
+
export type SetupTotpMachineKey = ExtractMachineKey<SetupTotpBaseProps> | CommonRouteMachineKey;
|
|
27
27
|
export type SignInMachineKey = ExtractMachineKey<SignInBaseProps> | CommonRouteMachineKey;
|
|
28
28
|
export type SignUpMachineKey = ExtractMachineKey<SignUpBaseProps> | CommonRouteMachineKey;
|
|
29
29
|
export type VerifyUserMachineKey = ExtractMachineKey<VerifyUserProps> | CommonRouteMachineKey;
|
|
@@ -4,8 +4,8 @@ export default function useAuthenticatorRoute<FieldType>(params: UseAuthenticato
|
|
|
4
4
|
export default function useAuthenticatorRoute<FieldType>(params: UseAuthenticatorRouteParams<FieldType>): UseAuthenticatorRoute<'ConfirmSignUp'>;
|
|
5
5
|
export default function useAuthenticatorRoute<FieldType>(params: UseAuthenticatorRouteParams<FieldType>): UseAuthenticatorRoute<'ConfirmVerifyUser'>;
|
|
6
6
|
export default function useAuthenticatorRoute<FieldType>(params: UseAuthenticatorRouteParams<FieldType>): UseAuthenticatorRoute<'ForceNewPassword'>;
|
|
7
|
-
export default function useAuthenticatorRoute<FieldType>(params: UseAuthenticatorRouteParams<FieldType>): UseAuthenticatorRoute<'
|
|
8
|
-
export default function useAuthenticatorRoute<FieldType>(params: UseAuthenticatorRouteParams<FieldType>): UseAuthenticatorRoute<'
|
|
7
|
+
export default function useAuthenticatorRoute<FieldType>(params: UseAuthenticatorRouteParams<FieldType>): UseAuthenticatorRoute<'ForgotPassword'>;
|
|
8
|
+
export default function useAuthenticatorRoute<FieldType>(params: UseAuthenticatorRouteParams<FieldType>): UseAuthenticatorRoute<'SetupTotp'>;
|
|
9
9
|
export default function useAuthenticatorRoute<FieldType>(params: UseAuthenticatorRouteParams<FieldType>): UseAuthenticatorRoute<'SignIn'>;
|
|
10
10
|
export default function useAuthenticatorRoute<FieldType>(params: UseAuthenticatorRouteParams<FieldType>): UseAuthenticatorRoute<'SignUp'>;
|
|
11
11
|
export default function useAuthenticatorRoute<FieldType>(params: UseAuthenticatorRouteParams<FieldType>): UseAuthenticatorRoute<'VerifyUser'>;
|
|
@@ -9,8 +9,8 @@ export declare function resolveConfirmSignInRoute<FieldType = {}>(Component: Def
|
|
|
9
9
|
export declare function resolveConfirmSignUpRoute<FieldType = {}>(Component: Defaults<FieldType>['ConfirmSignUp'], props: UseAuthenticator): UseAuthenticatorRoute<'ConfirmSignUp', FieldType>;
|
|
10
10
|
export declare function resolveConfirmVerifyUserRoute<FieldType = {}>(Component: Defaults<FieldType>['ConfirmVerifyUser'], props: UseAuthenticator): UseAuthenticatorRoute<'ConfirmVerifyUser', FieldType>;
|
|
11
11
|
export declare function resolveForceNewPasswordRoute<FieldType = {}>(Component: Defaults<FieldType>['ForceNewPassword'], props: UseAuthenticator): UseAuthenticatorRoute<'ForceNewPassword', FieldType>;
|
|
12
|
-
export declare function
|
|
13
|
-
export declare function
|
|
12
|
+
export declare function resolveForgotPasswordRoute<FieldType = {}>(Component: Defaults<FieldType>['ForgotPassword'], props: UseAuthenticator): UseAuthenticatorRoute<'ForgotPassword', FieldType>;
|
|
13
|
+
export declare function resolveSetupTotpRoute<FieldType = {}>(Component: Defaults<FieldType>['SetupTotp'], props: UseAuthenticator): UseAuthenticatorRoute<'SetupTotp', FieldType>;
|
|
14
14
|
export declare function resolveSignInRoute<FieldType = {}>(Component: Defaults<FieldType>['SignIn'], props: UseAuthenticator): UseAuthenticatorRoute<'SignIn', FieldType>;
|
|
15
15
|
export declare function resolveSignUpRoute<FieldType = {}>(Component: Defaults<FieldType>['SignUp'], props: UseAuthenticator): UseAuthenticatorRoute<'SignUp', FieldType>;
|
|
16
16
|
export declare function resolveVerifyUserRoute<FieldType = {}>(Component: Defaults<FieldType>['VerifyUser'], props: UseAuthenticator): UseAuthenticatorRoute<'VerifyUser', FieldType>;
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export { FormProviderProps, FormValues, FormHandle, SubmitHandler, UseForm, Validate, Validator, } from './types';
|
|
2
|
+
export { default as useField } from './useField';
|
|
3
|
+
export { default as useForm } from './useForm';
|
|
4
|
+
export { default as FormProvider } from './FormProvider';
|
|
5
|
+
export { default as withFormProvider } from './withFormProvider';
|
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { DefaultValues } from 'react-hook-form';
|
|
3
|
+
export interface FormValues {
|
|
4
|
+
[k: string]: string;
|
|
5
|
+
}
|
|
6
|
+
export type FocusHandler = (event?: any) => void;
|
|
7
|
+
export type ChangeHandler = (event?: any) => void;
|
|
8
|
+
export type SubmitHandler<Values extends FormValues = FormValues> = (values: Values, event?: React.BaseSyntheticEvent) => void;
|
|
9
|
+
type RefCallback = (node: any) => void;
|
|
10
|
+
export interface SetFormValueParams<Name extends string = string> {
|
|
11
|
+
/**
|
|
12
|
+
* `Form` value `name`
|
|
13
|
+
*/
|
|
14
|
+
name: Name;
|
|
15
|
+
/**
|
|
16
|
+
* `value` to be applied
|
|
17
|
+
*/
|
|
18
|
+
value: string;
|
|
19
|
+
/**
|
|
20
|
+
* Whether validation should be ran
|
|
21
|
+
*/
|
|
22
|
+
shouldValidate?: boolean;
|
|
23
|
+
}
|
|
24
|
+
type SetFormValue<Name extends string = string> = (params: SetFormValueParams<Name>) => void;
|
|
25
|
+
/**
|
|
26
|
+
* `Field` validator callback
|
|
27
|
+
*
|
|
28
|
+
* @param value current `Field` value`
|
|
29
|
+
* @param values current `Form` values
|
|
30
|
+
* @returns `string` message on invalid `Field`, `undefined` on valid
|
|
31
|
+
*/
|
|
32
|
+
export type Validator<Values extends FormValues = FormValues> = (value: string, values: Values) => string | undefined;
|
|
33
|
+
export type Validate<Values extends FormValues = FormValues> = Validator<Values> | Record<string, Validator<Values>>;
|
|
34
|
+
export interface RegisterFieldParams<Values extends FormValues = FormValues> {
|
|
35
|
+
disabled?: boolean;
|
|
36
|
+
name: string;
|
|
37
|
+
onBlur?: FocusHandler;
|
|
38
|
+
onChange?: ChangeHandler;
|
|
39
|
+
validate?: Validate<Values>;
|
|
40
|
+
value?: string;
|
|
41
|
+
}
|
|
42
|
+
export interface UseFormParams<Values extends FormValues = FormValues, OnSubmit extends SubmitHandler<Values> = SubmitHandler<Values>> {
|
|
43
|
+
/**
|
|
44
|
+
* Custom error message provided to `Error` on call to `useForm` outside `FormProvider`
|
|
45
|
+
*/
|
|
46
|
+
errorMessage?: string;
|
|
47
|
+
/**
|
|
48
|
+
* Submit event handler
|
|
49
|
+
*/
|
|
50
|
+
onSubmit?: OnSubmit;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* `Form` aware `Field` props to be provided to UI components
|
|
54
|
+
*/
|
|
55
|
+
interface RegisterProps<Name extends string = string> {
|
|
56
|
+
disabled?: boolean;
|
|
57
|
+
name: Name;
|
|
58
|
+
onBlur: FocusHandler;
|
|
59
|
+
onChange: ChangeHandler;
|
|
60
|
+
ref: RefCallback;
|
|
61
|
+
}
|
|
62
|
+
interface FieldState {
|
|
63
|
+
errorMessage: string | undefined;
|
|
64
|
+
hasError: boolean;
|
|
65
|
+
invalid: boolean;
|
|
66
|
+
isDirty: boolean;
|
|
67
|
+
isTouched: boolean;
|
|
68
|
+
}
|
|
69
|
+
export type UseForm<Values extends FormValues = FormValues> = {
|
|
70
|
+
/**
|
|
71
|
+
* Get `Field` state of `name`
|
|
72
|
+
*/
|
|
73
|
+
getFieldState: (name: string) => FieldState;
|
|
74
|
+
/**
|
|
75
|
+
* Get current `Form` values
|
|
76
|
+
*/
|
|
77
|
+
getValues: () => FormValues;
|
|
78
|
+
/**
|
|
79
|
+
* `Form` validity state
|
|
80
|
+
*/
|
|
81
|
+
isValid: boolean;
|
|
82
|
+
/**
|
|
83
|
+
* Registers `name` to `Form` values, returns `Form` aware `Field` handlers and `ref`
|
|
84
|
+
*/
|
|
85
|
+
registerField: (params: RegisterFieldParams) => RegisterProps;
|
|
86
|
+
/**
|
|
87
|
+
* Reset `Form` values
|
|
88
|
+
*/
|
|
89
|
+
reset: (values: Values) => void;
|
|
90
|
+
/**
|
|
91
|
+
* Sets `Form` value of `name` param to provided `value`
|
|
92
|
+
*/
|
|
93
|
+
setFormValue: SetFormValue;
|
|
94
|
+
/**
|
|
95
|
+
* `Form` submit callback prop
|
|
96
|
+
*
|
|
97
|
+
* @usage
|
|
98
|
+
* ```tsx
|
|
99
|
+
* const { onSubmit } = useForm();
|
|
100
|
+
*
|
|
101
|
+
* <form onSubmit={onSubmit} />
|
|
102
|
+
* ```
|
|
103
|
+
*/
|
|
104
|
+
onSubmit: (e?: React.BaseSyntheticEvent) => void;
|
|
105
|
+
};
|
|
106
|
+
export type FormHandle<Values extends FormValues = FormValues> = {
|
|
107
|
+
/**
|
|
108
|
+
* Get current `Form` values
|
|
109
|
+
*/
|
|
110
|
+
getValues: () => Values;
|
|
111
|
+
/**
|
|
112
|
+
* Reset `Form` values to default values
|
|
113
|
+
*/
|
|
114
|
+
reset: () => void;
|
|
115
|
+
};
|
|
116
|
+
type ValidationMode = 'onBlur' | 'onChange' | 'onSubmit' | 'onTouched' | 'all';
|
|
117
|
+
export interface FormProviderProps<Values extends FormValues = FormValues> {
|
|
118
|
+
/**
|
|
119
|
+
* `Form` children
|
|
120
|
+
*/
|
|
121
|
+
children?: React.ReactNode;
|
|
122
|
+
/**
|
|
123
|
+
* `Form` default values
|
|
124
|
+
*/
|
|
125
|
+
defaultValues?: DefaultValues<Values>;
|
|
126
|
+
/**
|
|
127
|
+
* Validation mode applied to `Field` components:
|
|
128
|
+
*
|
|
129
|
+
* - `onTouched`: Validate on first blur event and every subsequent change event
|
|
130
|
+
* - `onBlur`: validate on blur events only
|
|
131
|
+
* - `onChange`: validate on change events, non-performant
|
|
132
|
+
* - `all`: validate on blur and change events
|
|
133
|
+
*
|
|
134
|
+
* @default
|
|
135
|
+
* "onTouched"
|
|
136
|
+
*/
|
|
137
|
+
mode?: ValidationMode;
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* For use with React only.
|
|
141
|
+
*/
|
|
142
|
+
export interface UseField<Name extends string = string> extends FieldState, RegisterProps {
|
|
143
|
+
name: Name;
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* For use with React only.
|
|
147
|
+
*/
|
|
148
|
+
export interface UseFieldParams<OnBlur extends FocusHandler | undefined, OnChange extends ChangeHandler | undefined, Values extends FormValues = FormValues> {
|
|
149
|
+
/**
|
|
150
|
+
* Controlled `disabled` state
|
|
151
|
+
*/
|
|
152
|
+
disabled?: boolean;
|
|
153
|
+
/**
|
|
154
|
+
* Controlled blur event handler
|
|
155
|
+
*/
|
|
156
|
+
onBlur?: OnBlur;
|
|
157
|
+
/**
|
|
158
|
+
* Controlled change event handler
|
|
159
|
+
*/
|
|
160
|
+
onChange?: OnChange;
|
|
161
|
+
/**
|
|
162
|
+
* `Field` name, must be unique
|
|
163
|
+
*/
|
|
164
|
+
name: string;
|
|
165
|
+
/**
|
|
166
|
+
* `Field` validation handler. Accepts a callback function or an
|
|
167
|
+
* object of callback functions to validate against
|
|
168
|
+
*
|
|
169
|
+
* @usage
|
|
170
|
+
* ```tsx
|
|
171
|
+
*
|
|
172
|
+
* // validate `password` and `confim_pasword` values match
|
|
173
|
+
* const validate = (value: string, values: Values) =>
|
|
174
|
+
* value === values['confirm_password']
|
|
175
|
+
* ? undefined
|
|
176
|
+
* : 'Passwords must match!';
|
|
177
|
+
*
|
|
178
|
+
*
|
|
179
|
+
* const passwordProps = useField({ name: 'password' });
|
|
180
|
+
*
|
|
181
|
+
* const confirmPasswordProps = useField({
|
|
182
|
+
* name: 'confirm_password',
|
|
183
|
+
* validate
|
|
184
|
+
* });
|
|
185
|
+
*
|
|
186
|
+
* return (
|
|
187
|
+
* <>
|
|
188
|
+
* <TextField {...passwordProps} />
|
|
189
|
+
* <TextField {...confirmPasswordProps} />
|
|
190
|
+
* </>
|
|
191
|
+
* )
|
|
192
|
+
* ```
|
|
193
|
+
*/
|
|
194
|
+
validate?: Validate<Values>;
|
|
195
|
+
}
|
|
196
|
+
/**
|
|
197
|
+
* For use with React Native only.
|
|
198
|
+
*/
|
|
199
|
+
export interface UseControlledFieldParams<OnBlur extends FocusHandler | undefined, Values extends FormValues = FormValues> extends Omit<UseFieldParams<OnBlur, undefined, Values>, 'onChange'> {
|
|
200
|
+
/**
|
|
201
|
+
* Controlled text change event handler
|
|
202
|
+
*/
|
|
203
|
+
onChangeText?: (value: string) => void;
|
|
204
|
+
}
|
|
205
|
+
/**
|
|
206
|
+
* For use with React Native only.
|
|
207
|
+
*/
|
|
208
|
+
export interface UseControlledField<Name extends string = string> extends Omit<UseField<Name>, 'onChange'> {
|
|
209
|
+
name: Name;
|
|
210
|
+
/**
|
|
211
|
+
* React Native only.
|
|
212
|
+
* Controlled text change event handler
|
|
213
|
+
*/
|
|
214
|
+
onChangeText: (value: string) => void;
|
|
215
|
+
value: string;
|
|
216
|
+
}
|
|
217
|
+
export {};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { FocusHandler, UseControlledField, UseControlledFieldParams } from './types';
|
|
2
|
+
export declare const DEFAULT_ERROR_MESSAGE = "`useControlledField` must be used within a `FormProvider`";
|
|
3
|
+
/**
|
|
4
|
+
* `Field` integration hook for usage with React Native `Field` components
|
|
5
|
+
*
|
|
6
|
+
* @param params Requires `name`, all additional params optional
|
|
7
|
+
* @returns `Form` aware `Field` handlers and state values
|
|
8
|
+
*/
|
|
9
|
+
export default function useControlledField<OnBlur extends FocusHandler>({ onBlur: _onBlur, onChangeText: _onChangeText, ...rest }: UseControlledFieldParams<OnBlur>): UseControlledField;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { ChangeHandler, FocusHandler, UseField, UseFieldParams } from './types';
|
|
2
|
+
export declare const DEFAULT_ERROR_MESSAGE = "`useField` must be used within a `FormProvider`";
|
|
3
|
+
/**
|
|
4
|
+
* `Field` integration hook for usage with React `Field` components.
|
|
5
|
+
*
|
|
6
|
+
* @param params Requires `name`, all additional params optional
|
|
7
|
+
* @returns `Form` aware `Field` event handlers and state values
|
|
8
|
+
*/
|
|
9
|
+
export default function useField<OnBlur extends FocusHandler, OnChange extends ChangeHandler>(params: UseFieldParams<OnBlur, OnChange>): UseField;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { FormValues, SubmitHandler, UseForm, UseFormParams } from './types';
|
|
2
|
+
export declare const DEFAULT_ERROR_MESSAGE = "`useForm` must be called inside a `FormProvider`";
|
|
3
|
+
/**
|
|
4
|
+
* Utility hook corresponding to `FormProvider`, must be used within a `FormProvider`
|
|
5
|
+
*
|
|
6
|
+
* @internal Extend for public export. `useForm` and `UseForm` are an abstraction layer
|
|
7
|
+
* on top of `useFormContext` and `UseFormReturn`, imported from `react-hook-form`
|
|
8
|
+
*
|
|
9
|
+
* @param options optional parameters
|
|
10
|
+
* @returns `Form` utilities
|
|
11
|
+
*/
|
|
12
|
+
export default function useForm<Values extends FormValues = FormValues, OnSubmit extends SubmitHandler = SubmitHandler>(options?: UseFormParams<Values, OnSubmit>): UseForm<Values>;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { AnyComponent, MergeProps } from '../../types';
|
|
3
|
+
import { FormHandle, FormProviderProps } from './types';
|
|
4
|
+
/**
|
|
5
|
+
* @param Child `Form` base component wrapped inside `FormProvider`
|
|
6
|
+
* @returns Composed `Form` component exposing `FormContext` values to descendents
|
|
7
|
+
*/
|
|
8
|
+
export default function withFormProvider<ChildComp extends AnyComponent, ChildProps extends React.ComponentPropsWithRef<ChildComp>, Props extends MergeProps<FormProviderProps, ChildProps>>(Child: ChildComp): React.ForwardRefExoticComponent<React.PropsWithoutRef<Props> & React.RefAttributes<FormHandle>>;
|
|
@@ -1,3 +1,5 @@
|
|
|
1
1
|
export { default as useDeprecationWarning, UseDeprecationWarning, } from './useDeprecationWarning';
|
|
2
2
|
export { default as useHasValueUpdated } from './useHasValueUpdated';
|
|
3
3
|
export { default as usePreviousValue } from './usePreviousValue';
|
|
4
|
+
export { default as useSetUserAgent } from './useSetUserAgent';
|
|
5
|
+
export { default as useTimeout } from './useTimeout';
|
package/dist/types/index.d.ts
CHANGED
|
@@ -1,4 +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
|
-
export { RenderNothing } from './components';
|
|
3
|
-
export { useDeprecationWarning, UseDeprecationWarning, useHasValueUpdated, usePreviousValue, } from './hooks';
|
|
2
|
+
export { FormProvider, FormProviderProps, RenderNothing, FormValues, FormHandle, useField, useForm, UseForm, Validate, Validator, withFormProvider, } from './components';
|
|
3
|
+
export { useDeprecationWarning, UseDeprecationWarning, useHasValueUpdated, usePreviousValue, useSetUserAgent, useTimeout, } from './hooks';
|
|
4
4
|
export { MergeProps } from './types';
|
|
5
|
+
export { createContextUtilities } from './utils';
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export { MergeProps } from './types';
|
|
1
|
+
export { AnyComponent, MergeProps } from './types';
|
|
@@ -1,34 +1,40 @@
|
|
|
1
1
|
import React from 'react';
|
|
2
2
|
export declare const INVALID_OPTIONS_MESSAGE = "an `errorMessage` or a `defaultValue` must be provided in `options`";
|
|
3
|
-
|
|
4
|
-
contextName:
|
|
5
|
-
} & (// exclude `errorMessage` if `defaultValue` is present
|
|
6
|
-
{
|
|
3
|
+
interface OptionsWithDefaultValue<Name, ContextType> {
|
|
4
|
+
contextName: Name;
|
|
7
5
|
defaultValue: ContextType;
|
|
8
6
|
errorMessage?: never;
|
|
9
|
-
}
|
|
10
|
-
|
|
7
|
+
}
|
|
8
|
+
interface OptionsWithErrorMessage<Name, ErrorMessage> {
|
|
9
|
+
contextName: Name;
|
|
10
|
+
errorMessage: ErrorMessage;
|
|
11
11
|
defaultValue?: never;
|
|
12
|
-
}
|
|
12
|
+
}
|
|
13
|
+
type ContextOptions<Type, Name extends string, Message> = OptionsWithDefaultValue<Name, Type> | OptionsWithErrorMessage<Name, Message>;
|
|
14
|
+
type HookParams = {
|
|
15
|
+
errorMessage?: string;
|
|
16
|
+
};
|
|
13
17
|
type UtilityKey<ContextName extends string> = `${ContextName}Provider` | `use${ContextName}` | `${ContextName}Context`;
|
|
14
18
|
type CreateContextUtilitiesReturn<ContextType, ContextName extends string> = {
|
|
15
|
-
[Key in UtilityKey<ContextName>]: Key extends `${string}Provider` ? React.ComponentType<React.PropsWithChildren<
|
|
19
|
+
[Key in UtilityKey<ContextName>]: Key extends `${string}Provider` ? React.ComponentType<React.PropsWithChildren<ContextType>> : Key extends `use${string}` ? (params?: HookParams) => ContextType : Key extends `${string}Context` ? React.Context<ContextType | undefined> : never;
|
|
16
20
|
};
|
|
17
21
|
/**
|
|
18
|
-
*
|
|
19
|
-
* -
|
|
20
|
-
* - `Provider`: React Context `Provider` component exposing the `ContextType`
|
|
22
|
+
* Uses `ContextType`/`Name` generics and `options` to create:
|
|
23
|
+
* - `${Name}Context`: React Context of type `ContextType`
|
|
24
|
+
* - `Provider${Name}`: React Context `Provider` component exposing the `ContextType`
|
|
21
25
|
* as optional props
|
|
22
|
-
* - `
|
|
26
|
+
* - `use${Name}`: Utility Hook exposing the values of `ContextType`. Allows
|
|
27
|
+
* params with `errorMessage` for granular error messaging
|
|
23
28
|
*
|
|
24
29
|
* @template ContextType Type definition of the Context.
|
|
25
30
|
* > For most use cases the keys of `ContextType` should not be optional in
|
|
26
31
|
* preference of explicit `undefined` to avoid optional types on the
|
|
27
32
|
* Utility Hook return
|
|
28
33
|
*
|
|
29
|
-
* @param options Context utility options. Requires a `contextName`, and
|
|
30
|
-
* a `defaultValue` of `ContextType` or `errorMessage`
|
|
31
|
-
* behaviors of the Utility Hook when used outside a
|
|
34
|
+
* @param options Context utility options. Requires a `contextName`, and
|
|
35
|
+
* either a `defaultValue` of `ContextType` **or** an `errorMessage`
|
|
36
|
+
* allowing for differing behaviors of the Utility Hook when used outside a
|
|
37
|
+
* parent `Provider`:
|
|
32
38
|
*
|
|
33
39
|
* - `defaultValue`: Ensures the Utility Hook returns a default value for
|
|
34
40
|
* scenarios **where the missing context values should not impact usage**
|
|
@@ -44,9 +50,11 @@ type CreateContextUtilitiesReturn<ContextType, ContextName extends string> = {
|
|
|
44
50
|
* }
|
|
45
51
|
*
|
|
46
52
|
* // with `defaultValue`
|
|
47
|
-
* const {
|
|
53
|
+
* const defaultValue: StuffContextType = { things: 7 };
|
|
54
|
+
*
|
|
55
|
+
* const { StuffProvider, useStuff } = createContextUtilities({
|
|
48
56
|
* contextName: 'Stuff',
|
|
49
|
-
* defaultValue
|
|
57
|
+
* defaultValue,
|
|
50
58
|
* });
|
|
51
59
|
*
|
|
52
60
|
* // with `errorMessage`
|
|
@@ -56,5 +64,5 @@ type CreateContextUtilitiesReturn<ContextType, ContextName extends string> = {
|
|
|
56
64
|
* });
|
|
57
65
|
* ```
|
|
58
66
|
*/
|
|
59
|
-
export default function createContextUtilities<ContextType, ContextName extends string = string>(options: ContextOptions<ContextType, ContextName>): CreateContextUtilitiesReturn<ContextType, ContextName>;
|
|
67
|
+
export default function createContextUtilities<ContextType, ContextName extends string = string, Message extends string | undefined = string | undefined>(options: ContextOptions<ContextType, ContextName, Message>): CreateContextUtilitiesReturn<ContextType, ContextName>;
|
|
60
68
|
export {};
|
package/package.json
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aws-amplify/ui-react-core",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "3.0.0",
|
|
4
4
|
"main": "dist/index.js",
|
|
5
5
|
"module": "dist/esm/index.mjs",
|
|
6
|
-
"react-native": "
|
|
6
|
+
"react-native": "src/index.ts",
|
|
7
7
|
"exports": {
|
|
8
8
|
".": {
|
|
9
9
|
"import": "./dist/esm/index.mjs",
|
|
@@ -16,7 +16,10 @@
|
|
|
16
16
|
"license": "Apache-2.0",
|
|
17
17
|
"files": [
|
|
18
18
|
"dist",
|
|
19
|
-
"LICENSE"
|
|
19
|
+
"LICENSE",
|
|
20
|
+
"src",
|
|
21
|
+
"!**/__mocks__",
|
|
22
|
+
"!**/__tests__"
|
|
20
23
|
],
|
|
21
24
|
"scripts": {
|
|
22
25
|
"clean": "rimraf dist node_modules",
|
|
@@ -26,40 +29,19 @@
|
|
|
26
29
|
"lint": "yarn typecheck && eslint src",
|
|
27
30
|
"prebuild": "rimraf dist",
|
|
28
31
|
"test": "jest",
|
|
29
|
-
"test:ci": "yarn test && yarn check:esm",
|
|
30
32
|
"test:watch": "yarn test --watch",
|
|
31
33
|
"typecheck": "tsc --noEmit"
|
|
32
34
|
},
|
|
33
35
|
"dependencies": {
|
|
34
|
-
"@aws-amplify/ui": "
|
|
35
|
-
"@xstate/react": "3.
|
|
36
|
+
"@aws-amplify/ui": "6.0.0",
|
|
37
|
+
"@xstate/react": "^3.2.2",
|
|
36
38
|
"lodash": "4.17.21",
|
|
39
|
+
"react-hook-form": "^7.43.5",
|
|
37
40
|
"xstate": "^4.33.6"
|
|
38
41
|
},
|
|
39
42
|
"peerDependencies": {
|
|
40
|
-
"aws-amplify": "^
|
|
41
|
-
"react": "
|
|
42
|
-
},
|
|
43
|
-
"devDependencies": {
|
|
44
|
-
"@aws-amplify/eslint-config-amplify-ui": "0.0.0",
|
|
45
|
-
"@rollup/plugin-typescript": "^8.3.1",
|
|
46
|
-
"@testing-library/react": "^12.0.0",
|
|
47
|
-
"@testing-library/react-hooks": "^7.0.2",
|
|
48
|
-
"@types/jest": "^26.0.23",
|
|
49
|
-
"@types/react": "^17.0.2",
|
|
50
|
-
"@types/react-dom": "^17.0.13",
|
|
51
|
-
"@types/react-test-renderer": "^17.0.1",
|
|
52
|
-
"eslint": "^8.44.0",
|
|
53
|
-
"jest": "^27.0.4",
|
|
54
|
-
"prettier": "2.4.1",
|
|
55
|
-
"react": "^17.0.2",
|
|
56
|
-
"react-dom": "^17.0.2",
|
|
57
|
-
"react-test-renderer": "^17.0.2",
|
|
58
|
-
"rimraf": "^3.0.2",
|
|
59
|
-
"rollup": "^2.70.0",
|
|
60
|
-
"rollup-plugin-node-externals": "^4.1.1",
|
|
61
|
-
"ts-jest": "^27.1.3",
|
|
62
|
-
"typescript": "^4.3.2"
|
|
43
|
+
"aws-amplify": "^6.0.2",
|
|
44
|
+
"react": "^16.14.0 || ^17.0 || ^18.0"
|
|
63
45
|
},
|
|
64
46
|
"sideEffects": false
|
|
65
47
|
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
|
|
3
|
+
import { AuthInterpreter, AuthStatus } from '@aws-amplify/ui';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Authenticator React.Context type
|
|
7
|
+
*/
|
|
8
|
+
type AuthenticatorContextType = { service: AuthInterpreter };
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* AuthenticatorContext serves static reference to the auth machine service.
|
|
12
|
+
*
|
|
13
|
+
* https://xstate.js.org/docs/recipes/react.html#context-provider
|
|
14
|
+
*/
|
|
15
|
+
export const AuthenticatorContext = React.createContext<
|
|
16
|
+
(AuthenticatorContextType & { authStatus: AuthStatus }) | null
|
|
17
|
+
>(null);
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import React, { ReactNode, useContext, useEffect, useMemo } from 'react';
|
|
2
|
+
import { useInterpret } from '@xstate/react';
|
|
3
|
+
|
|
4
|
+
import { getCurrentUser } from 'aws-amplify/auth';
|
|
5
|
+
import {
|
|
6
|
+
AuthStatus,
|
|
7
|
+
AuthMachineHubHandler,
|
|
8
|
+
createAuthenticatorMachine,
|
|
9
|
+
defaultAuthHubHandler,
|
|
10
|
+
listenToAuthHub,
|
|
11
|
+
} from '@aws-amplify/ui';
|
|
12
|
+
|
|
13
|
+
import { AuthenticatorContext } from './AuthenticatorContext';
|
|
14
|
+
|
|
15
|
+
type Options = Parameters<AuthMachineHubHandler>[2];
|
|
16
|
+
|
|
17
|
+
const createHubHandler =
|
|
18
|
+
(options: Options): AuthMachineHubHandler =>
|
|
19
|
+
async (data, service) => {
|
|
20
|
+
await defaultAuthHubHandler(data, service, options);
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
export default function AuthenticatorProvider({
|
|
24
|
+
children,
|
|
25
|
+
}: {
|
|
26
|
+
children: ReactNode;
|
|
27
|
+
}): JSX.Element {
|
|
28
|
+
// `authStatus` is exposed by `useAuthenticator` but should not be derived directly from the
|
|
29
|
+
// state machine as the machine only updates on `Authenticator` initiated events, which
|
|
30
|
+
// leads to scenarios where the state machine `authStatus` gets "stuck". For exmample,
|
|
31
|
+
// if a user was to sign in using `Auth.signIn` directly rather than using `Authenticator`
|
|
32
|
+
const [authStatus, setAuthStatus] = React.useState<AuthStatus>('configuring');
|
|
33
|
+
|
|
34
|
+
// only run on first render
|
|
35
|
+
React.useEffect(() => {
|
|
36
|
+
getCurrentUser()
|
|
37
|
+
.then(() => {
|
|
38
|
+
setAuthStatus('authenticated');
|
|
39
|
+
})
|
|
40
|
+
.catch(() => {
|
|
41
|
+
setAuthStatus('unauthenticated');
|
|
42
|
+
});
|
|
43
|
+
}, []);
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Based on use cases, developer might already have added another Provider
|
|
47
|
+
* outside Authenticator. In that case, we sync the two providers by just
|
|
48
|
+
* passing the parent value.
|
|
49
|
+
*
|
|
50
|
+
* TODO(BREAKING): enforce only one provider in App tree
|
|
51
|
+
*/
|
|
52
|
+
const parentProviderVal = useContext(AuthenticatorContext);
|
|
53
|
+
const service = useInterpret(createAuthenticatorMachine);
|
|
54
|
+
|
|
55
|
+
const value = useMemo(
|
|
56
|
+
() => (!parentProviderVal ? { authStatus, service } : parentProviderVal),
|
|
57
|
+
[authStatus, parentProviderVal, service]
|
|
58
|
+
);
|
|
59
|
+
|
|
60
|
+
const { service: activeService } = value;
|
|
61
|
+
|
|
62
|
+
useEffect(() => {
|
|
63
|
+
const onSignIn = () => {
|
|
64
|
+
setAuthStatus('authenticated');
|
|
65
|
+
};
|
|
66
|
+
const onSignOut = () => {
|
|
67
|
+
setAuthStatus('unauthenticated');
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
const unsubscribe = listenToAuthHub(
|
|
71
|
+
activeService,
|
|
72
|
+
createHubHandler({ onSignIn, onSignOut })
|
|
73
|
+
);
|
|
74
|
+
return unsubscribe;
|
|
75
|
+
}, [activeService]);
|
|
76
|
+
|
|
77
|
+
return (
|
|
78
|
+
<AuthenticatorContext.Provider value={value}>
|
|
79
|
+
{children}
|
|
80
|
+
</AuthenticatorContext.Provider>
|
|
81
|
+
);
|
|
82
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import {
|
|
2
|
+
AuthenticatorRouteComponentKey,
|
|
3
|
+
AuthenticatorRouteComponentName,
|
|
4
|
+
} from './types';
|
|
5
|
+
|
|
6
|
+
export const COMPONENT_ROUTE_KEYS: AuthenticatorRouteComponentKey[] = [
|
|
7
|
+
'confirmResetPassword',
|
|
8
|
+
'confirmSignIn',
|
|
9
|
+
'confirmSignUp',
|
|
10
|
+
'confirmVerifyUser',
|
|
11
|
+
'forceNewPassword',
|
|
12
|
+
'forgotPassword',
|
|
13
|
+
'setupTotp',
|
|
14
|
+
'signIn',
|
|
15
|
+
'signUp',
|
|
16
|
+
'verifyUser',
|
|
17
|
+
];
|
|
18
|
+
|
|
19
|
+
export const COMPONENT_ROUTE_NAMES: AuthenticatorRouteComponentName[] = [
|
|
20
|
+
'ConfirmResetPassword',
|
|
21
|
+
'ConfirmSignIn',
|
|
22
|
+
'ConfirmSignUp',
|
|
23
|
+
'ConfirmVerifyUser',
|
|
24
|
+
'ForceNewPassword',
|
|
25
|
+
'ForgotPassword',
|
|
26
|
+
'SetupTotp',
|
|
27
|
+
'SignIn',
|
|
28
|
+
'SignUp',
|
|
29
|
+
'VerifyUser',
|
|
30
|
+
];
|