2022-11-03 14:06:26 +00:00
|
|
|
import { FormGroup, ValidatedOptions } from "@patternfly/react-core";
|
|
|
|
import {
|
|
|
|
FieldPath,
|
|
|
|
FieldValues,
|
|
|
|
PathValue,
|
|
|
|
useController,
|
|
|
|
UseControllerProps,
|
|
|
|
} from "react-hook-form";
|
|
|
|
|
|
|
|
import { KeycloakTextInput } from "../keycloak-text-input/KeycloakTextInput";
|
2023-03-07 09:29:40 +00:00
|
|
|
import { HelpItem } from "./HelpItem";
|
2022-11-03 14:06:26 +00:00
|
|
|
|
2023-02-07 11:29:30 +00:00
|
|
|
export type TextControlProps<
|
2022-11-03 14:06:26 +00:00
|
|
|
T extends FieldValues,
|
|
|
|
P extends FieldPath<T> = FieldPath<T>
|
|
|
|
> = UseControllerProps<T, P> & {
|
|
|
|
label: string;
|
2023-03-07 09:29:40 +00:00
|
|
|
labelIcon?: string;
|
2023-03-14 11:38:22 +00:00
|
|
|
isDisabled?: boolean;
|
2022-11-03 14:06:26 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
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 (
|
|
|
|
<FormGroup
|
|
|
|
isRequired={required}
|
|
|
|
label={props.label}
|
2023-03-07 09:29:40 +00:00
|
|
|
labelIcon={
|
|
|
|
props.labelIcon ? (
|
|
|
|
<HelpItem helpText={props.labelIcon} fieldLabelId={props.name} />
|
|
|
|
) : undefined
|
|
|
|
}
|
2022-11-03 14:06:26 +00:00
|
|
|
fieldId={props.name}
|
|
|
|
helperTextInvalid={fieldState.error?.message}
|
|
|
|
validated={
|
|
|
|
fieldState.error ? ValidatedOptions.error : ValidatedOptions.default
|
|
|
|
}
|
|
|
|
>
|
|
|
|
<KeycloakTextInput
|
|
|
|
isRequired={required}
|
|
|
|
id={props.name}
|
|
|
|
validated={
|
|
|
|
fieldState.error ? ValidatedOptions.error : ValidatedOptions.default
|
|
|
|
}
|
2023-03-14 11:38:22 +00:00
|
|
|
isDisabled={props.isDisabled}
|
2022-11-03 14:06:26 +00:00
|
|
|
{...field}
|
|
|
|
/>
|
|
|
|
</FormGroup>
|
|
|
|
);
|
|
|
|
};
|