8000 BACKPORT: fixed when client select is a id instead of an clientId by edewit · Pull Request #39117 · keycloak/keycloak · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

BACKPORT: fixed when client select is a id instead of an clientId #39117

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions js/apps/admin-ui/src/components/client/ClientSelect.tsx
< 8000 td class="blob-num blob-num-addition empty-cell">
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type ClientRepresentation from "@keycloak/keycloak-admin-client/lib/defs/
import type { ClientQuery } from "@keycloak/keycloak-admin-client/lib/resources/clients";
import {
SelectControl,
SelectControlOption,
SelectVariant,
useFetch,
} from "@keycloak/keycloak-ui-shared";
Expand All @@ -11,6 +12,7 @@ import { useAdminClient } from "../../admin-client";
import type { ComponentProps } from "../dynamic/components";
import { PermissionsConfigurationTabsParams } from "../../permissions-configuration/routes/PermissionsConfigurationTabs";
import { useParams } from "react-router-dom";
import { useFormContext, useWatch } from "react-hook-form";

type ClientSelectProps = Omit<ComponentProps, "convertToName"> & {
variant?: `${SelectVariant}`;
Expand All @@ -35,9 +37,18 @@ export const ClientSelect = ({
const { t } = useTranslation();

const [clients, setClients] = useState<ClientRepresentation[]>([]);
const [selectedClients, setSelectedClients] =
useState<SelectControlOption[]>();
const [search, setSearch] = useState("");
const { tab } = useParams<PermissionsConfigurationTabsParams>();

const { control } = useFormContext();
const value = useWatch({
control,
name: name!,
defaultValue: defaultValue || "",
});

useFetch(
() => {
const params: ClientQuery = {
Expand All @@ -53,6 +64,30 @@ export const ClientSelect = ({
[search],
);

useFetch(
() => {
const values = ((value as string[]) || []).map(async (clientId) => {
if (clientKey === "clientId") {
return (await adminClient.clients.find({ clientId }))[0];
} else {
return adminClient.clients.findOne({ id: clientId });
}
});
return Promise.all(values);
},
(clients) => {
setSelectedClients(
clients
.filter((client) => !!client)
.map((client) => ({
key: client[clientKey] as string,
value: client.clientId!,
})),
);
},
[],
);

return (
<SelectControl
name={name!}
Expand All @@ -70,6 +105,7 @@ export const ClientSelect = ({
=> setSearch(value)}
variant={variant}
isDisabled={isDisabled}
selectedOptions={selectedClients}
options={clients.map((client) => ({
key: client[clientKey] as string,
value: client.clientId!,
Expand Down
1 change: 1 addition & 0 deletions js/apps/admin-ui/test/realm-settings/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export async function fillEventListener(page: Page, listener: string) {
await eventListener.click();
await eventListener.fill(listener);
await page.getByRole("option", { name: listener }).click();
await page.keyboard.press("Escape");
}

export 10000 async function clickRevertButton(page: Page) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export type SelectControlProps<
name: string;
label?: string;
options: OptionType;
selectedOptions?: SelectControlOption[];
labelIcon?: string;
controller: Omit<ControllerProps, "name" | "render">;
onFilter?: (value: string) => void;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export const TypeaheadSelectControl = <
name,
label,
options,
selectedOptions = [],
controller,
labelIcon,
placeholderText,
Expand All @@ -57,6 +58,9 @@ export const TypeaheadSelectControl = <
const [open, setOpen] = useState(false);
const [filterValue, setFilterValue] = useState("");
const [focusedItemIndex, setFocusedItemIndex] = useState<number>(0);
const [selectedOptionsState, setSelectedOptions] = useState<
SelectControlOption[]
>([]);
const textInputRef = useRef<HTMLInputElement>();
const required = getRuleValue(controller.rules?.required) === true;
const isTypeaheadMulti = variant === SelectVariant.typeaheadMulti;
Expand All @@ -79,6 +83,28 @@ export const TypeaheadSelectControl = <
[focusedItemIndex, filteredOptions],
);

const updateValue = (
option: string | string[],
field: ControllerRenderProps<FieldValues, string>,
) => {
if (field.value.includes(option)) {
field.onChange(field.value.filter((item: string) => item !== option));
if (isSelectBasedOptions(options)) {
setSelectedOptions(
selectedOptionsState.filter((item) => item.key !== option),
);
}
} else {
field.onChange([...field.value, option]);
if (isSelectBasedOptions(options)) {
setSelectedOptions([
...selectedOptionsState,
options.find((o) => o.key === option)!,
]);
}
}
};

const >
event: React.KeyboardEvent<HTMLDivElement>,
field: ControllerRenderProps<FieldValues, string>,
Expand All @@ -96,11 +122,8 @@ export const TypeaheadSelectControl = <
setFilterValue("");
}

field.onChange(
Array.isArray(field.value)
? [...field.value, key(focusedItem)]
: key(focusedItem),
);
updateValue(key(focusedItem), field);

setOpen(false);
setFocusedItemIndex(0);

Expand Down Expand Up @@ -232,8 +255,11 @@ export const TypeaheadSelectControl = <
}}
>
{isSelectBasedOptions(options)
? options.find((o) => selection === o.key)
?.value
? [
...options,
...selectedOptionsState,
...selectedOptions,
].find((o) => selection === o.key)?.value
: getValue(selection)}
</Chip>
),
Expand Down Expand Up @@ -263,13 +289,8 @@ export co 5D39 nst TypeaheadSelectControl = <
event?.stopPropagation();
const option = v?.toString();
if (isTypeaheadMulti && Array.isArray(field.value)) {
if (field.value.includes(option)) {
field.onChange(
field.value.filter((item: string) => item !== option),
);
} else {
field.onChange([...field.value, option]);
}
setFilterValue("");
updateValue(option || "", field);
} else {
field.onChange(Array.isArray(field.value) ? [option] : option);
setOpen(false);
Expand Down
Loading
0