Use react-form-hook v7 for bind flow dialog (#3776)

This commit is contained in:
Jon Koops 2022-11-16 13:35:49 +01:00 committed by GitHub
parent 71f2db02a9
commit edf79d41af
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,23 +1,23 @@
import { useTranslation } from "react-i18next"; import type RealmRepresentation from "@keycloak/keycloak-admin-client/lib/defs/realmRepresentation";
import { Controller, useForm } from "react-hook-form";
import { import {
Modal, AlertVariant,
Button, Button,
ButtonVariant, ButtonVariant,
Form, Form,
FormGroup, FormGroup,
Modal,
Select, Select,
SelectVariant,
SelectOption, SelectOption,
AlertVariant, SelectVariant,
} from "@patternfly/react-core"; } from "@patternfly/react-core";
import { Controller, useForm } from "react-hook-form-v7";
import { useTranslation } from "react-i18next";
import type RealmRepresentation from "@keycloak/keycloak-admin-client/lib/defs/realmRepresentation"; import { useAlerts } from "../components/alert/Alerts";
import { useAdminClient } from "../context/auth/AdminClient";
import { useRealm } from "../context/realm-context/RealmContext";
import useToggle from "../utils/useToggle"; import useToggle from "../utils/useToggle";
import { REALM_FLOWS } from "./AuthenticationSection"; import { REALM_FLOWS } from "./AuthenticationSection";
import { useRealm } from "../context/realm-context/RealmContext";
import { useAdminClient } from "../context/auth/AdminClient";
import { useAlerts } from "../components/alert/Alerts";
type BindingForm = { type BindingForm = {
bindingType: keyof RealmRepresentation; bindingType: keyof RealmRepresentation;
@ -31,13 +31,12 @@ type BindFlowDialogProps = {
export const BindFlowDialog = ({ flowAlias, onClose }: BindFlowDialogProps) => { export const BindFlowDialog = ({ flowAlias, onClose }: BindFlowDialogProps) => {
const { t } = useTranslation("authentication"); const { t } = useTranslation("authentication");
const { control, handleSubmit } = useForm<BindingForm>(); const { control, handleSubmit } = useForm<BindingForm>();
const { adminClient } = useAdminClient(); const { adminClient } = useAdminClient();
const { addAlert, addError } = useAlerts(); const { addAlert, addError } = useAlerts();
const { realm } = useRealm(); const { realm } = useRealm();
const [open, toggle] = useToggle(); const [open, toggleOpen] = useToggle();
const save = async ({ bindingType }: BindingForm) => { const onSubmit = async ({ bindingType }: BindingForm) => {
const realmRep = await adminClient.realms.findOne({ realm }); const realmRep = await adminClient.realms.findOne({ realm });
try { try {
@ -56,22 +55,14 @@ export const BindFlowDialog = ({ flowAlias, onClose }: BindFlowDialogProps) => {
return ( return (
<Modal <Modal
title={t("bindFlow")} title={t("bindFlow")}
isOpen
variant="small" variant="small"
onClose={onClose} onClose={onClose}
actions={[ actions={[
<Button <Button key="confirm" data-testid="save" type="submit" form="bind-form">
id="modal-confirm"
key="confirm"
data-testid="save"
type="submit"
form="bind-form"
>
{t("common:save")} {t("common:save")}
</Button>, </Button>,
<Button <Button
data-testid="cancel" data-testid="cancel"
id="modal-cancel"
key="cancel" key="cancel"
variant={ButtonVariant.link} variant={ButtonVariant.link}
onClick={onClose} onClick={onClose}
@ -79,24 +70,24 @@ export const BindFlowDialog = ({ flowAlias, onClose }: BindFlowDialogProps) => {
{t("common:cancel")} {t("common:cancel")}
</Button>, </Button>,
]} ]}
isOpen
> >
<Form id="bind-form" isHorizontal onSubmit={handleSubmit(save)}> <Form id="bind-form" isHorizontal onSubmit={handleSubmit(onSubmit)}>
<FormGroup label={t("chooseBindingType")} fieldId="chooseBindingType"> <FormGroup label={t("chooseBindingType")} fieldId="chooseBindingType">
<Controller <Controller
name="bindingType" name="bindingType"
defaultValue={"browserFlow"} defaultValue="browserFlow"
control={control} control={control}
render={({ onChange, value }) => ( render={({ field }) => (
<Select <Select
toggleId="chooseBindingType" toggleId="chooseBindingType"
onToggle={toggle} onToggle={toggleOpen}
onSelect={(_, value) => { onSelect={(_, value) => {
onChange(value.toString()); field.onChange(value.toString());
toggle(); toggleOpen();
}} }}
selections={value} selections={field.value}
variant={SelectVariant.single} variant={SelectVariant.single}
aria-label={t("bindingFlow")}
isOpen={open} isOpen={open}
menuAppendTo="parent" menuAppendTo="parent"
> >