2021-09-20 15:31:05 +00:00
|
|
|
import React, { useState } from "react";
|
|
|
|
import { useTranslation } from "react-i18next";
|
|
|
|
import { Controller, useFormContext } from "react-hook-form";
|
|
|
|
import {
|
|
|
|
FormGroup,
|
|
|
|
Select,
|
|
|
|
SelectOption,
|
|
|
|
SelectVariant,
|
|
|
|
} from "@patternfly/react-core";
|
|
|
|
|
|
|
|
import type { ComponentProps } from "./components";
|
2021-11-08 09:46:03 +00:00
|
|
|
import { HelpItem } from "../help-enabler/HelpItem";
|
|
|
|
import { convertToHyphens } from "../../util";
|
2021-09-20 15:31:05 +00:00
|
|
|
|
|
|
|
export const ListComponent = ({
|
|
|
|
name,
|
|
|
|
label,
|
|
|
|
helpText,
|
|
|
|
defaultValue,
|
|
|
|
options,
|
|
|
|
}: ComponentProps) => {
|
2021-11-08 09:46:03 +00:00
|
|
|
const { t } = useTranslation("dynamic");
|
2021-09-20 15:31:05 +00:00
|
|
|
const { control } = useFormContext();
|
|
|
|
const [open, setOpen] = useState(false);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<FormGroup
|
|
|
|
label={t(label!)}
|
|
|
|
labelIcon={
|
|
|
|
<HelpItem helpText={t(helpText!)} forLabel={t(label!)} forID={name!} />
|
|
|
|
}
|
|
|
|
fieldId={name!}
|
|
|
|
>
|
|
|
|
<Controller
|
2021-11-01 22:18:27 +00:00
|
|
|
name={`config.${convertToHyphens(name!)}`}
|
2021-10-26 20:16:19 +00:00
|
|
|
data-testid={name}
|
2021-09-20 15:31:05 +00:00
|
|
|
defaultValue={defaultValue || ""}
|
|
|
|
control={control}
|
|
|
|
render={({ onChange, value }) => (
|
|
|
|
<Select
|
|
|
|
toggleId={name}
|
|
|
|
onToggle={(toggle) => setOpen(toggle)}
|
|
|
|
onSelect={(_, value) => {
|
|
|
|
onChange(value as string);
|
|
|
|
setOpen(false);
|
|
|
|
}}
|
|
|
|
selections={value}
|
|
|
|
variant={SelectVariant.single}
|
|
|
|
aria-label={t(label!)}
|
|
|
|
isOpen={open}
|
|
|
|
>
|
2021-11-10 14:26:43 +00:00
|
|
|
{options?.map((option) => (
|
2021-09-20 15:31:05 +00:00
|
|
|
<SelectOption
|
|
|
|
selected={option === value}
|
|
|
|
key={option}
|
|
|
|
value={option}
|
|
|
|
/>
|
|
|
|
))}
|
|
|
|
</Select>
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
</FormGroup>
|
|
|
|
);
|
|
|
|
};
|