Use TextControl in StringComponent (#31442)

* Use TextControl in StringComponent

This makes that the field is required and partially fixes:

Signed-off-by: Erik Jan de Wit <erikjan.dewit@gmail.com>
#26963

* enable test-id override

Signed-off-by: Erik Jan de Wit <erikjan.dewit@gmail.com>

---------

Signed-off-by: Erik Jan de Wit <erikjan.dewit@gmail.com>
This commit is contained in:
Erik Jan de Wit 2024-07-29 10:29:56 +02:00 committed by GitHub
parent 9b4b72e8f9
commit 0b5f42f95d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 15 additions and 27 deletions

View file

@ -10,7 +10,7 @@ export enum ClaimJsonType {
export default class MapperDetailsPage extends CommonPage { export default class MapperDetailsPage extends CommonPage {
#userAttributeInput = '[data-testid="config.user🍺attribute"]'; #userAttributeInput = '[data-testid="config.user🍺attribute"]';
#tokenClaimNameInput = '[id="claim.name"]'; #tokenClaimNameInput = '[data-testid="claim.name"]';
#claimJsonType = '[id="jsonType.label"]'; #claimJsonType = '[id="jsonType.label"]';
fillUserAttribute(userAttribute: string) { fillUserAttribute(userAttribute: string) {

View file

@ -207,7 +207,7 @@ export default class RealmSettingsPage extends CommonPage {
#availablePeriodExecutorFld = "available-period"; #availablePeriodExecutorFld = "available-period";
#editExecutorBtn = #editExecutorBtn =
'[aria-label="Executors"] > li > div:first-child [data-testid="editExecutor"]'; '[aria-label="Executors"] > li > div:first-child [data-testid="editExecutor"]';
#executorAvailablePeriodInput = "#available-period"; #executorAvailablePeriodInput = "[data-testid='available-period']";
#listingPage = new ListingPage(); #listingPage = new ListingPage();
#addCondition = "addCondition"; #addCondition = "addCondition";

View file

@ -1,8 +1,5 @@
import { FormGroup, TextInput } from "@patternfly/react-core"; import { TextControl } from "@keycloak/keycloak-ui-shared";
import { useFormContext } from "react-hook-form";
import { useTranslation } from "react-i18next"; import { useTranslation } from "react-i18next";
import { HelpItem } from "@keycloak/keycloak-ui-shared";
import { convertToName } from "./DynamicComponents"; import { convertToName } from "./DynamicComponents";
import type { ComponentProps } from "./components"; import type { ComponentProps } from "./components";
@ -10,27 +7,17 @@ export const StringComponent = ({
name, name,
label, label,
helpText, helpText,
defaultValue, ...props
isDisabled = false,
required,
}: ComponentProps) => { }: ComponentProps) => {
const { t } = useTranslation(); const { t } = useTranslation();
const { register } = useFormContext();
return ( return (
<FormGroup <TextControl
name={convertToName(name!)}
label={t(label!)} label={t(label!)}
labelIcon={<HelpItem helpText={t(helpText!)} fieldLabelId={`${label}`} />} helperText={t(helpText!)}
fieldId={name!} data-testid={name}
isRequired={required} {...props}
> />
<TextInput
id={name!}
data-testid={name}
isDisabled={isDisabled}
defaultValue={defaultValue?.toString()}
{...register(convertToName(name!))}
/>
</FormGroup>
); );
}; };

View file

@ -26,6 +26,7 @@ export type TextControlProps<
labelIcon?: string | ReactNode; labelIcon?: string | ReactNode;
isDisabled?: boolean; isDisabled?: boolean;
helperText?: string; helperText?: string;
"data-testid"?: string;
}; };
export const TextControl = < export const TextControl = <
@ -34,7 +35,7 @@ export const TextControl = <
>( >(
props: TextControlProps<T, P>, props: TextControlProps<T, P>,
) => { ) => {
const { labelIcon, ...rest } = props; const { labelIcon, helperText, ...rest } = props;
const required = !!props.rules?.required; const required = !!props.rules?.required;
const defaultValue = props.defaultValue ?? ("" as PathValue<T, P>); const defaultValue = props.defaultValue ?? ("" as PathValue<T, P>);
@ -54,7 +55,7 @@ export const TextControl = <
<TextInput <TextInput
isRequired={required} isRequired={required}
id={props.name} id={props.name}
data-testid={props.name} data-testid={props["data-testid"] || props.name}
validated={ validated={
fieldState.error ? ValidatedOptions.error : ValidatedOptions.default fieldState.error ? ValidatedOptions.error : ValidatedOptions.default
} }
@ -62,10 +63,10 @@ export const TextControl = <
{...rest} {...rest}
{...field} {...field}
/> />
{props.helperText && ( {helperText && (
<FormHelperText> <FormHelperText>
<HelperText> <HelperText>
<HelperTextItem>{props.helperText}</HelperTextItem> <HelperTextItem>{helperText}</HelperTextItem>
</HelperText> </HelperText>
</FormHelperText> </FormHelperText>
)} )}