Use react-hook-form v7 for add sub-flow modal (#3868)

This commit is contained in:
Jon Koops 2022-11-25 14:48:17 +01:00 committed by GitHub
parent 6b63f83fd3
commit 69fa367bf9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,6 +1,4 @@
import { useState } from "react"; import type { AuthenticationProviderRepresentation } from "@keycloak/keycloak-admin-client/lib/defs/authenticatorConfigRepresentation";
import { useTranslation } from "react-i18next";
import { Controller, useForm } from "react-hook-form";
import { import {
Button, Button,
ButtonVariant, ButtonVariant,
@ -13,8 +11,10 @@ import {
SelectVariant, SelectVariant,
ValidatedOptions, ValidatedOptions,
} from "@patternfly/react-core"; } from "@patternfly/react-core";
import { useEffect, useState } from "react";
import { Controller, useForm } from "react-hook-form-v7";
import { useTranslation } from "react-i18next";
import type { AuthenticationProviderRepresentation } from "@keycloak/keycloak-admin-client/lib/defs/authenticatorConfigRepresentation";
import { HelpItem } from "../../../components/help-enabler/HelpItem"; import { HelpItem } from "../../../components/help-enabler/HelpItem";
import { KeycloakTextInput } from "../../../components/keycloak-text-input/KeycloakTextInput"; import { KeycloakTextInput } from "../../../components/keycloak-text-input/KeycloakTextInput";
import { useAdminClient, useFetch } from "../../../context/auth/AdminClient"; import { useAdminClient, useFetch } from "../../../context/auth/AdminClient";
@ -29,7 +29,7 @@ const types = ["basic-flow", "form-flow"] as const;
export type Flow = { export type Flow = {
name: string; name: string;
description?: string; description: string;
type: typeof types[number]; type: typeof types[number];
provider: string; provider: string;
}; };
@ -43,6 +43,7 @@ export const AddSubFlowModal = ({
const { const {
register, register,
control, control,
setValue,
handleSubmit, handleSubmit,
formState: { errors }, formState: { errors },
} = useForm<Flow>(); } = useForm<Flow>();
@ -54,40 +55,42 @@ export const AddSubFlowModal = ({
useFetch( useFetch(
() => adminClient.authenticationManagement.getFormProviders(), () => adminClient.authenticationManagement.getFormProviders(),
(providers) => setFormProviders(providers), setFormProviders,
[] []
); );
useEffect(() => {
if (formProviders?.length === 1) {
setValue("provider", formProviders[0].id!);
}
}, [formProviders]);
return ( return (
<Modal <Modal
variant={ModalVariant.medium} variant={ModalVariant.medium}
isOpen={true}
title={t("addStepTo", { name })} title={t("addStepTo", { name })}
onClose={() => onCancel()} onClose={onCancel}
actions={[ actions={[
<Button <Button
id="modal-add"
data-testid="modal-add"
key="add" key="add"
data-testid="modal-add"
type="submit" type="submit"
form="sub-flow-form" form="sub-flow-form"
> >
{t("common:add")} {t("common:add")}
</Button>, </Button>,
<Button <Button
data-testid="cancel"
id="modal-cancel"
key="cancel" key="cancel"
data-testid="cancel"
variant={ButtonVariant.link} variant={ButtonVariant.link}
onClick={() => { onClick={onCancel}
onCancel();
}}
> >
{t("common:cancel")} {t("common:cancel")}
</Button>, </Button>,
]} ]}
isOpen
> >
<Form id="sub-flow-form" isHorizontal onSubmit={handleSubmit(onConfirm)}> <Form id="sub-flow-form" onSubmit={handleSubmit(onConfirm)} isHorizontal>
<FormGroup <FormGroup
label={t("common:name")} label={t("common:name")}
fieldId="name" fieldId="name"
@ -95,20 +98,18 @@ export const AddSubFlowModal = ({
validated={ validated={
errors.name ? ValidatedOptions.error : ValidatedOptions.default errors.name ? ValidatedOptions.error : ValidatedOptions.default
} }
isRequired
labelIcon={ labelIcon={
<HelpItem helpText="authentication-help:name" fieldLabelId="name" /> <HelpItem helpText="authentication-help:name" fieldLabelId="name" />
} }
isRequired
> >
<KeycloakTextInput <KeycloakTextInput
type="text"
id="name" id="name"
name="name"
data-testid="name" data-testid="name"
ref={register({ required: true })}
validated={ validated={
errors.name ? ValidatedOptions.error : ValidatedOptions.default errors.name ? ValidatedOptions.error : ValidatedOptions.default
} }
{...register("name", { required: true })}
/> />
</FormGroup> </FormGroup>
<FormGroup <FormGroup
@ -122,46 +123,43 @@ export const AddSubFlowModal = ({
} }
> >
<KeycloakTextInput <KeycloakTextInput
type="text"
id="description" id="description"
name="description"
data-testid="description" data-testid="description"
ref={register} {...register("description")}
/> />
</FormGroup> </FormGroup>
<FormGroup <FormGroup
label={t("flowType")} label={t("flowType")}
fieldId="flowType"
labelIcon={ labelIcon={
<HelpItem <HelpItem
helpText="authentication-help:flowType" helpText="authentication-help:flowType"
fieldLabelId="authentication:flowType" fieldLabelId="authentication:flowType"
/> />
} }
fieldId="flowType"
> >
<Controller <Controller
name="type" name="type"
defaultValue={types[0]} defaultValue={types[0]}
control={control} control={control}
render={({ onChange, value }) => ( render={({ field }) => (
<Select <Select
menuAppendTo="parent" menuAppendTo="parent"
toggleId="flowType" toggleId="flowType"
onToggle={setOpen} onToggle={setOpen}
onSelect={(_, value) => { onSelect={(_, value) => {
onChange(value as string); field.onChange(value.toString());
setOpen(false); setOpen(false);
}} }}
selections={t(`flow-type.${value}`)} selections={t(`flow-type.${field.value}`)}
variant={SelectVariant.single} variant={SelectVariant.single}
aria-label={t("flowType")}
isOpen={open} isOpen={open}
> >
{types.map((type) => ( {types.map((type) => (
<SelectOption <SelectOption
selected={type === value}
key={type} key={type}
value={type} value={type}
selected={type === field.value}
> >
{t(`flow-type.${type}`)} {t(`flow-type.${type}`)}
</SelectOption> </SelectOption>
@ -185,25 +183,24 @@ export const AddSubFlowModal = ({
name="provider" name="provider"
defaultValue="" defaultValue=""
control={control} control={control}
render={({ onChange, value }) => ( render={({ field }) => (
<Select <Select
menuAppendTo="parent" menuAppendTo="parent"
toggleId="provider" toggleId="provider"
onToggle={setOpenProvider} onToggle={setOpenProvider}
onSelect={(_, value) => { onSelect={(_, value) => {
onChange(value.toString()); field.onChange(value.toString());
setOpenProvider(false); setOpenProvider(false);
}} }}
selections={value.displayName} selections={field.value}
variant={SelectVariant.single} variant={SelectVariant.single}
aria-label={t("flowType")}
isOpen={openProvider} isOpen={openProvider}
> >
{formProviders.map((provider) => ( {formProviders.map((provider) => (
<SelectOption <SelectOption
selected={provider.displayName === value}
key={provider.id} key={provider.id}
value={provider.id} value={provider.id}
selected={provider.displayName === field.value}
> >
{provider.displayName} {provider.displayName}
</SelectOption> </SelectOption>
@ -213,14 +210,6 @@ export const AddSubFlowModal = ({
/> />
</FormGroup> </FormGroup>
)} )}
{formProviders?.length === 1 && (
<input
name="provider"
type="hidden"
ref={register}
value={formProviders[0].id}
/>
)}
</Form> </Form>
</Modal> </Modal>
); );