Use react-hook-form v7 for search dropdown form (#3955)

This commit is contained in:
Jon Koops 2022-12-07 10:03:38 +01:00 committed by GitHub
parent e5cd8b30d4
commit b4d001bd82
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,6 +1,4 @@
import { useEffect } from "react"; import type PolicyProviderRepresentation from "@keycloak/keycloak-admin-client/lib/defs/policyProviderRepresentation";
import { useTranslation } from "react-i18next";
import { Controller, useForm } from "react-hook-form";
import { import {
ActionGroup, ActionGroup,
Button, Button,
@ -12,10 +10,12 @@ import {
SelectOption, SelectOption,
SelectVariant, SelectVariant,
} from "@patternfly/react-core"; } from "@patternfly/react-core";
import { useEffect } from "react";
import { Controller, useForm } from "react-hook-form-v7";
import { useTranslation } from "react-i18next";
import type PolicyProviderRepresentation from "@keycloak/keycloak-admin-client/lib/defs/policyProviderRepresentation";
import useToggle from "../../utils/useToggle";
import { KeycloakTextInput } from "../../components/keycloak-text-input/KeycloakTextInput"; import { KeycloakTextInput } from "../../components/keycloak-text-input/KeycloakTextInput";
import useToggle from "../../utils/useToggle";
import "./search-dropdown.css"; import "./search-dropdown.css";
@ -24,6 +24,8 @@ export type SearchForm = {
resource?: string; resource?: string;
scope?: string; scope?: string;
type?: string; type?: string;
uri?: string;
owner?: string;
}; };
type SearchDropdownProps = { type SearchDropdownProps = {
@ -58,7 +60,7 @@ export const SearchDropdown = ({
useEffect(() => reset(search), [search]); useEffect(() => reset(search), [search]);
const typeOptions = (value: string) => [ const typeOptions = (value?: string) => [
<SelectOption key="empty" value=""> <SelectOption key="empty" value="">
{t("allTypes")} {t("allTypes")}
</SelectOption>, </SelectOption>,
@ -94,40 +96,32 @@ export const SearchDropdown = ({
> >
<FormGroup label={t("common:name")} fieldId="name"> <FormGroup label={t("common:name")} fieldId="name">
<KeycloakTextInput <KeycloakTextInput
ref={register}
type="text"
id="name" id="name"
name="name"
data-testid="searchdropdown_name" data-testid="searchdropdown_name"
{...register("name")}
/> />
</FormGroup> </FormGroup>
{isResource && ( {isResource && (
<> <>
<FormGroup label={t("common:type")} fieldId="type"> <FormGroup label={t("common:type")} fieldId="type">
<KeycloakTextInput <KeycloakTextInput
ref={register}
type="text"
id="type" id="type"
name="type"
data-testid="searchdropdown_type" data-testid="searchdropdown_type"
{...register("type")}
/> />
</FormGroup> </FormGroup>
<FormGroup label={t("uris")} fieldId="uri"> <FormGroup label={t("uris")} fieldId="uri">
<KeycloakTextInput <KeycloakTextInput
ref={register}
type="text"
id="uri" id="uri"
name="uri"
data-testid="searchdropdown_uri" data-testid="searchdropdown_uri"
{...register("uri")}
/> />
</FormGroup> </FormGroup>
<FormGroup label={t("owner")} fieldId="owner"> <FormGroup label={t("owner")} fieldId="owner">
<KeycloakTextInput <KeycloakTextInput
ref={register}
type="text"
id="owner" id="owner"
name="owner"
data-testid="searchdropdown_owner" data-testid="searchdropdown_owner"
{...register("owner")}
/> />
</FormGroup> </FormGroup>
</> </>
@ -135,21 +129,17 @@ export const SearchDropdown = ({
{!isResource && ( {!isResource && (
<FormGroup label={t("resource")} fieldId="resource"> <FormGroup label={t("resource")} fieldId="resource">
<KeycloakTextInput <KeycloakTextInput
ref={register}
type="text"
id="resource" id="resource"
name="resource"
data-testid="searchdropdown_resource" data-testid="searchdropdown_resource"
{...register("resource")}
/> />
</FormGroup> </FormGroup>
)} )}
<FormGroup label={t("scope")} fieldId="scope"> <FormGroup label={t("scope")} fieldId="scope">
<KeycloakTextInput <KeycloakTextInput
ref={register}
type="text"
id="scope" id="scope"
name="scope"
data-testid="searchdropdown_scope" data-testid="searchdropdown_scope"
{...register("scope")}
/> />
</FormGroup> </FormGroup>
{!isResource && ( {!isResource && (
@ -158,21 +148,21 @@ export const SearchDropdown = ({
name="type" name="type"
defaultValue="" defaultValue=""
control={control} control={control}
render={({ onChange, value }) => ( render={({ field }) => (
<Select <Select
toggleId="type" toggleId="type"
onToggle={toggleType} onToggle={toggleType}
onSelect={(event, value) => { onSelect={(event, value) => {
event.stopPropagation(); event.stopPropagation();
onChange(value); field.onChange(value);
toggleType(); toggleType();
}} }}
selections={value || t("allTypes")} selections={field.value || t("allTypes")}
variant={SelectVariant.single} variant={SelectVariant.single}
aria-label={t("common:type")} aria-label={t("common:type")}
isOpen={typeOpen} isOpen={typeOpen}
> >
{typeOptions(value)} {typeOptions(field.value)}
</Select> </Select>
)} )}
/> />