diff --git a/js/apps/admin-ui/src/components/group/GroupPickerDialog.tsx b/js/apps/admin-ui/src/components/group/GroupPickerDialog.tsx index 6394ea5c52..0a8c480c0c 100644 --- a/js/apps/admin-ui/src/components/group/GroupPickerDialog.tsx +++ b/js/apps/admin-ui/src/components/group/GroupPickerDialog.tsx @@ -3,7 +3,11 @@ import { GroupQuery, SubGroupQuery, } from "@keycloak/keycloak-admin-client/lib/resources/groups"; -import { PaginatingTableToolbar, useFetch } from "@keycloak/keycloak-ui-shared"; +import { + ListEmptyState, + PaginatingTableToolbar, + useFetch, +} from "@keycloak/keycloak-ui-shared"; import { Breadcrumb, BreadcrumbItem, @@ -19,10 +23,9 @@ import { ModalVariant, } from "@patternfly/react-core"; import { AngleRightIcon } from "@patternfly/react-icons"; -import { Fragment, useState } from "react"; +import { useState } from "react"; import { useTranslation } from "react-i18next"; import { useAdminClient } from "../../admin-client"; -import { ListEmptyState } from "@keycloak/keycloak-ui-shared"; import { GroupPath } from "./GroupPath"; import "./group-picker-dialog.css"; @@ -62,7 +65,6 @@ export const GroupPickerDialog = ({ const [filter, setFilter] = useState(""); const [joinedGroups, setJoinedGroups] = useState([]); const [groupId, setGroupId] = useState(); - const [isSearching, setIsSearching] = useState(false); const [max, setMax] = useState(10); const [first, setFirst] = useState(0); @@ -82,7 +84,7 @@ export const GroupPickerDialog = ({ first, max: max + 1, }; - if (isSearching) { + if (filter !== "") { args.search = filter; } groups = await adminClient.groups.find(args); @@ -121,7 +123,7 @@ export const GroupPickerDialog = ({ group.checked = !!selectedRows.find((r) => r.id === group.id); }); setGroups(groups); - if (isSearching || !groupId) { + if (filter !== "" || !groupId) { setCount(groups.length); } }, @@ -137,7 +139,7 @@ export const GroupPickerDialog = ({ return ( { setFilter(search); - setIsSearching(search !== ""); setFirst(0); setMax(10); setNavigation([]); @@ -222,57 +223,47 @@ export const GroupPickerDialog = ({ ))} - {groups.slice(0, max).map((group: SelectableGroup) => ( - - {(!isSearching || - group.name - ?.toLocaleUpperCase() - .includes(filter.toLocaleUpperCase())) && ( + {filter == "" + ? groups.slice(0, max).map((group: SelectableGroup) => ( { - setGroupId(groupId); + onSelect={(group) => { + setGroupId(group.id); setFirst(0); }} type={type} - isSearching={isSearching} - setIsSearching={setIsSearching} + isSearching={filter !== ""} + setIsSearching={(boolean) => setFilter(boolean ? "" : filter)} selectedRows={selectedRows} setSelectedRows={setSelectedRows} canBrowse={canBrowse} /> - )} - {isSearching && - group.subGroups?.map((g) => ( + )) + : groups + ?.map((g) => deepGroup(g)) + .map((g) => ( { - setGroupId(groupId); - setFirst(0); - }} type={type} - isSearching={isSearching} - setIsSearching={setIsSearching} + isSearching selectedRows={selectedRows} setSelectedRows={setSelectedRows} - canBrowse={canBrowse} + canBrowse={false} /> ))} - - ))} - {groups.length === 0 && !isSearching && ( + {groups.length === 0 && filter === "" && ( )} - {groups.length === 0 && isSearching && ( + {groups.length === 0 && filter !== "" && ( { + let result = group; + if (group.subGroups?.length === 1) { + result = deepGroup(group.subGroups[0]); + } + + return result; +}; + type GroupRowProps = { group: SelectableGroup; type: "selectOne" | "selectMany"; isRowDisabled: (row?: GroupRepresentation) => boolean; isSearching: boolean; - setIsSearching: (value: boolean) => void; - onSelect: (groupId: string) => void; + setIsSearching?: (value: boolean) => void; + onSelect?: (group: GroupRepresentation) => void; selectedRows: SelectableGroup[]; setSelectedRows: (groups: SelectableGroup[]) => void; canBrowse: boolean; @@ -315,13 +315,13 @@ const GroupRow = ({ id={group.id} onClick={(e) => { if (type === "selectOne") { - onSelect(group.id!); + onSelect?.(group); } else if ( (e.target as HTMLInputElement).type !== "checkbox" && group.subGroupCount !== 0 ) { - onSelect(group.id!); - setIsSearching(false); + onSelect?.(group); + setIsSearching?.(false); } }} >