2022-01-12 16:01:54 +00:00
|
|
|
import React from "react";
|
|
|
|
import { useTranslation } from "react-i18next";
|
|
|
|
import { Controller, useForm } from "react-hook-form";
|
|
|
|
import {
|
2022-01-24 14:17:32 +00:00
|
|
|
ActionGroup,
|
|
|
|
Button,
|
2022-01-12 16:01:54 +00:00
|
|
|
Dropdown,
|
|
|
|
DropdownToggle,
|
|
|
|
Form,
|
|
|
|
FormGroup,
|
|
|
|
Select,
|
|
|
|
SelectOption,
|
|
|
|
SelectVariant,
|
|
|
|
TextInput,
|
|
|
|
} from "@patternfly/react-core";
|
|
|
|
|
|
|
|
import type PolicyProviderRepresentation from "@keycloak/keycloak-admin-client/lib/defs/policyProviderRepresentation";
|
|
|
|
import useToggle from "../../utils/useToggle";
|
|
|
|
|
|
|
|
import "./search-dropdown.css";
|
|
|
|
|
2022-01-24 14:17:32 +00:00
|
|
|
export type SearchForm = {
|
|
|
|
name?: string;
|
|
|
|
resource?: string;
|
|
|
|
scope?: string;
|
|
|
|
type?: string;
|
|
|
|
};
|
|
|
|
|
2022-01-12 16:01:54 +00:00
|
|
|
type SearchDropdownProps = {
|
|
|
|
types?: PolicyProviderRepresentation[];
|
2022-01-24 14:17:32 +00:00
|
|
|
onSearch: (form: SearchForm) => void;
|
2022-01-12 16:01:54 +00:00
|
|
|
};
|
|
|
|
|
2022-01-24 14:17:32 +00:00
|
|
|
export const SearchDropdown = ({ types, onSearch }: SearchDropdownProps) => {
|
2022-01-12 16:01:54 +00:00
|
|
|
const { t } = useTranslation("clients");
|
2022-01-24 14:17:32 +00:00
|
|
|
const {
|
|
|
|
register,
|
|
|
|
control,
|
|
|
|
formState: { isDirty },
|
|
|
|
handleSubmit,
|
|
|
|
} = useForm<SearchForm>({ mode: "onChange" });
|
2022-01-12 16:01:54 +00:00
|
|
|
|
|
|
|
const [open, toggle] = useToggle();
|
|
|
|
const [typeOpen, toggleType] = useToggle();
|
|
|
|
|
2022-01-24 14:17:32 +00:00
|
|
|
const submit = (form: SearchForm) => {
|
|
|
|
toggle();
|
|
|
|
onSearch(form);
|
|
|
|
};
|
|
|
|
|
|
|
|
const typeOptions = (value: string) => [
|
|
|
|
<SelectOption key="empty" value="">
|
|
|
|
{t("allTypes")}
|
|
|
|
</SelectOption>,
|
|
|
|
...(types || []).map((type) => (
|
|
|
|
<SelectOption
|
|
|
|
selected={type.type === value}
|
|
|
|
key={type.type}
|
|
|
|
value={type.type}
|
|
|
|
>
|
|
|
|
{type.name}
|
|
|
|
</SelectOption>
|
|
|
|
)),
|
|
|
|
];
|
|
|
|
|
2022-01-12 16:01:54 +00:00
|
|
|
return (
|
|
|
|
<Dropdown
|
|
|
|
data-testid="searchdropdown_dorpdown"
|
|
|
|
className="pf-u-ml-md"
|
|
|
|
toggle={
|
|
|
|
<DropdownToggle
|
|
|
|
onToggle={toggle}
|
|
|
|
className="keycloak__client_authentication__searchdropdown"
|
|
|
|
>
|
|
|
|
{t("searchForPermission")}
|
|
|
|
</DropdownToggle>
|
|
|
|
}
|
|
|
|
isOpen={open}
|
|
|
|
>
|
|
|
|
<Form
|
|
|
|
isHorizontal
|
|
|
|
className="keycloak__client_authentication__searchdropdown_form"
|
2022-01-24 14:17:32 +00:00
|
|
|
onSubmit={handleSubmit(submit)}
|
2022-01-12 16:01:54 +00:00
|
|
|
>
|
|
|
|
<FormGroup label={t("common:name")} fieldId="name">
|
|
|
|
<TextInput
|
|
|
|
ref={register}
|
|
|
|
type="text"
|
|
|
|
id="name"
|
|
|
|
name="name"
|
|
|
|
data-testid="searchdropdown_name"
|
|
|
|
/>
|
|
|
|
</FormGroup>
|
2022-01-24 14:17:32 +00:00
|
|
|
<FormGroup label={t("resource")} fieldId="resource">
|
|
|
|
<TextInput
|
|
|
|
ref={register}
|
|
|
|
type="text"
|
|
|
|
id="resource"
|
|
|
|
name="resource"
|
|
|
|
data-testid="searchdropdown_resource"
|
|
|
|
/>
|
|
|
|
</FormGroup>
|
|
|
|
<FormGroup label={t("scope")} fieldId="scope">
|
|
|
|
<TextInput
|
|
|
|
ref={register}
|
|
|
|
type="text"
|
|
|
|
id="scope"
|
|
|
|
name="scope"
|
|
|
|
data-testid="searchdropdown_scope"
|
|
|
|
/>
|
|
|
|
</FormGroup>
|
2022-01-12 16:01:54 +00:00
|
|
|
<FormGroup label={t("common:type")} fieldId="type">
|
|
|
|
<Controller
|
|
|
|
name="type"
|
|
|
|
defaultValue=""
|
|
|
|
control={control}
|
|
|
|
render={({ onChange, value }) => (
|
|
|
|
<Select
|
|
|
|
toggleId="type"
|
|
|
|
onToggle={toggleType}
|
|
|
|
onSelect={(event, value) => {
|
|
|
|
event.stopPropagation();
|
|
|
|
onChange(value);
|
|
|
|
toggleType();
|
|
|
|
}}
|
2022-01-24 14:17:32 +00:00
|
|
|
selections={value || t("allTypes")}
|
2022-01-12 16:01:54 +00:00
|
|
|
variant={SelectVariant.single}
|
|
|
|
aria-label={t("common:type")}
|
|
|
|
isOpen={typeOpen}
|
|
|
|
>
|
2022-01-24 14:17:32 +00:00
|
|
|
{typeOptions(value)}
|
2022-01-12 16:01:54 +00:00
|
|
|
</Select>
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
</FormGroup>
|
2022-01-24 14:17:32 +00:00
|
|
|
<ActionGroup>
|
|
|
|
<Button
|
|
|
|
variant="primary"
|
|
|
|
type="submit"
|
|
|
|
data-testid="search-btn"
|
|
|
|
isDisabled={!isDirty}
|
|
|
|
>
|
|
|
|
{t("common:search")}
|
|
|
|
</Button>
|
|
|
|
<Button
|
|
|
|
variant="link"
|
|
|
|
data-testid="revert-btn"
|
|
|
|
onClick={() => onSearch({})}
|
|
|
|
>
|
|
|
|
{t("common:clear")}
|
|
|
|
</Button>
|
|
|
|
</ActionGroup>
|
2022-01-12 16:01:54 +00:00
|
|
|
</Form>
|
|
|
|
</Dropdown>
|
|
|
|
);
|
|
|
|
};
|