4fb2f73b2c
* migrated to use ui-shared Signed-off-by: Erik Jan de Wit <erikjan.dewit@gmail.com> * fixed tests Signed-off-by: Erik Jan de Wit <erikjan.dewit@gmail.com> * fixed tests Signed-off-by: Erik Jan de Wit <erikjan.dewit@gmail.com> --------- Signed-off-by: Erik Jan de Wit <erikjan.dewit@gmail.com>
62 lines
1.5 KiB
TypeScript
62 lines
1.5 KiB
TypeScript
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";
|
|
import { ReactNode } from "react";
|
|
|
|
export type TextControlProps<
|
|
T extends FieldValues,
|
|
P extends FieldPath<T> = FieldPath<T>,
|
|
> = UseControllerProps<T, P> &
|
|
Omit<TextInputProps, "name" | "isRequired" | "required"> & {
|
|
label: string;
|
|
labelIcon?: string | ReactNode;
|
|
isDisabled?: boolean;
|
|
helperText?: string;
|
|
};
|
|
|
|
export const TextControl = <
|
|
T extends FieldValues,
|
|
P extends FieldPath<T> = FieldPath<T>,
|
|
>(
|
|
props: TextControlProps<T, P>,
|
|
) => {
|
|
const { labelIcon, ...rest } = props;
|
|
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={labelIcon}
|
|
isRequired={required}
|
|
error={fieldState.error}
|
|
helperText={props.helperText}
|
|
>
|
|
<KeycloakTextInput
|
|
isRequired={required}
|
|
id={props.name}
|
|
data-testid={props.name}
|
|
validated={
|
|
fieldState.error ? ValidatedOptions.error : ValidatedOptions.default
|
|
}
|
|
isDisabled={props.isDisabled}
|
|
{...rest}
|
|
{...field}
|
|
/>
|
|
</FormLabel>
|
|
);
|
|
};
|