2021-09-20 15:31:05 +00:00
|
|
|
import { Controller, useFormContext } from "react-hook-form";
|
|
|
|
import { useTranslation } from "react-i18next";
|
|
|
|
import { FormGroup, Switch } from "@patternfly/react-core";
|
|
|
|
|
|
|
|
import type { ComponentProps } from "./components";
|
2021-11-08 09:46:03 +00:00
|
|
|
import { HelpItem } from "../help-enabler/HelpItem";
|
2022-08-09 08:32:16 +00:00
|
|
|
import { convertToName } from "./DynamicComponents";
|
2021-09-20 15:31:05 +00:00
|
|
|
|
|
|
|
export const BooleanComponent = ({
|
|
|
|
name,
|
|
|
|
label,
|
|
|
|
helpText,
|
|
|
|
defaultValue,
|
2022-02-16 14:53:45 +00:00
|
|
|
isDisabled = false,
|
2021-09-20 15:31:05 +00:00
|
|
|
}: ComponentProps) => {
|
2021-12-10 12:27:04 +00:00
|
|
|
const { t } = useTranslation("dynamic");
|
2021-09-20 15:31:05 +00:00
|
|
|
const { control } = useFormContext();
|
|
|
|
|
|
|
|
return (
|
|
|
|
<FormGroup
|
|
|
|
hasNoPaddingTop
|
|
|
|
label={t(label!)}
|
|
|
|
fieldId={name!}
|
|
|
|
labelIcon={
|
2021-12-14 14:56:36 +00:00
|
|
|
<HelpItem helpText={t(helpText!)} fieldLabelId={`dynamic:${label}`} />
|
2021-09-20 15:31:05 +00:00
|
|
|
}
|
|
|
|
>
|
|
|
|
<Controller
|
2022-08-09 08:32:16 +00:00
|
|
|
name={convertToName(name!)}
|
2021-10-26 20:16:19 +00:00
|
|
|
data-testid={name}
|
2021-12-14 22:46:19 +00:00
|
|
|
defaultValue={defaultValue || false}
|
2021-09-20 15:31:05 +00:00
|
|
|
control={control}
|
|
|
|
render={({ onChange, value }) => (
|
|
|
|
<Switch
|
|
|
|
id={name!}
|
2022-02-16 14:53:45 +00:00
|
|
|
isDisabled={isDisabled}
|
2021-09-20 15:31:05 +00:00
|
|
|
label={t("common:on")}
|
|
|
|
labelOff={t("common:off")}
|
2021-10-26 20:16:19 +00:00
|
|
|
isChecked={value === "true" || value === true}
|
2021-09-20 15:31:05 +00:00
|
|
|
onChange={(value) => onChange("" + value)}
|
2021-12-14 22:46:19 +00:00
|
|
|
data-testid={name}
|
2021-09-20 15:31:05 +00:00
|
|
|
/>
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
</FormGroup>
|
|
|
|
);
|
|
|
|
};
|