keycloak-scim/js/libs/ui-shared/src/controls/TextControl.tsx

59 lines
1.3 KiB
TypeScript
Raw Normal View History

2023-06-12 10:52:45 +00:00
import { TextInputProps, ValidatedOptions } from "@patternfly/react-core";
import {
FieldPath,
FieldValues,
PathValue,
useController,
UseControllerProps,
} from "react-hook-form";
import { KeycloakTextInput } from "../keycloak-text-input/KeycloakTextInput";
import { FormLabel } from "./FormLabel";
export type TextControlProps<
T extends FieldValues,
P extends FieldPath<T> = FieldPath<T>
2023-06-12 10:52:45 +00:00
> = UseControllerProps<T, P> &
TextInputProps & {
label: string;
labelIcon?: string;
isDisabled?: boolean;
};
export const TextControl = <
T extends FieldValues,
P extends FieldPath<T> = FieldPath<T>
>(
props: TextControlProps<T, P>
) => {
const required = !!props.rules?.required;
const defaultValue = props.defaultValue ?? ("" as PathValue<T, P>);
const { field, fieldState } = useController({
...props,
defaultValue,
});
return (
<FormLabel
name={props.name}
label={props.label}
labelIcon={props.labelIcon}
isRequired={required}
error={fieldState.error}
>
<KeycloakTextInput
isRequired={required}
id={props.name}
data-testid={props.name}
validated={
fieldState.error ? ValidatedOptions.error : ValidatedOptions.default
}
2023-03-14 11:38:22 +00:00
isDisabled={props.isDisabled}
2023-06-12 10:52:45 +00:00
{...props}
{...field}
/>
</FormLabel>
);
};