Use react-hook-form v7 for client keys (#4045)

This commit is contained in:
Erik Jan de Wit 2022-12-15 10:13:36 -05:00 committed by GitHub
parent 3c8e4f3b98
commit 31f96761ea
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 24 additions and 23 deletions

View file

@ -5,7 +5,7 @@ import {
FormProvider, FormProvider,
useForm, useForm,
useFormContext, useFormContext,
} from "react-hook-form"; } from "react-hook-form-v7";
import { import {
Button, Button,
ButtonVariant, ButtonVariant,
@ -46,6 +46,10 @@ const extensions = new Map([
["BCFKS", "bcfks"], ["BCFKS", "bcfks"],
]); ]);
type FormFields = KeyStoreConfig & {
file: string | File;
};
export const getFileExtension = (format: string) => extensions.get(format); export const getFileExtension = (format: string) => extensions.get(format);
export const KeyForm = ({ export const KeyForm = ({
@ -58,7 +62,7 @@ export const KeyForm = ({
const [filename, setFilename] = useState<string>(); const [filename, setFilename] = useState<string>();
const [openArchiveFormat, setOpenArchiveFormat] = useState(false); const [openArchiveFormat, setOpenArchiveFormat] = useState(false);
const { control, watch } = useFormContext<KeyStoreConfig>(); const { control, watch } = useFormContext<FormFields>();
const format = watch("format"); const format = watch("format");
const { cryptoInfo } = useServerInfo(); const { cryptoInfo } = useServerInfo();
@ -79,15 +83,15 @@ export const KeyForm = ({
name="format" name="format"
defaultValue="JKS" defaultValue="JKS"
control={control} control={control}
render={({ onChange, value }) => ( render={({ field }) => (
<Select <Select
toggleId="archiveFormat" toggleId="archiveFormat"
onToggle={setOpenArchiveFormat} onToggle={setOpenArchiveFormat}
onSelect={(_, value) => { onSelect={(_, value) => {
onChange(value.toString()); field.onChange(value.toString());
setOpenArchiveFormat(false); setOpenArchiveFormat(false);
}} }}
selections={value} selections={field.value}
variant={SelectVariant.single} variant={SelectVariant.single}
aria-label={t("archiveFormat")} aria-label={t("archiveFormat")}
isOpen={openArchiveFormat} isOpen={openArchiveFormat}
@ -96,7 +100,7 @@ export const KeyForm = ({
.concat(hasPem ? CERT_PEM : []) .concat(hasPem ? CERT_PEM : [])
.map((option) => ( .map((option) => (
<SelectOption <SelectOption
selected={option === value} selected={option === field.value}
key={option} key={option}
value={option} value={option}
/> />
@ -120,15 +124,15 @@ export const KeyForm = ({
name="file" name="file"
defaultValue="" defaultValue=""
control={control} control={control}
render={({ onChange, value }) => ( render={({ field }) => (
<FileUpload <FileUpload
id="importFile" id="importFile"
value={value} value={field.value}
filename={filename} filename={filename}
browseButtonText={t("browse")} browseButtonText={t("browse")}
onChange={(value, filename) => { onChange={(value, filename) => {
setFilename(filename); setFilename(filename);
onChange(value); field.onChange(value);
}} }}
/> />
)} )}

View file

@ -1,5 +1,5 @@
import { useTranslation } from "react-i18next"; import { useTranslation } from "react-i18next";
import { FormProvider, useFormContext } from "react-hook-form"; import { FormProvider, useFormContext } from "react-hook-form-v7";
import { AlertVariant } from "@patternfly/react-core"; import { AlertVariant } from "@patternfly/react-core";
import type { KeyTypes } from "./SamlKeys"; import type { KeyTypes } from "./SamlKeys";
@ -21,7 +21,7 @@ export const SamlImportKeyDialog = ({
onClose, onClose,
}: SamlImportKeyDialogProps) => { }: SamlImportKeyDialogProps) => {
const { t } = useTranslation("clients"); const { t } = useTranslation("clients");
const form = useFormContext(); const form = useFormContext<SamlKeysDialogForm>();
const { handleSubmit } = form; const { handleSubmit } = form;
const { adminClient } = useAdminClient(); const { adminClient } = useAdminClient();

View file

@ -1,5 +1,5 @@
import { useTranslation } from "react-i18next"; import { useTranslation } from "react-i18next";
import { useFormContext } from "react-hook-form"; import { useFormContext } from "react-hook-form-v7";
import { FormGroup } from "@patternfly/react-core"; import { FormGroup } from "@patternfly/react-core";
import type KeyStoreConfig from "@keycloak/keycloak-admin-client/lib/defs/keystoreConfig"; import type KeyStoreConfig from "@keycloak/keycloak-admin-client/lib/defs/keystoreConfig";
@ -15,7 +15,10 @@ export const StoreSettings = ({
isSaml?: boolean; isSaml?: boolean;
}) => { }) => {
const { t } = useTranslation("clients"); const { t } = useTranslation("clients");
const { register, errors } = useFormContext<KeyStoreConfig>(); const {
register,
formState: { errors },
} = useFormContext<KeyStoreConfig>();
return ( return (
<> <>
@ -34,11 +37,9 @@ export const StoreSettings = ({
> >
<KeycloakTextInput <KeycloakTextInput
data-testid="keyAlias" data-testid="keyAlias"
type="text"
id="keyAlias" id="keyAlias"
name="keyAlias"
ref={register({ required: true })}
validated={errors.keyAlias ? "error" : "default"} validated={errors.keyAlias ? "error" : "default"}
{...register("keyAlias", { required: true })}
/> />
</FormGroup> </FormGroup>
{!hidePassword && ( {!hidePassword && (
@ -58,9 +59,8 @@ export const StoreSettings = ({
<PasswordInput <PasswordInput
data-testid="keyPassword" data-testid="keyPassword"
id="keyPassword" id="keyPassword"
name="keyPassword"
ref={register({ required: true })}
validated={errors.keyPassword ? "error" : "default"} validated={errors.keyPassword ? "error" : "default"}
{...register("keyPassword", { required: true })}
/> />
</FormGroup> </FormGroup>
)} )}
@ -77,10 +77,8 @@ export const StoreSettings = ({
> >
<KeycloakTextInput <KeycloakTextInput
data-testid="realmCertificateAlias" data-testid="realmCertificateAlias"
type="text"
id="realmCertificateAlias" id="realmCertificateAlias"
name="realmAlias" {...register("realmAlias")}
ref={register()}
/> />
</FormGroup> </FormGroup>
)} )}
@ -100,9 +98,8 @@ export const StoreSettings = ({
<PasswordInput <PasswordInput
data-testid="storePassword" data-testid="storePassword"
id="storePassword" id="storePassword"
name="storePassword"
ref={register({ required: true })}
validated={errors.storePassword ? "error" : "default"} validated={errors.storePassword ? "error" : "default"}
{...register("storePassword", { required: true })}
/> />
</FormGroup> </FormGroup>
</> </>