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