keycloak-scim/js/apps/admin-ui/src/identity-providers/component/SwitchField.tsx
Jon Koops d3c2475041
Upgrade admin and account console to PatternFly 5 (#28196)
Closes #21345
Closes #21344

Signed-off-by: Jon Koops <jonkoops@gmail.com>
Co-authored-by: Erik Jan de Wit <erikjan.dewit@gmail.com>
Co-authored-by: Mark Franceschelli <mfrances@redhat.com>
Co-authored-by: Hynek Mlnařík <hmlnarik@redhat.com>
Co-authored-by: Agnieszka Gancarczyk <agancarc@redhat.com>
2024-04-05 16:37:05 +02:00

51 lines
1.3 KiB
TypeScript

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