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

fixed when client select is a id instead of an clientId #38974

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 1 commit into from
Apr 16, 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
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
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 Down Expand Up @@ -85,8 +89,19 @@ export const TypeaheadSelectControl = <
) => {
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)!,
]);
}
}
};

Expand Down Expand Up @@ -240,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
Loading
0