Keycloak
1import { TextInputProps, ValidatedOptions } from "@patternfly/react-core";
2import {
3FieldPath,
4FieldValues,
5PathValue,
6useController,
7UseControllerProps,
8} from "react-hook-form";
9
10import { KeycloakTextInput } from "../keycloak-text-input/KeycloakTextInput";
11import { FormLabel } from "./FormLabel";
12
13export type TextControlProps<
14T extends FieldValues,
15P extends FieldPath<T> = FieldPath<T>,
16> = UseControllerProps<T, P> &
17Omit<TextInputProps, "name" | "isRequired" | "required"> & {
18label: string;
19labelIcon?: string;
20isDisabled?: boolean;
21};
22
23export const TextControl = <
24T extends FieldValues,
25P extends FieldPath<T> = FieldPath<T>,
26>(
27props: TextControlProps<T, P>,
28) => {
29const { labelIcon, ...rest } = props;
30const required = !!props.rules?.required;
31const defaultValue = props.defaultValue ?? ("" as PathValue<T, P>);
32
33const { field, fieldState } = useController({
34...props,
35defaultValue,
36});
37
38return (
39<FormLabel
40name={props.name}
41label={props.label}
42labelIcon={labelIcon}
43isRequired={required}
44error={fieldState.error}
45>
46<KeycloakTextInput
47isRequired={required}
48id={props.name}
49data-testid={props.name}
50validated={
51fieldState.error ? ValidatedOptions.error : ValidatedOptions.default
52}
53isDisabled={props.isDisabled}
54{...rest}
55{...field}
56/>
57</FormLabel>
58);
59};
60