Use react-hook-form v7 for edit flow modal (#3789)

This commit is contained in:
Jon Koops 2022-11-18 12:47:16 +01:00 committed by GitHub
parent 44891c14cd
commit d0ba084906
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,6 +1,3 @@
import { useEffect } from "react";
import { useTranslation } from "react-i18next";
import { useForm } from "react-hook-form";
import { import {
Button, Button,
ButtonVariant, ButtonVariant,
@ -12,33 +9,40 @@ import {
ValidatedOptions, ValidatedOptions,
} from "@patternfly/react-core"; } from "@patternfly/react-core";
import { PencilAltIcon } from "@patternfly/react-icons"; import { PencilAltIcon } from "@patternfly/react-icons";
import { useEffect } from "react";
import { useForm } from "react-hook-form-v7";
import { useTranslation } from "react-i18next";
import type { ExpandableExecution } from "../execution-model";
import useToggle from "../../utils/useToggle";
import { HelpItem } from "../../components/help-enabler/HelpItem"; import { HelpItem } from "../../components/help-enabler/HelpItem";
import { KeycloakTextInput } from "../../components/keycloak-text-input/KeycloakTextInput";
import { KeycloakTextArea } from "../../components/keycloak-text-area/KeycloakTextArea"; import { KeycloakTextArea } from "../../components/keycloak-text-area/KeycloakTextArea";
import { KeycloakTextInput } from "../../components/keycloak-text-input/KeycloakTextInput";
import useToggle from "../../utils/useToggle";
import type { ExpandableExecution } from "../execution-model";
type EditFlowProps = { type EditFlowProps = {
execution: ExpandableExecution; execution: ExpandableExecution;
onRowChange: (execution: ExpandableExecution) => void; onRowChange: (execution: ExpandableExecution) => void;
}; };
type FormFields = Omit<ExpandableExecution, "executionList">;
export const EditFlow = ({ execution, onRowChange }: EditFlowProps) => { export const EditFlow = ({ execution, onRowChange }: EditFlowProps) => {
const { t } = useTranslation("authentication"); const { t } = useTranslation("authentication");
const { register, errors, reset, handleSubmit } = useForm(); const {
register,
reset,
handleSubmit,
formState: { errors, isValid },
} = useForm<FormFields>({ mode: "onChange", defaultValues: execution });
const [show, toggle] = useToggle(); const [show, toggle] = useToggle();
const update = (values: ExpandableExecution) => { useEffect(() => reset(execution), [execution]);
onRowChange({ ...execution, ...values });
const onSubmit = (formValues: FormFields) => {
onRowChange({ ...execution, ...formValues });
toggle(); toggle();
}; };
useEffect(() => {
reset(execution);
}, [execution, reset]);
return ( return (
<> <>
<Tooltip content={t("common:edit")}> <Tooltip content={t("common:edit")}>
@ -54,21 +58,20 @@ export const EditFlow = ({ execution, onRowChange }: EditFlowProps) => {
{show && ( {show && (
<Modal <Modal
title={t("editFlow")} title={t("editFlow")}
isOpen={true}
onClose={toggle} onClose={toggle}
variant={ModalVariant.small} variant={ModalVariant.small}
actions={[ actions={[
<Button <Button
id="modal-confirm"
key="confirm" key="confirm"
onClick={handleSubmit(update)}
data-testid="confirm" data-testid="confirm"
type="submit"
form="edit-flow-form"
isDisabled={!isValid}
> >
{t("edit")} {t("edit")}
</Button>, </Button>,
<Button <Button
data-testid="cancel" data-testid="cancel"
id="modal-cancel"
key="cancel" key="cancel"
variant={ButtonVariant.link} variant={ButtonVariant.link}
onClick={toggle} onClick={toggle}
@ -76,8 +79,13 @@ export const EditFlow = ({ execution, onRowChange }: EditFlowProps) => {
{t("common:cancel")} {t("common:cancel")}
</Button>, </Button>,
]} ]}
isOpen
> >
<Form isHorizontal> <Form
id="edit-flow-form"
onSubmit={handleSubmit(onSubmit)}
isHorizontal
>
<FormGroup <FormGroup
label={t("common:name")} label={t("common:name")}
fieldId="name" fieldId="name"
@ -87,25 +95,24 @@ export const EditFlow = ({ execution, onRowChange }: EditFlowProps) => {
? ValidatedOptions.error ? ValidatedOptions.error
: ValidatedOptions.default : ValidatedOptions.default
} }
isRequired
labelIcon={ labelIcon={
<HelpItem <HelpItem
helpText="authentication-help:name" helpText="authentication-help:name"
fieldLabelId="name" fieldLabelId="name"
/> />
} }
isRequired
> >
<KeycloakTextInput <KeycloakTextInput
type="text"
id="name" id="name"
name="displayName"
data-testid="displayName" data-testid="displayName"
ref={register({ required: true })}
validated={ validated={
errors.displayName errors.displayName
? ValidatedOptions.error ? ValidatedOptions.error
: ValidatedOptions.default : ValidatedOptions.default
} }
{...register("displayName", { required: true })}
isRequired
/> />
</FormGroup> </FormGroup>
<FormGroup <FormGroup
@ -125,21 +132,19 @@ export const EditFlow = ({ execution, onRowChange }: EditFlowProps) => {
helperTextInvalid={errors.description?.message} helperTextInvalid={errors.description?.message}
> >
<KeycloakTextArea <KeycloakTextArea
ref={register({
maxLength: {
value: 255,
message: t("common:maxLength", { length: 255 }),
},
})}
type="text"
id="kc-description" id="kc-description"
name="description"
data-testid="description" data-testid="description"
validated={ validated={
errors.description errors.description
? ValidatedOptions.error ? ValidatedOptions.error
: ValidatedOptions.default : ValidatedOptions.default
} }
{...register("description", {
maxLength: {
value: 255,
message: t("common:maxLength", { length: 255 }),
},
})}
/> />
</FormGroup> </FormGroup>
</Form> </Form>