keycloak-scim/apps/admin-ui/src/components/dynamic/BooleanComponent.tsx
agagancarczyk d81164e371
added aria-labels required by screen recorder (#3234)
Authored-by: Agnieszka Gancarczyk <agancarc@redhat.com>
2022-08-30 14:07:51 +01:00

48 lines
1.3 KiB
TypeScript

import { Controller, useFormContext } from "react-hook-form";
import { useTranslation } from "react-i18next";
import { FormGroup, Switch } from "@patternfly/react-core";
import type { ComponentProps } from "./components";
import { HelpItem } from "../help-enabler/HelpItem";
import { convertToName } from "./DynamicComponents";
export const BooleanComponent = ({
name,
label,
helpText,
defaultValue,
isDisabled = false,
}: ComponentProps) => {
const { t } = useTranslation("dynamic");
const { control } = useFormContext();
return (
<FormGroup
hasNoPaddingTop
label={t(label!)}
fieldId={name!}
labelIcon={
<HelpItem helpText={t(helpText!)} fieldLabelId={`dynamic:${label}`} />
}
>
<Controller
name={convertToName(name!)}
data-testid={name}
defaultValue={defaultValue || false}
control={control}
render={({ onChange, value }) => (
<Switch
id={name!}
isDisabled={isDisabled}
label={t("common:on")}
labelOff={t("common:off")}
isChecked={value === "true" || value === true}
onChange={(value) => onChange("" + value)}
data-testid={name}
aria-label={t(label!)}
/>
)}
/>
</FormGroup>
);
};