Changed KeyckloakTextInput to TextControl in NewRealmForm (#27596)

* Replaced KeycloakTextInput with TextControl

Signed-off-by: Agnieszka Gancarczyk <agancarc@redhat.com>

* changed Switch to DefaultSwitchControl

Signed-off-by: Agnieszka Gancarczyk <agancarc@redhat.com>

* Fix `defaultValue` behaviour for `SwitchControl`

Signed-off-by: Jon Koops <jonkoops@gmail.com>

* fixing test

Signed-off-by: Agnieszka Gancarczyk <agancarc@redhat.com>

* fixing test

Signed-off-by: Agnieszka Gancarczyk <agancarc@redhat.com>

---------

Signed-off-by: Agnieszka Gancarczyk <agancarc@redhat.com>
Signed-off-by: Jon Koops <jonkoops@gmail.com>
Co-authored-by: Agnieszka Gancarczyk <agancarc@redhat.com>
Co-authored-by: Jon Koops <jonkoops@gmail.com>
This commit is contained in:
agagancarczyk 2024-03-06 14:07:49 +00:00 committed by GitHub
parent 2199d37879
commit 65448ff8c3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 48 additions and 80 deletions

View file

@ -1,14 +1,14 @@
export default class CreateRealmPage { export default class CreateRealmPage {
#clearBtn = ".pf-c-file-upload__file-select button:last-child"; #clearBtn = ".pf-c-file-upload__file-select button:last-child";
#modalClearBtn = "clear-button"; #modalClearBtn = "clear-button";
#realmNameInput = "#kc-realm-name"; #realmNameInput = "realm";
#enabledSwitch = ".pf-c-toolbar .pf-c-switch__toggle"; #enabledSwitch = ".pf-c-toolbar .pf-c-switch__toggle";
#createBtn = '.pf-c-form__group:last-child button[type="submit"]'; #createBtn = '.pf-c-form__group:last-child button[type="submit"]';
#cancelBtn = '.pf-c-form__group:last-child button[type="button"]'; #cancelBtn = '.pf-c-form__group:last-child button[type="button"]';
#codeEditor = ".pf-c-code-editor__code"; #codeEditor = ".pf-c-code-editor__code";
fillRealmName(realmName: string) { fillRealmName(realmName: string) {
cy.get(this.#realmNameInput).clear().type(realmName); cy.findByTestId(this.#realmNameInput).clear().type(realmName);
return this; return this;
} }
@ -45,7 +45,7 @@ export default class CreateRealmPage {
} }
verifyRealmNameFieldInvalid() { verifyRealmNameFieldInvalid() {
cy.get(this.#realmNameInput) cy.findByTestId(this.#realmNameInput)
.next("div") .next("div")
.contains("Required field") .contains("Required field")
.should("have.class", "pf-m-error"); .should("have.class", "pf-m-error");

View file

@ -1,18 +1,12 @@
import { SwitchProps } from "@patternfly/react-core"; import { FieldPath, FieldValues } from "react-hook-form";
import { FieldPath, FieldValues, UseControllerProps } from "react-hook-form";
import { useTranslation } from "react-i18next"; import { useTranslation } from "react-i18next";
import type { SwitchControlProps } from "ui-shared";
import { SwitchControl } from "ui-shared"; import { SwitchControl } from "ui-shared";
type DefaultSwitchControlProps< export type DefaultSwitchControlProps<
T extends FieldValues, T extends FieldValues,
P extends FieldPath<T> = FieldPath<T>, P extends FieldPath<T> = FieldPath<T>,
> = SwitchProps & > = Omit<SwitchControlProps<T, P>, "labelOn" | "labelOff">;
UseControllerProps<T, P> & {
name: string;
label?: string;
labelIcon?: string;
stringify?: boolean;
};
export const DefaultSwitchControl = < export const DefaultSwitchControl = <
T extends FieldValues, T extends FieldValues,

View file

@ -1,21 +1,16 @@
import type RealmRepresentation from "@keycloak/keycloak-admin-client/lib/defs/realmRepresentation"; import type RealmRepresentation from "@keycloak/keycloak-admin-client/lib/defs/realmRepresentation";
import { import { ActionGroup, Button, PageSection } from "@patternfly/react-core";
ActionGroup,
Button,
FormGroup,
PageSection,
Switch,
} from "@patternfly/react-core";
import { useState } from "react"; import { useState } from "react";
import { Controller, useForm } from "react-hook-form"; import { FormProvider, useForm } from "react-hook-form";
import { useTranslation } from "react-i18next"; import { useTranslation } from "react-i18next";
import { useNavigate } from "react-router-dom"; import { useNavigate } from "react-router-dom";
import { TextControl } from "ui-shared";
import { adminClient } from "../../admin-client"; import { adminClient } from "../../admin-client";
import { DefaultSwitchControl } from "../../components/SwitchControl";
import { useAlerts } from "../../components/alert/Alerts"; import { useAlerts } from "../../components/alert/Alerts";
import { FormAccess } from "../../components/form/FormAccess"; import { FormAccess } from "../../components/form/FormAccess";
import { JsonFileUpload } from "../../components/json-file-upload/JsonFileUpload"; import { JsonFileUpload } from "../../components/json-file-upload/JsonFileUpload";
import { KeycloakTextInput } from "../../components/keycloak-text-input/KeycloakTextInput";
import { ViewHeader } from "../../components/view-header/ViewHeader"; import { ViewHeader } from "../../components/view-header/ViewHeader";
import { useRealms } from "../../context/RealmsContext"; import { useRealms } from "../../context/RealmsContext";
import { useWhoAmI } from "../../context/whoami/WhoAmI"; import { useWhoAmI } from "../../context/whoami/WhoAmI";
@ -30,13 +25,11 @@ export default function NewRealmForm() {
const { addAlert, addError } = useAlerts(); const { addAlert, addError } = useAlerts();
const [realm, setRealm] = useState<RealmRepresentation>(); const [realm, setRealm] = useState<RealmRepresentation>();
const { const form = useForm<RealmRepresentation>({
register, mode: "onChange",
handleSubmit, });
control,
setValue, const { handleSubmit, setValue } = form;
formState: { errors },
} = useForm<RealmRepresentation>({ mode: "onChange" });
const handleFileChange = (obj?: object) => { const handleFileChange = (obj?: object) => {
const defaultRealm = { id: "", realm: "", enabled: true }; const defaultRealm = { id: "", realm: "", enabled: true };
@ -64,6 +57,7 @@ export default function NewRealmForm() {
<> <>
<ViewHeader titleKey="createRealm" subKey="realmExplain" /> <ViewHeader titleKey="createRealm" subKey="realmExplain" />
<PageSection variant="light"> <PageSection variant="light">
<FormProvider {...form}>
<FormAccess <FormAccess
isHorizontal isHorizontal
onSubmit={handleSubmit(save)} onSubmit={handleSubmit(save)}
@ -75,40 +69,16 @@ export default function NewRealmForm() {
allowEditingUploadedText allowEditingUploadedText
onChange={handleFileChange} onChange={handleFileChange}
/> />
<FormGroup <TextControl
name="realm"
label={t("realmNameField")} label={t("realmNameField")}
isRequired rules={{ required: t("required") }}
fieldId="kc-realm-name"
validated={errors.realm ? "error" : "default"}
helperTextInvalid={errors.realm?.message}
>
<KeycloakTextInput
isRequired
id="kc-realm-name"
validated={errors.realm ? "error" : "default"}
{...register("realm", {
required: { value: true, message: t("required") },
})}
/> />
</FormGroup> <DefaultSwitchControl
<FormGroup label={t("enabled")} fieldId="kc-realm-enabled-switch">
<Controller
name="enabled" name="enabled"
label={t("enabled")}
defaultValue={true} defaultValue={true}
control={control}
render={({ field }) => (
<Switch
id="kc-realm-enabled-switch"
name="enabled"
label={t("on")}
labelOff={t("off")}
isChecked={field.value}
onChange={field.onChange}
aria-label={t("enabled")}
/> />
)}
/>
</FormGroup>
<ActionGroup> <ActionGroup>
<Button variant="primary" type="submit"> <Button variant="primary" type="submit">
{t("create")} {t("create")}
@ -118,6 +88,7 @@ export default function NewRealmForm() {
</Button> </Button>
</ActionGroup> </ActionGroup>
</FormAccess> </FormAccess>
</FormProvider>
</PageSection> </PageSection>
</> </>
); );

View file

@ -12,7 +12,7 @@ import { FormLabel } from "./FormLabel";
export type SwitchControlProps< export type SwitchControlProps<
T extends FieldValues, T extends FieldValues,
P extends FieldPath<T> = FieldPath<T>, P extends FieldPath<T> = FieldPath<T>,
> = Omit<SwitchProps, "name"> & > = Omit<SwitchProps, "name" | "defaultValue"> &
UseControllerProps<T, P> & { UseControllerProps<T, P> & {
name: string; name: string;
label?: string; label?: string;

View file

@ -1,7 +1,10 @@
export { ContinueCancelModal } from "./continue-cancel/ContinueCancelModal"; export { ContinueCancelModal } from "./continue-cancel/ContinueCancelModal";
export { SelectControl } from "./controls/SelectControl"; export { SelectControl } from "./controls/SelectControl";
export type { SelectControlOption } from "./controls/SelectControl"; export type { SelectControlOption } from "./controls/SelectControl";
export { SwitchControl } from "./controls/SwitchControl"; export {
SwitchControl,
type SwitchControlProps,
} from "./controls/SwitchControl";
export { TextControl } from "./controls/TextControl"; export { TextControl } from "./controls/TextControl";
export { TextAreaControl } from "./controls/TextAreaControl"; export { TextAreaControl } from "./controls/TextAreaControl";
export { NumberControl } from "./controls/NumberControl"; export { NumberControl } from "./controls/NumberControl";