@keycloakify/keycloak-account-ui 25.0.1-rc.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/KeycloakAccountUi.d.ts +3 -0
- package/KeycloakAccountUi.js +29 -0
- package/KeycloakAccountUi.js.map +1 -0
- package/LICENSE +2 -0
- package/README.md +39 -0
- package/account-security/AccountRow.d.ts +8 -0
- package/account-security/AccountRow.js +36 -0
- package/account-security/AccountRow.js.map +1 -0
- package/account-security/DeviceActivity.d.ts +2 -0
- package/account-security/DeviceActivity.js +70 -0
- package/account-security/DeviceActivity.js.map +1 -0
- package/account-security/LinkedAccounts.d.ts +2 -0
- package/account-security/LinkedAccounts.js +25 -0
- package/account-security/LinkedAccounts.js.map +1 -0
- package/account-security/SigningIn.d.ts +2 -0
- package/account-security/SigningIn.js +65 -0
- package/account-security/SigningIn.js.map +1 -0
- package/api/constants.d.ts +2 -0
- package/api/constants.js +3 -0
- package/api/constants.js.map +1 -0
- package/api/methods.d.ts +25 -0
- package/api/methods.js +73 -0
- package/api/methods.js.map +1 -0
- package/api/parse-links.d.ts +5 -0
- package/api/parse-links.js +23 -0
- package/api/parse-links.js.map +1 -0
- package/api/parse-response.d.ts +3 -0
- package/api/parse-response.js +40 -0
- package/api/parse-response.js.map +1 -0
- package/api/representations.d.ts +202 -0
- package/api/representations.js +3 -0
- package/api/representations.js.map +1 -0
- package/api/request.d.ts +12 -0
- package/api/request.js +33 -0
- package/api/request.js.map +1 -0
- package/api.d.ts +13 -0
- package/api.js +52 -0
- package/api.js.map +1 -0
- package/applications/Applications.d.ts +2 -0
- package/applications/Applications.js +52 -0
- package/applications/Applications.js.map +1 -0
- package/components/datalist/EmptyRow.d.ts +5 -0
- package/components/datalist/EmptyRow.js +20 -0
- package/components/datalist/EmptyRow.js.map +1 -0
- package/components/page/Page.d.ts +7 -0
- package/components/page/Page.js +6 -0
- package/components/page/Page.js.map +1 -0
- package/content/ContentComponent.d.ts +2 -0
- package/content/ContentComponent.js +36 -0
- package/content/ContentComponent.js.map +1 -0
- package/content/fetchContent.d.ts +3 -0
- package/content/fetchContent.js +6 -0
- package/content/fetchContent.js.map +1 -0
- package/environment.d.ts +25 -0
- package/environment.js +3 -0
- package/environment.js.map +1 -0
- package/groups/Groups.d.ts +2 -0
- package/groups/Groups.js +45 -0
- package/groups/Groups.js.map +1 -0
- package/i18n.d.ts +4 -0
- package/i18n.js +31 -0
- package/i18n.js.map +1 -0
- package/oid4vci/Oid4Vci.d.ts +2 -0
- package/oid4vci/Oid4Vci.js +54 -0
- package/oid4vci/Oid4Vci.js.map +1 -0
- package/package.json +428 -0
- package/personal-info/PersonalInfo.d.ts +2 -0
- package/personal-info/PersonalInfo.js +63 -0
- package/personal-info/PersonalInfo.js.map +1 -0
- package/resources/EditTheResource.d.ts +8 -0
- package/resources/EditTheResource.js +36 -0
- package/resources/EditTheResource.js.map +1 -0
- package/resources/PermissionRequest.d.ts +7 -0
- package/resources/PermissionRequest.js +39 -0
- package/resources/PermissionRequest.js.map +1 -0
- package/resources/ResourceToolbar.d.ts +12 -0
- package/resources/ResourceToolbar.js +24 -0
- package/resources/ResourceToolbar.js.map +1 -0
- package/resources/Resources.d.ts +2 -0
- package/resources/Resources.js +13 -0
- package/resources/Resources.js.map +1 -0
- package/resources/ResourcesTab.d.ts +5 -0
- package/resources/ResourcesTab.js +103 -0
- package/resources/ResourcesTab.js.map +1 -0
- package/resources/ShareTheResource.d.ts +9 -0
- package/resources/ShareTheResource.js +70 -0
- package/resources/ShareTheResource.js.map +1 -0
- package/resources/SharedWith.d.ts +6 -0
- package/resources/SharedWith.js +8 -0
- package/resources/SharedWith.js.map +1 -0
- package/root/ErrorPage.d.ts +5 -0
- package/root/ErrorPage.js +29 -0
- package/root/ErrorPage.js.map +1 -0
- package/root/Header.d.ts +1 -0
- package/root/Header.js +31 -0
- package/root/Header.js.map +1 -0
- package/root/PageNav.d.ts +23 -0
- package/root/PageNav.js +44 -0
- package/root/PageNav.js.map +1 -0
- package/root/Root.d.ts +1 -0
- package/root/Root.js +12 -0
- package/root/Root.js.map +1 -0
- package/root/header.module.css +3 -0
- package/routes.d.ts +15 -0
- package/routes.js +69 -0
- package/routes.js.map +1 -0
- package/src/KeycloakAccountUi.tsx +36 -0
- package/src/account-security/AccountRow.tsx +136 -0
- package/src/account-security/DeviceActivity.tsx +253 -0
- package/src/account-security/LinkedAccounts.tsx +86 -0
- package/src/account-security/SigningIn.tsx +251 -0
- package/src/api/constants.ts +2 -0
- package/src/api/methods.ts +158 -0
- package/src/api/parse-links.ts +28 -0
- package/src/api/parse-response.ts +53 -0
- package/src/api/representations.ts +219 -0
- package/src/api/request.ts +74 -0
- package/src/api.ts +127 -0
- package/src/applications/Applications.tsx +282 -0
- package/src/components/datalist/EmptyRow.tsx +26 -0
- package/src/components/page/Page.tsx +27 -0
- package/src/content/ContentComponent.tsx +62 -0
- package/src/content/fetchContent.ts +13 -0
- package/src/environment.ts +31 -0
- package/src/global.d.ts +4 -0
- package/src/groups/Groups.tsx +142 -0
- package/src/i18n.ts +47 -0
- package/src/i18next.d.ts +10 -0
- package/src/oid4vci/Oid4Vci.tsx +136 -0
- package/src/personal-info/PersonalInfo.tsx +184 -0
- package/src/resources/EditTheResource.tsx +101 -0
- package/src/resources/PermissionRequest.tsx +129 -0
- package/src/resources/ResourceToolbar.tsx +87 -0
- package/src/resources/Resources.tsx +39 -0
- package/src/resources/ResourcesTab.tsx +410 -0
- package/src/resources/ShareTheResource.tsx +217 -0
- package/src/resources/SharedWith.tsx +26 -0
- package/src/root/ErrorPage.tsx +65 -0
- package/src/root/Header.tsx +62 -0
- package/src/root/PageNav.tsx +154 -0
- package/src/root/Root.tsx +20 -0
- package/src/root/header.module.css +3 -0
- package/src/routes.tsx +85 -0
- package/src/ui-shared/alerts/Alerts.tsx +86 -0
- package/src/ui-shared/buttons/FormSubmitButton.tsx +47 -0
- package/src/ui-shared/context/ErrorPage.tsx +60 -0
- package/src/ui-shared/context/HelpContext.tsx +30 -0
- package/src/ui-shared/context/KeycloakContext.tsx +97 -0
- package/src/ui-shared/context/environment.ts +50 -0
- package/src/ui-shared/continue-cancel/ContinueCancelModal.tsx +75 -0
- package/src/ui-shared/controls/FormErrorText.tsx +23 -0
- package/src/ui-shared/controls/FormLabel.tsx +40 -0
- package/src/ui-shared/controls/HelpItem.tsx +43 -0
- package/src/ui-shared/controls/NumberControl.tsx +89 -0
- package/src/ui-shared/controls/PasswordControl.tsx +71 -0
- package/src/ui-shared/controls/PasswordInput.tsx +50 -0
- package/src/ui-shared/controls/SwitchControl.tsx +67 -0
- package/src/ui-shared/controls/TextAreaControl.tsx +60 -0
- package/src/ui-shared/controls/TextControl.tsx +74 -0
- package/src/ui-shared/controls/keycloak-text-area/KeycloakTextArea.tsx +23 -0
- package/src/ui-shared/controls/select-control/SelectControl.tsx +75 -0
- package/src/ui-shared/controls/select-control/SingleSelectControl.tsx +107 -0
- package/src/ui-shared/controls/select-control/TypeaheadSelectControl.tsx +283 -0
- package/src/ui-shared/icons/IconMapper.tsx +63 -0
- package/src/ui-shared/index.ts +1 -0
- package/src/ui-shared/main.ts +62 -0
- package/src/ui-shared/masthead/DefaultAvatar.tsx +109 -0
- package/src/ui-shared/masthead/KeycloakDropdown.tsx +47 -0
- package/src/ui-shared/masthead/Masthead.tsx +145 -0
- package/src/ui-shared/scroll-form/FormPanel.tsx +29 -0
- package/src/ui-shared/scroll-form/FormTitle.tsx +28 -0
- package/src/ui-shared/scroll-form/ScrollForm.tsx +98 -0
- package/src/ui-shared/scroll-form/ScrollPanel.tsx +21 -0
- package/src/ui-shared/scroll-form/form-title.module.css +4 -0
- package/src/ui-shared/scroll-form/scroll-form.module.css +8 -0
- package/src/ui-shared/select/KeycloakSelect.tsx +49 -0
- package/src/ui-shared/select/SingleSelect.tsx +85 -0
- package/src/ui-shared/select/TypeaheadSelect.tsx +196 -0
- package/src/ui-shared/user-profile/LocaleSelector.tsx +51 -0
- package/src/ui-shared/user-profile/MultiInputComponent.tsx +146 -0
- package/src/ui-shared/user-profile/OptionsComponent.tsx +60 -0
- package/src/ui-shared/user-profile/SelectComponent.tsx +106 -0
- package/src/ui-shared/user-profile/TextAreaComponent.tsx +23 -0
- package/src/ui-shared/user-profile/TextComponent.tsx +30 -0
- package/src/ui-shared/user-profile/UserProfileFields.tsx +246 -0
- package/src/ui-shared/user-profile/UserProfileGroup.tsx +70 -0
- package/src/ui-shared/user-profile/utils.ts +161 -0
- package/src/ui-shared/utils/createNamedContext.ts +11 -0
- package/src/ui-shared/utils/isDefined.ts +3 -0
- package/src/ui-shared/utils/useRequiredContext.ts +24 -0
- package/src/ui-shared/utils/useStorageItem.ts +51 -0
- package/src/ui-shared/utils/useStoredState.ts +38 -0
- package/src/utils/formatDate.ts +18 -0
- package/src/utils/isRecord.ts +2 -0
- package/src/utils/joinPath.ts +22 -0
- package/src/utils/usePromise.ts +74 -0
- package/ui-shared/alerts/Alerts.d.ts +17 -0
- package/ui-shared/alerts/Alerts.js +27 -0
- package/ui-shared/alerts/Alerts.js.map +1 -0
- package/ui-shared/buttons/FormSubmitButton.d.ts +10 -0
- package/ui-shared/buttons/FormSubmitButton.js +26 -0
- package/ui-shared/buttons/FormSubmitButton.js.map +1 -0
- package/ui-shared/context/ErrorPage.d.ts +5 -0
- package/ui-shared/context/ErrorPage.js +24 -0
- package/ui-shared/context/ErrorPage.js.map +1 -0
- package/ui-shared/context/HelpContext.d.ts +9 -0
- package/ui-shared/context/HelpContext.js +14 -0
- package/ui-shared/context/HelpContext.js.map +1 -0
- package/ui-shared/context/KeycloakContext.d.ts +12 -0
- package/ui-shared/context/KeycloakContext.js +53 -0
- package/ui-shared/context/KeycloakContext.js.map +1 -0
- package/ui-shared/context/environment.d.ts +36 -0
- package/ui-shared/context/environment.js +27 -0
- package/ui-shared/context/environment.js.map +1 -0
- package/ui-shared/continue-cancel/ContinueCancelModal.d.ts +15 -0
- package/ui-shared/continue-cancel/ContinueCancelModal.js +27 -0
- package/ui-shared/continue-cancel/ContinueCancelModal.js.map +1 -0
- package/ui-shared/controls/FormErrorText.d.ts +5 -0
- package/ui-shared/controls/FormErrorText.js +19 -0
- package/ui-shared/controls/FormErrorText.js.map +1 -0
- package/ui-shared/controls/FormLabel.d.ts +13 -0
- package/ui-shared/controls/FormLabel.js +20 -0
- package/ui-shared/controls/FormLabel.js.map +1 -0
- package/ui-shared/controls/HelpItem.d.ts +9 -0
- package/ui-shared/controls/HelpItem.js +9 -0
- package/ui-shared/controls/HelpItem.js.map +1 -0
- package/ui-shared/controls/NumberControl.d.ts +13 -0
- package/ui-shared/controls/NumberControl.js +32 -0
- package/ui-shared/controls/NumberControl.js.map +1 -0
- package/ui-shared/controls/PasswordControl.d.ts +9 -0
- package/ui-shared/controls/PasswordControl.js +25 -0
- package/ui-shared/controls/PasswordControl.js.map +1 -0
- package/ui-shared/controls/PasswordInput.d.ts +7 -0
- package/ui-shared/controls/PasswordInput.js +25 -0
- package/ui-shared/controls/PasswordInput.js.map +1 -0
- package/ui-shared/controls/SwitchControl.d.ts +11 -0
- package/ui-shared/controls/SwitchControl.js +29 -0
- package/ui-shared/controls/SwitchControl.js.map +1 -0
- package/ui-shared/controls/TextAreaControl.d.ts +8 -0
- package/ui-shared/controls/TextAreaControl.js +12 -0
- package/ui-shared/controls/TextAreaControl.js.map +1 -0
- package/ui-shared/controls/TextControl.d.ts +10 -0
- package/ui-shared/controls/TextControl.js +24 -0
- package/ui-shared/controls/TextControl.js.map +1 -0
- package/ui-shared/controls/keycloak-text-area/KeycloakTextArea.d.ts +4 -0
- package/ui-shared/controls/keycloak-text-area/KeycloakTextArea.js +5 -0
- package/ui-shared/controls/keycloak-text-area/KeycloakTextArea.js.map +1 -0
- package/ui-shared/controls/select-control/SelectControl.d.ts +31 -0
- package/ui-shared/controls/select-control/SelectControl.js +28 -0
- package/ui-shared/controls/select-control/SelectControl.js.map +1 -0
- package/ui-shared/controls/select-control/SingleSelectControl.d.ts +3 -0
- package/ui-shared/controls/select-control/SingleSelectControl.js +41 -0
- package/ui-shared/controls/select-control/SingleSelectControl.js.map +1 -0
- package/ui-shared/controls/select-control/TypeaheadSelectControl.d.ts +3 -0
- package/ui-shared/controls/select-control/TypeaheadSelectControl.js +138 -0
- package/ui-shared/controls/select-control/TypeaheadSelectControl.js.map +1 -0
- package/ui-shared/icons/IconMapper.d.ts +5 -0
- package/ui-shared/icons/IconMapper.js +40 -0
- package/ui-shared/icons/IconMapper.js.map +1 -0
- package/ui-shared/index.d.ts +1 -0
- package/ui-shared/index.js +2 -0
- package/ui-shared/index.js.map +1 -0
- package/ui-shared/main.d.ts +31 -0
- package/ui-shared/main.js +29 -0
- package/ui-shared/main.js.map +1 -0
- package/ui-shared/masthead/DefaultAvatar.d.ts +7 -0
- package/ui-shared/masthead/DefaultAvatar.js +26 -0
- package/ui-shared/masthead/DefaultAvatar.js.map +1 -0
- package/ui-shared/masthead/KeycloakDropdown.d.ts +10 -0
- package/ui-shared/masthead/KeycloakDropdown.js +23 -0
- package/ui-shared/masthead/KeycloakDropdown.js.map +1 -0
- package/ui-shared/masthead/Masthead.d.ts +22 -0
- package/ui-shared/masthead/Masthead.js +55 -0
- package/ui-shared/masthead/Masthead.js.map +1 -0
- package/ui-shared/scroll-form/FormPanel.d.ts +8 -0
- package/ui-shared/scroll-form/FormPanel.js +9 -0
- package/ui-shared/scroll-form/FormPanel.js.map +1 -0
- package/ui-shared/scroll-form/FormTitle.d.ts +8 -0
- package/ui-shared/scroll-form/FormTitle.js +19 -0
- package/ui-shared/scroll-form/FormTitle.js.map +1 -0
- package/ui-shared/scroll-form/ScrollForm.d.ts +15 -0
- package/ui-shared/scroll-form/ScrollForm.js +38 -0
- package/ui-shared/scroll-form/ScrollForm.js.map +1 -0
- package/ui-shared/scroll-form/ScrollPanel.d.ts +7 -0
- package/ui-shared/scroll-form/ScrollPanel.js +18 -0
- package/ui-shared/scroll-form/ScrollPanel.js.map +1 -0
- package/ui-shared/select/KeycloakSelect.d.ts +30 -0
- package/ui-shared/select/KeycloakSelect.js +31 -0
- package/ui-shared/select/KeycloakSelect.js.map +1 -0
- package/ui-shared/select/SingleSelect.d.ts +4 -0
- package/ui-shared/select/SingleSelect.js +46 -0
- package/ui-shared/select/SingleSelect.js.map +1 -0
- package/ui-shared/select/TypeaheadSelect.d.ts +2 -0
- package/ui-shared/select/TypeaheadSelect.js +96 -0
- package/ui-shared/select/TypeaheadSelect.js.map +1 -0
- package/ui-shared/user-profile/LocaleSelector.d.ts +7 -0
- package/ui-shared/user-profile/LocaleSelector.js +28 -0
- package/ui-shared/user-profile/LocaleSelector.js.map +1 -0
- package/ui-shared/user-profile/MultiInputComponent.d.ts +15 -0
- package/ui-shared/user-profile/MultiInputComponent.js +61 -0
- package/ui-shared/user-profile/MultiInputComponent.js.map +1 -0
- package/ui-shared/user-profile/OptionsComponent.d.ts +2 -0
- package/ui-shared/user-profile/OptionsComponent.js +28 -0
- package/ui-shared/user-profile/OptionsComponent.js.map +1 -0
- package/ui-shared/user-profile/SelectComponent.d.ts +2 -0
- package/ui-shared/user-profile/SelectComponent.js +55 -0
- package/ui-shared/user-profile/SelectComponent.js.map +1 -0
- package/ui-shared/user-profile/TextAreaComponent.d.ts +2 -0
- package/ui-shared/user-profile/TextAreaComponent.js +11 -0
- package/ui-shared/user-profile/TextAreaComponent.js.map +1 -0
- package/ui-shared/user-profile/TextComponent.d.ts +2 -0
- package/ui-shared/user-profile/TextComponent.js +14 -0
- package/ui-shared/user-profile/TextComponent.js.map +1 -0
- package/ui-shared/user-profile/UserProfileFields.d.ts +39 -0
- package/ui-shared/user-profile/UserProfileFields.js +112 -0
- package/ui-shared/user-profile/UserProfileFields.js.map +1 -0
- package/ui-shared/user-profile/UserProfileGroup.d.ts +12 -0
- package/ui-shared/user-profile/UserProfileGroup.js +15 -0
- package/ui-shared/user-profile/UserProfileGroup.js.map +1 -0
- package/ui-shared/user-profile/utils.d.ts +32 -0
- package/ui-shared/user-profile/utils.js +79 -0
- package/ui-shared/user-profile/utils.js.map +1 -0
- package/ui-shared/utils/createNamedContext.d.ts +3 -0
- package/ui-shared/utils/createNamedContext.js +7 -0
- package/ui-shared/utils/createNamedContext.js.map +1 -0
- package/ui-shared/utils/isDefined.d.ts +1 -0
- package/ui-shared/utils/isDefined.js +4 -0
- package/ui-shared/utils/isDefined.js.map +1 -0
- package/ui-shared/utils/useRequiredContext.d.ts +9 -0
- package/ui-shared/utils/useRequiredContext.js +17 -0
- package/ui-shared/utils/useRequiredContext.js.map +1 -0
- package/ui-shared/utils/useStorageItem.d.ts +10 -0
- package/ui-shared/utils/useStorageItem.js +40 -0
- package/ui-shared/utils/useStorageItem.js.map +1 -0
- package/ui-shared/utils/useStoredState.d.ts +13 -0
- package/ui-shared/utils/useStoredState.js +21 -0
- package/ui-shared/utils/useStoredState.js.map +1 -0
- package/utils/formatDate.d.ts +4 -0
- package/utils/formatDate.js +12 -0
- package/utils/formatDate.js.map +1 -0
- package/utils/isRecord.d.ts +1 -0
- package/utils/isRecord.js +2 -0
- package/utils/isRecord.js.map +1 -0
- package/utils/joinPath.d.ts +1 -0
- package/utils/joinPath.js +18 -0
- package/utils/joinPath.js.map +1 -0
- package/utils/usePromise.d.ts +41 -0
- package/utils/usePromise.js +55 -0
- package/utils/usePromise.js.map +1 -0
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Alert,
|
|
3
|
+
AlertActionCloseButton,
|
|
4
|
+
AlertGroup,
|
|
5
|
+
AlertVariant,
|
|
6
|
+
} from "@patternfly/react-core";
|
|
7
|
+
import { createContext, PropsWithChildren, useContext, useState } from "react";
|
|
8
|
+
|
|
9
|
+
export type AddAlertFunction = (
|
|
10
|
+
message: string,
|
|
11
|
+
variant?: AlertVariant,
|
|
12
|
+
description?: string,
|
|
13
|
+
) => void;
|
|
14
|
+
|
|
15
|
+
export type AddErrorFunction = (message: string) => void;
|
|
16
|
+
|
|
17
|
+
export type AlertProps = {
|
|
18
|
+
addAlert: AddAlertFunction;
|
|
19
|
+
addError: AddErrorFunction;
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
export const AlertContext = createContext<AlertProps | undefined>(undefined);
|
|
23
|
+
|
|
24
|
+
export const useAlerts = () => useContext(AlertContext)!;
|
|
25
|
+
|
|
26
|
+
export type AlertType = {
|
|
27
|
+
id: number;
|
|
28
|
+
message: string;
|
|
29
|
+
variant: AlertVariant;
|
|
30
|
+
description?: string;
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
export const AlertProvider = ({ children }: PropsWithChildren) => {
|
|
34
|
+
const [alerts, setAlerts] = useState<AlertType[]>([]);
|
|
35
|
+
|
|
36
|
+
const hideAlert = (id: number) => {
|
|
37
|
+
setAlerts((alerts) => alerts.filter((alert) => alert.id !== id));
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
const addAlert = (
|
|
41
|
+
message: string,
|
|
42
|
+
variant: AlertVariant = AlertVariant.success,
|
|
43
|
+
description?: string,
|
|
44
|
+
) => {
|
|
45
|
+
setAlerts([
|
|
46
|
+
{
|
|
47
|
+
id: Math.random() * 100,
|
|
48
|
+
message,
|
|
49
|
+
variant,
|
|
50
|
+
description,
|
|
51
|
+
},
|
|
52
|
+
...alerts,
|
|
53
|
+
]);
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
const addError = (message: string) => {
|
|
57
|
+
addAlert(message, AlertVariant.danger);
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
return (
|
|
61
|
+
<AlertContext.Provider value={{ addAlert, addError }}>
|
|
62
|
+
<AlertGroup isToast data-testid="alerts">
|
|
63
|
+
{alerts.map(({ id, variant, message, description }) => (
|
|
64
|
+
<Alert
|
|
65
|
+
key={id}
|
|
66
|
+
isLiveRegion
|
|
67
|
+
variant={AlertVariant[variant]}
|
|
68
|
+
variantLabel=""
|
|
69
|
+
title={message}
|
|
70
|
+
actionClose={
|
|
71
|
+
<AlertActionCloseButton
|
|
72
|
+
title={message}
|
|
73
|
+
onClose={() => hideAlert(id)}
|
|
74
|
+
/>
|
|
75
|
+
}
|
|
76
|
+
timeout
|
|
77
|
+
onTimeout={() => hideAlert(id)}
|
|
78
|
+
>
|
|
79
|
+
{description && <p>{description}</p>}
|
|
80
|
+
</Alert>
|
|
81
|
+
))}
|
|
82
|
+
</AlertGroup>
|
|
83
|
+
{children}
|
|
84
|
+
</AlertContext.Provider>
|
|
85
|
+
);
|
|
86
|
+
};
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { Button, ButtonProps } from "@patternfly/react-core";
|
|
2
|
+
import { PropsWithChildren } from "react";
|
|
3
|
+
import { FieldValues, FormState } from "react-hook-form";
|
|
4
|
+
|
|
5
|
+
export type FormSubmitButtonProps = Omit<ButtonProps, "isDisabled"> & {
|
|
6
|
+
formState: FormState<FieldValues>;
|
|
7
|
+
allowNonDirty?: boolean;
|
|
8
|
+
allowInvalid?: boolean;
|
|
9
|
+
isDisabled?: boolean;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
const isSubmittable = (
|
|
13
|
+
formState: FormState<FieldValues>,
|
|
14
|
+
allowNonDirty: boolean,
|
|
15
|
+
allowInvalid: boolean,
|
|
16
|
+
) => {
|
|
17
|
+
return (
|
|
18
|
+
(formState.isValid || allowInvalid) &&
|
|
19
|
+
(formState.isDirty || allowNonDirty) &&
|
|
20
|
+
!formState.isLoading &&
|
|
21
|
+
!formState.isValidating &&
|
|
22
|
+
!formState.isSubmitting
|
|
23
|
+
);
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
export const FormSubmitButton = ({
|
|
27
|
+
formState,
|
|
28
|
+
isDisabled = false,
|
|
29
|
+
allowInvalid = false,
|
|
30
|
+
allowNonDirty = false,
|
|
31
|
+
children,
|
|
32
|
+
...rest
|
|
33
|
+
}: PropsWithChildren<FormSubmitButtonProps>) => {
|
|
34
|
+
return (
|
|
35
|
+
<Button
|
|
36
|
+
variant="primary"
|
|
37
|
+
isDisabled={
|
|
38
|
+
(formState && !isSubmittable(formState, allowNonDirty, allowInvalid)) ||
|
|
39
|
+
isDisabled
|
|
40
|
+
}
|
|
41
|
+
{...rest}
|
|
42
|
+
type="submit"
|
|
43
|
+
>
|
|
44
|
+
{children}
|
|
45
|
+
</Button>
|
|
46
|
+
);
|
|
47
|
+
};
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Button,
|
|
3
|
+
Modal,
|
|
4
|
+
ModalVariant,
|
|
5
|
+
Page,
|
|
6
|
+
Text,
|
|
7
|
+
TextContent,
|
|
8
|
+
TextVariants,
|
|
9
|
+
} from "@patternfly/react-core";
|
|
10
|
+
import { useTranslation } from "react-i18next";
|
|
11
|
+
|
|
12
|
+
type ErrorPageProps = {
|
|
13
|
+
error?: unknown;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
export const ErrorPage = (props: ErrorPageProps) => {
|
|
17
|
+
const { t } = useTranslation();
|
|
18
|
+
const error = props.error;
|
|
19
|
+
const errorMessage = getErrorMessage(error);
|
|
20
|
+
|
|
21
|
+
function onRetry() {
|
|
22
|
+
location.href = location.origin + location.pathname;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
return (
|
|
26
|
+
<Page>
|
|
27
|
+
<Modal
|
|
28
|
+
variant={ModalVariant.small}
|
|
29
|
+
title={t("somethingWentWrong")}
|
|
30
|
+
titleIconVariant="danger"
|
|
31
|
+
showClose={false}
|
|
32
|
+
isOpen
|
|
33
|
+
actions={[
|
|
34
|
+
<Button key="tryAgain" variant="primary" onClick={onRetry}>
|
|
35
|
+
{t("tryAgain")}
|
|
36
|
+
</Button>,
|
|
37
|
+
]}
|
|
38
|
+
>
|
|
39
|
+
<TextContent>
|
|
40
|
+
<Text>{t("somethingWentWrongDescription")}</Text>
|
|
41
|
+
{errorMessage && (
|
|
42
|
+
<Text component={TextVariants.small}>{errorMessage}</Text>
|
|
43
|
+
)}
|
|
44
|
+
</TextContent>
|
|
45
|
+
</Modal>
|
|
46
|
+
</Page>
|
|
47
|
+
);
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
function getErrorMessage(error: unknown): string | null {
|
|
51
|
+
if (typeof error === "string") {
|
|
52
|
+
return error;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
if (error instanceof Error) {
|
|
56
|
+
return error.message;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
return null;
|
|
60
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { PropsWithChildren } from "react";
|
|
2
|
+
import { createNamedContext } from "@keycloakify/keycloak-account-ui/ui-shared/utils/createNamedContext";
|
|
3
|
+
import { useRequiredContext } from "@keycloakify/keycloak-account-ui/ui-shared/utils/useRequiredContext";
|
|
4
|
+
import { useStoredState } from "@keycloakify/keycloak-account-ui/ui-shared/utils/useStoredState";
|
|
5
|
+
|
|
6
|
+
type HelpContextProps = {
|
|
7
|
+
enabled: boolean;
|
|
8
|
+
toggleHelp: () => void;
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
export const HelpContext = createNamedContext<HelpContextProps | undefined>(
|
|
12
|
+
"HelpContext",
|
|
13
|
+
undefined,
|
|
14
|
+
);
|
|
15
|
+
|
|
16
|
+
export const useHelp = () => useRequiredContext(HelpContext);
|
|
17
|
+
|
|
18
|
+
export const Help = ({ children }: PropsWithChildren) => {
|
|
19
|
+
const [enabled, setHelp] = useStoredState(localStorage, "helpEnabled", true);
|
|
20
|
+
|
|
21
|
+
function toggleHelp() {
|
|
22
|
+
setHelp(!enabled);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
return (
|
|
26
|
+
<HelpContext.Provider value={{ enabled, toggleHelp }}>
|
|
27
|
+
{children}
|
|
28
|
+
</HelpContext.Provider>
|
|
29
|
+
);
|
|
30
|
+
};
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { Spinner } from "@patternfly/react-core";
|
|
2
|
+
import Keycloak from "keycloak-js";
|
|
3
|
+
import {
|
|
4
|
+
PropsWithChildren,
|
|
5
|
+
createContext,
|
|
6
|
+
useContext,
|
|
7
|
+
useEffect,
|
|
8
|
+
useMemo,
|
|
9
|
+
useRef,
|
|
10
|
+
useState,
|
|
11
|
+
} from "react";
|
|
12
|
+
import { AlertProvider } from "@keycloakify/keycloak-account-ui/ui-shared/alerts/Alerts";
|
|
13
|
+
import { ErrorPage } from "@keycloakify/keycloak-account-ui/ui-shared/context/ErrorPage";
|
|
14
|
+
import { Help } from "@keycloakify/keycloak-account-ui/ui-shared/context/HelpContext";
|
|
15
|
+
import { BaseEnvironment } from "@keycloakify/keycloak-account-ui/ui-shared/context/environment";
|
|
16
|
+
|
|
17
|
+
export type KeycloakContext<T extends BaseEnvironment = BaseEnvironment> =
|
|
18
|
+
KeycloakContextProps<T> & {
|
|
19
|
+
keycloak: Keycloak;
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
const createKeycloakEnvContext = <T extends BaseEnvironment>() =>
|
|
23
|
+
createContext<KeycloakContext<T> | undefined>(undefined);
|
|
24
|
+
|
|
25
|
+
let KeycloakEnvContext: any;
|
|
26
|
+
|
|
27
|
+
export const useEnvironment = <
|
|
28
|
+
T extends BaseEnvironment = BaseEnvironment,
|
|
29
|
+
>() => {
|
|
30
|
+
const context = useContext<KeycloakContext<T>>(KeycloakEnvContext);
|
|
31
|
+
if (!context)
|
|
32
|
+
throw Error(
|
|
33
|
+
"no environment provider in the hierarchy make sure to add the provider",
|
|
34
|
+
);
|
|
35
|
+
return context;
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
interface KeycloakContextProps<T extends BaseEnvironment> {
|
|
39
|
+
environment: T;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export const KeycloakProvider = <T extends BaseEnvironment>({
|
|
43
|
+
environment,
|
|
44
|
+
children,
|
|
45
|
+
}: PropsWithChildren<KeycloakContextProps<T>>) => {
|
|
46
|
+
KeycloakEnvContext = createKeycloakEnvContext<T>();
|
|
47
|
+
const calledOnce = useRef(false);
|
|
48
|
+
const [init, setInit] = useState(false);
|
|
49
|
+
const [error, setError] = useState<unknown>();
|
|
50
|
+
const keycloak = useMemo(() => {
|
|
51
|
+
const keycloak = new Keycloak({
|
|
52
|
+
url: environment.serverBaseUrl,
|
|
53
|
+
realm: environment.realm,
|
|
54
|
+
clientId: environment.clientId,
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
keycloak.onAuthLogout = () => keycloak.login();
|
|
58
|
+
|
|
59
|
+
return keycloak;
|
|
60
|
+
}, [environment]);
|
|
61
|
+
|
|
62
|
+
useEffect(() => {
|
|
63
|
+
// only needed in dev mode
|
|
64
|
+
if (calledOnce.current) {
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const init = () =>
|
|
69
|
+
keycloak.init({
|
|
70
|
+
onLoad: "check-sso",
|
|
71
|
+
pkceMethod: "S256",
|
|
72
|
+
responseMode: "query",
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
init()
|
|
76
|
+
.then(() => setInit(true))
|
|
77
|
+
.catch((error) => setError(error));
|
|
78
|
+
|
|
79
|
+
calledOnce.current = true;
|
|
80
|
+
}, [keycloak]);
|
|
81
|
+
|
|
82
|
+
if (error) {
|
|
83
|
+
return <ErrorPage error={error} />;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
if (!init) {
|
|
87
|
+
return <Spinner />;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
return (
|
|
91
|
+
<KeycloakEnvContext.Provider value={{ environment, keycloak }}>
|
|
92
|
+
<AlertProvider>
|
|
93
|
+
<Help>{children}</Help>
|
|
94
|
+
</AlertProvider>
|
|
95
|
+
</KeycloakEnvContext.Provider>
|
|
96
|
+
);
|
|
97
|
+
};
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/** The base environment variables that are shared between the Admin and Account Consoles. */
|
|
2
|
+
export type BaseEnvironment = {
|
|
3
|
+
/**
|
|
4
|
+
* The URL to the root of the Keycloak server, including the path if present, this is **NOT** always equivalent to the URL of the Admin Console.
|
|
5
|
+
* For example, the Keycloak server could be hosted on `auth.example.com` and Admin Console may be hosted on `admin.example.com/some/path`.
|
|
6
|
+
*
|
|
7
|
+
* Note that this URL is normalized not to include a trailing slash, so take this into account when constructing URLs.
|
|
8
|
+
*
|
|
9
|
+
* @see {@link https://www.keycloak.org/server/hostname#_administration_console}
|
|
10
|
+
*/
|
|
11
|
+
serverBaseUrl: string;
|
|
12
|
+
/** The identifier of the realm used to authenticate the user. */
|
|
13
|
+
realm: string;
|
|
14
|
+
/** The identifier of the client used to authenticate the user. */
|
|
15
|
+
clientId: string;
|
|
16
|
+
/** The base URL of the resources. */
|
|
17
|
+
resourceUrl: string;
|
|
18
|
+
/** The source URL for the the logo image. */
|
|
19
|
+
logo: string;
|
|
20
|
+
/** The URL to be followed when the logo is clicked. */
|
|
21
|
+
logoUrl: string;
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Extracts the environment variables from the document, these variables are injected by Keycloak as a script tag, the contents of which can be parsed as JSON. For example:
|
|
26
|
+
*
|
|
27
|
+
*```html
|
|
28
|
+
* <script id="environment" type="application/json">
|
|
29
|
+
* {
|
|
30
|
+
* "realm": "master",
|
|
31
|
+
* "clientId": "security-admin-console",
|
|
32
|
+
* "etc": "..."
|
|
33
|
+
* }
|
|
34
|
+
* </script>
|
|
35
|
+
* ```
|
|
36
|
+
*/
|
|
37
|
+
export function getInjectedEnvironment<T>(): T {
|
|
38
|
+
const element = document.getElementById("environment");
|
|
39
|
+
const contents = element?.textContent;
|
|
40
|
+
|
|
41
|
+
if (typeof contents !== "string") {
|
|
42
|
+
throw new Error("Environment variables not found in the document.");
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
try {
|
|
46
|
+
return JSON.parse(contents);
|
|
47
|
+
} catch (error) {
|
|
48
|
+
throw new Error("Unable to parse environment variables as JSON.");
|
|
49
|
+
}
|
|
50
|
+
}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { ReactNode, useState } from "react";
|
|
2
|
+
import { Button, ButtonProps, Modal, ModalProps } from "@patternfly/react-core";
|
|
3
|
+
|
|
4
|
+
export type ContinueCancelModalProps = Omit<ModalProps, "ref" | "children"> & {
|
|
5
|
+
modalTitle: string;
|
|
6
|
+
continueLabel: string;
|
|
7
|
+
cancelLabel: string;
|
|
8
|
+
buttonTitle: string | ReactNode;
|
|
9
|
+
buttonVariant?: ButtonProps["variant"];
|
|
10
|
+
buttonTestRole?: string;
|
|
11
|
+
isDisabled?: boolean;
|
|
12
|
+
onContinue: () => void;
|
|
13
|
+
component?: React.ElementType<any> | React.ComponentType<any>;
|
|
14
|
+
children?: ReactNode;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
export const ContinueCancelModal = ({
|
|
18
|
+
modalTitle,
|
|
19
|
+
continueLabel,
|
|
20
|
+
cancelLabel,
|
|
21
|
+
buttonTitle,
|
|
22
|
+
isDisabled,
|
|
23
|
+
buttonVariant,
|
|
24
|
+
buttonTestRole,
|
|
25
|
+
onContinue,
|
|
26
|
+
component = Button,
|
|
27
|
+
children,
|
|
28
|
+
...rest
|
|
29
|
+
}: ContinueCancelModalProps) => {
|
|
30
|
+
const [open, setOpen] = useState(false);
|
|
31
|
+
const Component = component;
|
|
32
|
+
|
|
33
|
+
return (
|
|
34
|
+
<>
|
|
35
|
+
<Component
|
|
36
|
+
variant={buttonVariant}
|
|
37
|
+
onClick={() => setOpen(true)}
|
|
38
|
+
isDisabled={isDisabled}
|
|
39
|
+
data-testrole={buttonTestRole}
|
|
40
|
+
>
|
|
41
|
+
{buttonTitle}
|
|
42
|
+
</Component>
|
|
43
|
+
<Modal
|
|
44
|
+
variant="small"
|
|
45
|
+
{...rest}
|
|
46
|
+
title={modalTitle}
|
|
47
|
+
isOpen={open}
|
|
48
|
+
onClose={() => setOpen(false)}
|
|
49
|
+
actions={[
|
|
50
|
+
<Button
|
|
51
|
+
id="modal-confirm"
|
|
52
|
+
key="confirm"
|
|
53
|
+
variant="primary"
|
|
54
|
+
onClick={() => {
|
|
55
|
+
setOpen(false);
|
|
56
|
+
onContinue();
|
|
57
|
+
}}
|
|
58
|
+
>
|
|
59
|
+
{continueLabel}
|
|
60
|
+
</Button>,
|
|
61
|
+
<Button
|
|
62
|
+
id="modal-cancel"
|
|
63
|
+
key="cancel"
|
|
64
|
+
variant="secondary"
|
|
65
|
+
onClick={() => setOpen(false)}
|
|
66
|
+
>
|
|
67
|
+
{cancelLabel}
|
|
68
|
+
</Button>,
|
|
69
|
+
]}
|
|
70
|
+
>
|
|
71
|
+
{children}
|
|
72
|
+
</Modal>
|
|
73
|
+
</>
|
|
74
|
+
);
|
|
75
|
+
};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import {
|
|
2
|
+
FormHelperText,
|
|
3
|
+
FormHelperTextProps,
|
|
4
|
+
HelperText,
|
|
5
|
+
HelperTextItem,
|
|
6
|
+
} from "@patternfly/react-core";
|
|
7
|
+
import { ExclamationCircleIcon } from "@patternfly/react-icons";
|
|
8
|
+
|
|
9
|
+
export type FormErrorTextProps = FormHelperTextProps & {
|
|
10
|
+
message: string;
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
export const FormErrorText = ({ message, ...props }: FormErrorTextProps) => {
|
|
14
|
+
return (
|
|
15
|
+
<FormHelperText {...props}>
|
|
16
|
+
<HelperText>
|
|
17
|
+
<HelperTextItem icon={<ExclamationCircleIcon />} variant="error">
|
|
18
|
+
{message}
|
|
19
|
+
</HelperTextItem>
|
|
20
|
+
</HelperText>
|
|
21
|
+
</FormHelperText>
|
|
22
|
+
);
|
|
23
|
+
};
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { FormGroup, FormGroupProps } from "@patternfly/react-core";
|
|
2
|
+
import { PropsWithChildren, ReactNode } from "react";
|
|
3
|
+
import { FieldError, FieldValues, Merge } from "react-hook-form";
|
|
4
|
+
import { FormErrorText } from "@keycloakify/keycloak-account-ui/ui-shared/controls/FormErrorText";
|
|
5
|
+
import { HelpItem } from "@keycloakify/keycloak-account-ui/ui-shared/controls/HelpItem";
|
|
6
|
+
|
|
7
|
+
export type FieldProps<T extends FieldValues = FieldValues> = {
|
|
8
|
+
label?: string;
|
|
9
|
+
name: string;
|
|
10
|
+
labelIcon?: string | ReactNode;
|
|
11
|
+
error?: FieldError | Merge<FieldError, T>;
|
|
12
|
+
isRequired: boolean;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
type FormLabelProps = FieldProps & Omit<FormGroupProps, "label" | "labelIcon">;
|
|
16
|
+
|
|
17
|
+
export const FormLabel = ({
|
|
18
|
+
name,
|
|
19
|
+
label,
|
|
20
|
+
labelIcon,
|
|
21
|
+
error,
|
|
22
|
+
children,
|
|
23
|
+
...rest
|
|
24
|
+
}: PropsWithChildren<FormLabelProps>) => (
|
|
25
|
+
<FormGroup
|
|
26
|
+
label={label || name}
|
|
27
|
+
fieldId={name}
|
|
28
|
+
labelIcon={
|
|
29
|
+
labelIcon ? (
|
|
30
|
+
<HelpItem helpText={labelIcon} fieldLabelId={name} />
|
|
31
|
+
) : undefined
|
|
32
|
+
}
|
|
33
|
+
{...rest}
|
|
34
|
+
>
|
|
35
|
+
{children}
|
|
36
|
+
{error && (
|
|
37
|
+
<FormErrorText data-testid={`${name}-helper`} message={error.message} />
|
|
38
|
+
)}
|
|
39
|
+
</FormGroup>
|
|
40
|
+
);
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { Icon, Popover } from "@patternfly/react-core";
|
|
2
|
+
import { HelpIcon } from "@patternfly/react-icons";
|
|
3
|
+
import { ReactNode } from "react";
|
|
4
|
+
import { useHelp } from "@keycloakify/keycloak-account-ui/ui-shared/context/HelpContext";
|
|
5
|
+
|
|
6
|
+
type HelpItemProps = {
|
|
7
|
+
helpText: string | ReactNode;
|
|
8
|
+
fieldLabelId: string;
|
|
9
|
+
noVerticalAlign?: boolean;
|
|
10
|
+
unWrap?: boolean;
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
export const HelpItem = ({
|
|
14
|
+
helpText,
|
|
15
|
+
fieldLabelId,
|
|
16
|
+
noVerticalAlign = true,
|
|
17
|
+
unWrap = false,
|
|
18
|
+
}: HelpItemProps) => {
|
|
19
|
+
const { enabled } = useHelp();
|
|
20
|
+
return enabled ? (
|
|
21
|
+
<Popover bodyContent={helpText}>
|
|
22
|
+
<>
|
|
23
|
+
{!unWrap && (
|
|
24
|
+
<button
|
|
25
|
+
data-testid={`help-label-${fieldLabelId}`}
|
|
26
|
+
aria-label={fieldLabelId}
|
|
27
|
+
onClick={(e) => e.preventDefault()}
|
|
28
|
+
className="pf-v5-c-form__group-label-help"
|
|
29
|
+
>
|
|
30
|
+
<Icon isInline={noVerticalAlign}>
|
|
31
|
+
<HelpIcon />
|
|
32
|
+
</Icon>
|
|
33
|
+
</button>
|
|
34
|
+
)}
|
|
35
|
+
{unWrap && (
|
|
36
|
+
<Icon isInline={noVerticalAlign}>
|
|
37
|
+
<HelpIcon />
|
|
38
|
+
</Icon>
|
|
39
|
+
)}
|
|
40
|
+
</>
|
|
41
|
+
</Popover>
|
|
42
|
+
) : null;
|
|
43
|
+
};
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import {
|
|
2
|
+
NumberInput,
|
|
3
|
+
NumberInputProps,
|
|
4
|
+
ValidatedOptions,
|
|
5
|
+
} from "@patternfly/react-core";
|
|
6
|
+
import {
|
|
7
|
+
Controller,
|
|
8
|
+
ControllerProps,
|
|
9
|
+
FieldPath,
|
|
10
|
+
FieldValues,
|
|
11
|
+
UseControllerProps,
|
|
12
|
+
useFormContext,
|
|
13
|
+
} from "react-hook-form";
|
|
14
|
+
import { FormLabel } from "@keycloakify/keycloak-account-ui/ui-shared/controls/FormLabel";
|
|
15
|
+
|
|
16
|
+
export type NumberControlOption = {
|
|
17
|
+
key: string;
|
|
18
|
+
value: string;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
export type NumberControlProps<
|
|
22
|
+
T extends FieldValues,
|
|
23
|
+
P extends FieldPath<T> = FieldPath<T>,
|
|
24
|
+
> = Omit<NumberInputProps, "name" | "isRequired" | "required"> &
|
|
25
|
+
UseControllerProps<T, P> & {
|
|
26
|
+
name: string;
|
|
27
|
+
label?: string;
|
|
28
|
+
labelIcon?: string;
|
|
29
|
+
controller: Omit<ControllerProps, "name" | "render">;
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
export const NumberControl = <
|
|
33
|
+
T extends FieldValues,
|
|
34
|
+
P extends FieldPath<T> = FieldPath<T>,
|
|
35
|
+
>({
|
|
36
|
+
name,
|
|
37
|
+
label,
|
|
38
|
+
controller,
|
|
39
|
+
labelIcon,
|
|
40
|
+
...rest
|
|
41
|
+
}: NumberControlProps<T, P>) => {
|
|
42
|
+
const {
|
|
43
|
+
control,
|
|
44
|
+
formState: { errors },
|
|
45
|
+
} = useFormContext();
|
|
46
|
+
return (
|
|
47
|
+
<FormLabel
|
|
48
|
+
name={name}
|
|
49
|
+
label={label}
|
|
50
|
+
isRequired={controller.rules?.required === true}
|
|
51
|
+
error={errors[name]}
|
|
52
|
+
labelIcon={labelIcon}
|
|
53
|
+
>
|
|
54
|
+
<Controller
|
|
55
|
+
{...controller}
|
|
56
|
+
name={name}
|
|
57
|
+
control={control}
|
|
58
|
+
render={({ field }) => {
|
|
59
|
+
const required = !!controller.rules?.required;
|
|
60
|
+
const min = controller.rules?.min;
|
|
61
|
+
const value =
|
|
62
|
+
field.value === 0 ? controller.defaultValue : field.value;
|
|
63
|
+
const setValue = (newValue: number) =>
|
|
64
|
+
field.onChange(min ? Math.max(newValue, Number(min)) : newValue);
|
|
65
|
+
|
|
66
|
+
return (
|
|
67
|
+
<NumberInput
|
|
68
|
+
{...rest}
|
|
69
|
+
id={name}
|
|
70
|
+
value={value}
|
|
71
|
+
validated={
|
|
72
|
+
errors[name] ? ValidatedOptions.error : ValidatedOptions.default
|
|
73
|
+
}
|
|
74
|
+
required={required}
|
|
75
|
+
min={Number(min)}
|
|
76
|
+
max={Number(controller.rules?.max)}
|
|
77
|
+
onPlus={() => setValue(value + 1)}
|
|
78
|
+
onMinus={() => setValue(value - 1)}
|
|
79
|
+
onChange={(event) => {
|
|
80
|
+
const newValue = Number(event.currentTarget.value);
|
|
81
|
+
setValue(!isNaN(newValue) ? newValue : controller.defaultValue);
|
|
82
|
+
}}
|
|
83
|
+
/>
|
|
84
|
+
);
|
|
85
|
+
}}
|
|
86
|
+
/>
|
|
87
|
+
</FormLabel>
|
|
88
|
+
);
|
|
89
|
+
};
|