@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.
Files changed (82) hide show
  1. package/dist/esm/Authenticator/context/AuthenticatorProvider.mjs +5 -6
  2. package/dist/esm/Authenticator/hooks/constants.mjs +4 -4
  3. package/dist/esm/Authenticator/hooks/useAuthenticator/useAuthenticator.mjs +14 -11
  4. package/dist/esm/Authenticator/hooks/useAuthenticator/utils.mjs +10 -18
  5. package/dist/esm/Authenticator/hooks/useAuthenticatorRoute/constants.mjs +7 -4
  6. package/dist/esm/Authenticator/hooks/useAuthenticatorRoute/useAuthenticatorRoute.mjs +6 -6
  7. package/dist/esm/Authenticator/hooks/useAuthenticatorRoute/utils.mjs +47 -18
  8. package/dist/esm/Authenticator/hooks/utils.mjs +2 -2
  9. package/dist/esm/components/FormCore/FormProvider.mjs +15 -0
  10. package/dist/esm/components/FormCore/useField.mjs +20 -0
  11. package/dist/esm/components/FormCore/useForm.mjs +51 -0
  12. package/dist/esm/components/FormCore/withFormProvider.mjs +15 -0
  13. package/dist/esm/hooks/useSetUserAgent.mjs +15 -0
  14. package/dist/esm/hooks/useTimeout.mjs +22 -0
  15. package/dist/esm/index.mjs +7 -0
  16. package/dist/esm/utils/createContextUtilities.mjs +80 -0
  17. package/dist/index.js +284 -96
  18. package/dist/types/Authenticator/hooks/types.d.ts +10 -7
  19. package/dist/types/Authenticator/hooks/useAuthenticator/types.d.ts +1 -3
  20. package/dist/types/Authenticator/hooks/useAuthenticator/utils.d.ts +2 -3
  21. package/dist/types/Authenticator/hooks/useAuthenticatorRoute/types.d.ts +3 -3
  22. package/dist/types/Authenticator/hooks/useAuthenticatorRoute/useAuthenticatorRoute.d.ts +2 -2
  23. package/dist/types/Authenticator/hooks/useAuthenticatorRoute/utils.d.ts +2 -2
  24. package/dist/types/components/FormCore/FormProvider.d.ts +4 -0
  25. package/dist/types/components/FormCore/index.d.ts +5 -0
  26. package/dist/types/components/FormCore/types.d.ts +217 -0
  27. package/dist/types/components/FormCore/useControlledField.d.ts +9 -0
  28. package/dist/types/components/FormCore/useField.d.ts +9 -0
  29. package/dist/types/components/FormCore/useForm.d.ts +12 -0
  30. package/dist/types/components/FormCore/withFormProvider.d.ts +8 -0
  31. package/dist/types/components/index.d.ts +1 -0
  32. package/dist/types/hooks/index.d.ts +2 -0
  33. package/dist/types/hooks/useSetUserAgent.d.ts +2 -0
  34. package/dist/types/hooks/useTimeout.d.ts +4 -0
  35. package/dist/types/index.d.ts +3 -2
  36. package/dist/types/types/index.d.ts +1 -1
  37. package/dist/types/types/types.d.ts +2 -0
  38. package/dist/types/utils/createContextUtilities.d.ts +26 -18
  39. package/package.json +11 -29
  40. package/src/Authenticator/context/AuthenticatorContext.tsx +17 -0
  41. package/src/Authenticator/context/AuthenticatorProvider.tsx +82 -0
  42. package/src/Authenticator/context/index.ts +2 -0
  43. package/src/Authenticator/hooks/constants.ts +30 -0
  44. package/src/Authenticator/hooks/index.ts +5 -0
  45. package/src/Authenticator/hooks/types.ts +218 -0
  46. package/src/Authenticator/hooks/useAuthenticator/__mock__/useAuthenticator.ts +66 -0
  47. package/src/Authenticator/hooks/useAuthenticator/constants.ts +2 -0
  48. package/src/Authenticator/hooks/useAuthenticator/index.ts +2 -0
  49. package/src/Authenticator/hooks/useAuthenticator/types.ts +48 -0
  50. package/src/Authenticator/hooks/useAuthenticator/useAuthenticator.ts +72 -0
  51. package/src/Authenticator/hooks/useAuthenticator/utils.ts +97 -0
  52. package/src/Authenticator/hooks/useAuthenticatorInitMachine/index.ts +1 -0
  53. package/src/Authenticator/hooks/useAuthenticatorInitMachine/useAuthenticatorInitMachine.tsx +25 -0
  54. package/src/Authenticator/hooks/useAuthenticatorRoute/constants.ts +107 -0
  55. package/src/Authenticator/hooks/useAuthenticatorRoute/index.ts +2 -0
  56. package/src/Authenticator/hooks/useAuthenticatorRoute/types.ts +111 -0
  57. package/src/Authenticator/hooks/useAuthenticatorRoute/useAuthenticatorRoute.ts +126 -0
  58. package/src/Authenticator/hooks/useAuthenticatorRoute/utils.ts +204 -0
  59. package/src/Authenticator/hooks/utils.ts +38 -0
  60. package/src/Authenticator/index.ts +23 -0
  61. package/src/components/FormCore/FormProvider.tsx +37 -0
  62. package/src/components/FormCore/index.ts +13 -0
  63. package/src/components/FormCore/types.ts +277 -0
  64. package/src/components/FormCore/useControlledField.ts +73 -0
  65. package/src/components/FormCore/useField.ts +25 -0
  66. package/src/components/FormCore/useForm.ts +84 -0
  67. package/src/components/FormCore/withFormProvider.tsx +31 -0
  68. package/src/components/RenderNothing/RenderNothing.tsx +6 -0
  69. package/src/components/RenderNothing/index.ts +1 -0
  70. package/src/components/index.ts +15 -0
  71. package/src/hooks/index.ts +8 -0
  72. package/src/hooks/useDeprecationWarning.ts +27 -0
  73. package/src/hooks/useHasValueUpdated.ts +28 -0
  74. package/src/hooks/usePreviousValue.ts +15 -0
  75. package/src/hooks/useSetUserAgent.ts +18 -0
  76. package/src/hooks/useTimeout.ts +30 -0
  77. package/src/index.ts +48 -0
  78. package/src/types/index.ts +1 -0
  79. package/src/types/types.ts +3 -0
  80. package/src/utils/createContextUtilities.tsx +131 -0
  81. package/src/utils/index.ts +1 -0
  82. package/dist/esm/node_modules/tslib/tslib.es6.mjs +0 -38
@@ -0,0 +1,3 @@
1
+ export type AnyComponent = React.ComponentType<any>;
2
+
3
+ export type MergeProps<C, P> = C & Omit<P, keyof C>;
@@ -0,0 +1,131 @@
1
+ import React from 'react';
2
+
3
+ import { isUndefined, isString } from '@aws-amplify/ui';
4
+
5
+ export const INVALID_OPTIONS_MESSAGE =
6
+ 'an `errorMessage` or a `defaultValue` must be provided in `options`';
7
+
8
+ interface OptionsWithDefaultValue<Name, ContextType> {
9
+ contextName: Name;
10
+ defaultValue: ContextType;
11
+ // exclude `errorMessage` if `defaultValue` is present
12
+ errorMessage?: never;
13
+ }
14
+
15
+ interface OptionsWithErrorMessage<Name, ErrorMessage> {
16
+ contextName: Name;
17
+ errorMessage: ErrorMessage;
18
+ // exclude `defaultValue` if `errorMessage` is present
19
+ defaultValue?: never;
20
+ }
21
+
22
+ type ContextOptions<Type, Name extends string, Message> =
23
+ | OptionsWithDefaultValue<Name, Type>
24
+ | OptionsWithErrorMessage<Name, Message>;
25
+
26
+ type HookParams = { errorMessage?: string };
27
+
28
+ type UtilityKey<ContextName extends string> =
29
+ | `${ContextName}Provider`
30
+ | `use${ContextName}`
31
+ | `${ContextName}Context`;
32
+
33
+ type CreateContextUtilitiesReturn<ContextType, ContextName extends string> = {
34
+ [Key in UtilityKey<ContextName>]: Key extends `${string}Provider`
35
+ ? React.ComponentType<React.PropsWithChildren<ContextType>>
36
+ : Key extends `use${string}`
37
+ ? (params?: HookParams) => ContextType
38
+ : Key extends `${string}Context`
39
+ ? React.Context<ContextType | undefined>
40
+ : never;
41
+ };
42
+
43
+ /**
44
+ * Uses `ContextType`/`Name` generics and `options` to create:
45
+ * - `${Name}Context`: React Context of type `ContextType`
46
+ * - `Provider${Name}`: React Context `Provider` component exposing the `ContextType`
47
+ * as optional props
48
+ * - `use${Name}`: Utility Hook exposing the values of `ContextType`. Allows
49
+ * params with `errorMessage` for granular error messaging
50
+ *
51
+ * @template ContextType Type definition of the Context.
52
+ * > For most use cases the keys of `ContextType` should not be optional in
53
+ * preference of explicit `undefined` to avoid optional types on the
54
+ * Utility Hook return
55
+ *
56
+ * @param options Context utility options. Requires a `contextName`, and
57
+ * either a `defaultValue` of `ContextType` **or** an `errorMessage`
58
+ * allowing for differing behaviors of the Utility Hook when used outside a
59
+ * parent `Provider`:
60
+ *
61
+ * - `defaultValue`: Ensures the Utility Hook returns a default value for
62
+ * scenarios **where the missing context values should not impact usage**
63
+ * - `errorMessage`: Ensures the Utility Hook throws an error for
64
+ * scenarios **where the missing context values should prevent** usage
65
+ *
66
+ * @returns `Context`, `Provider` Component and `useContext` Utility Hook
67
+ *
68
+ * @usage
69
+ * ```ts
70
+ * interface StuffContextType {
71
+ * things: number;
72
+ * }
73
+ *
74
+ * // with `defaultValue`
75
+ * const defaultValue: StuffContextType = { things: 7 };
76
+ *
77
+ * const { StuffProvider, useStuff } = createContextUtilities({
78
+ * contextName: 'Stuff',
79
+ * defaultValue,
80
+ * });
81
+ *
82
+ * // with `errorMessage`
83
+ * const { StuffProvider, useStuff } = createContextUtilities<StuffContextType>({
84
+ * contextName: 'Stuff',
85
+ * errorMessage: '`useStuff` must be used in a `StuffProvider`'
86
+ * });
87
+ * ```
88
+ */
89
+ export default function createContextUtilities<
90
+ ContextType,
91
+ ContextName extends string = string,
92
+ Message extends string | undefined = string | undefined
93
+ >(
94
+ options: ContextOptions<ContextType, ContextName, Message>
95
+ ): CreateContextUtilitiesReturn<ContextType, ContextName> {
96
+ const { contextName, defaultValue, errorMessage } = options ?? {};
97
+
98
+ if (isUndefined(defaultValue) && !isString(errorMessage)) {
99
+ throw new Error(INVALID_OPTIONS_MESSAGE);
100
+ }
101
+
102
+ const Context = React.createContext<ContextType | undefined>(defaultValue);
103
+
104
+ function Provider(props: React.PropsWithChildren<ContextType>) {
105
+ const { children, ...context } = props;
106
+ const value = React.useMemo(
107
+ () => context,
108
+ // Unpack `context` for the dep array; using `[context]` results in
109
+ // evaluation on every render
110
+ // eslint-disable-next-line react-hooks/exhaustive-deps
111
+ Object.values(context)
112
+ ) as ContextType;
113
+ return <Context.Provider value={value}>{children}</Context.Provider>;
114
+ }
115
+
116
+ Provider.displayName = `${contextName}Provider`;
117
+
118
+ return {
119
+ [`use${contextName}`]: function (params?: HookParams) {
120
+ const context = React.useContext(Context);
121
+
122
+ if (isUndefined(context)) {
123
+ throw new Error(params?.errorMessage ?? errorMessage);
124
+ }
125
+
126
+ return context;
127
+ },
128
+ [`${contextName}Provider`]: Provider,
129
+ [`${contextName}Context`]: Context,
130
+ } as CreateContextUtilitiesReturn<ContextType, ContextName>;
131
+ }
@@ -0,0 +1 @@
1
+ export { default as createContextUtilities } from './createContextUtilities';
@@ -1,38 +0,0 @@
1
- /******************************************************************************
2
- Copyright (c) Microsoft Corporation.
3
-
4
- Permission to use, copy, modify, and/or distribute this software for any
5
- purpose with or without fee is hereby granted.
6
-
7
- THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
8
- REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
9
- AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
10
- INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
11
- LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
12
- OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
13
- PERFORMANCE OF THIS SOFTWARE.
14
- ***************************************************************************** */
15
-
16
- function __rest(s, e) {
17
- var t = {};
18
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
19
- t[p] = s[p];
20
- if (s != null && typeof Object.getOwnPropertySymbols === "function")
21
- for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
22
- if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
23
- t[p[i]] = s[p[i]];
24
- }
25
- return t;
26
- }
27
-
28
- function __awaiter(thisArg, _arguments, P, generator) {
29
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
30
- return new (P || (P = Promise))(function (resolve, reject) {
31
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
32
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
33
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
34
- step((generator = generator.apply(thisArg, _arguments || [])).next());
35
- });
36
- }
37
-
38
- export { __awaiter, __rest };