2021-05-06 18:59:00 +00:00
|
|
|
import { Switch } from "@patternfly/react-core";
|
2023-01-26 09:31:07 +00:00
|
|
|
import { Controller, useFormContext } from "react-hook-form";
|
2022-12-21 17:39:24 +00:00
|
|
|
import { useTranslation } from "react-i18next";
|
2021-05-06 18:59:00 +00:00
|
|
|
|
|
|
|
import { FieldProps, FormGroupField } from "./FormGroupField";
|
|
|
|
|
|
|
|
type FieldType = "boolean" | "string";
|
|
|
|
|
|
|
|
type SwitchFieldProps = FieldProps & {
|
|
|
|
fieldType?: FieldType;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const SwitchField = ({
|
|
|
|
label,
|
|
|
|
field,
|
|
|
|
fieldType = "string",
|
|
|
|
isReadOnly = false,
|
|
|
|
}: SwitchFieldProps) => {
|
2023-09-08 13:17:17 +00:00
|
|
|
const { t } = useTranslation();
|
2021-05-06 18:59:00 +00:00
|
|
|
const { control } = useFormContext();
|
|
|
|
return (
|
|
|
|
<FormGroupField label={label}>
|
|
|
|
<Controller
|
|
|
|
name={field}
|
|
|
|
defaultValue={fieldType === "string" ? "false" : false}
|
|
|
|
control={control}
|
2022-12-21 17:39:24 +00:00
|
|
|
render={({ field }) => (
|
2021-05-06 18:59:00 +00:00
|
|
|
<Switch
|
|
|
|
id={label}
|
|
|
|
label={t("common:on")}
|
|
|
|
labelOff={t("common:off")}
|
|
|
|
isChecked={
|
2022-12-21 17:39:24 +00:00
|
|
|
fieldType === "string"
|
|
|
|
? field.value === "true"
|
|
|
|
: (field.value as boolean)
|
2021-05-06 18:59:00 +00:00
|
|
|
}
|
|
|
|
onChange={(value) =>
|
2022-12-21 17:39:24 +00:00
|
|
|
field.onChange(fieldType === "string" ? "" + value : value)
|
2021-05-06 18:59:00 +00:00
|
|
|
}
|
2021-05-20 09:38:37 +00:00
|
|
|
isDisabled={isReadOnly}
|
2022-08-30 13:07:51 +00:00
|
|
|
aria-label={label}
|
2021-05-06 18:59:00 +00:00
|
|
|
/>
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
</FormGroupField>
|
|
|
|
);
|
|
|
|
};
|