From 3e02fd1aa61c64d0a2191a3f1243416d21f30e4c Mon Sep 17 00:00:00 2001 From: Erik Jan de Wit Date: Tue, 30 Mar 2021 14:25:45 +0200 Subject: [PATCH] adds fake pagination to datatable (#472) even when the backend doesn't support pagination we still show paginated results --- .../table-toolbar/KeycloakDataTable.tsx | 84 +++++++------------ 1 file changed, 31 insertions(+), 53 deletions(-) diff --git a/src/components/table-toolbar/KeycloakDataTable.tsx b/src/components/table-toolbar/KeycloakDataTable.tsx index 22b6322ecd..7cb31d97b3 100644 --- a/src/components/table-toolbar/KeycloakDataTable.tsx +++ b/src/components/table-toolbar/KeycloakDataTable.tsx @@ -139,6 +139,7 @@ export function KeycloakDataTable({ const { t } = useTranslation(); const [selected, setSelected] = useState([]); const [rows, setRows] = useState[]>(); + const [unPaginatedData, setUnPaginatedData] = useState(); const [filteredData, setFilteredData] = useState[]>(); const [loading, setLoading] = useState(false); @@ -154,23 +155,15 @@ export function KeycloakDataTable({ return asyncStateFetch( async () => { setLoading(true); - const data = await loader(first, max, search); - const result = data!.map((value) => { - return { - data: value, - selected: !!selected.find( - (v) => (v as any).id === (value as any).id - ), - cells: columns.map((col) => { - if (col.cellRenderer) { - return col.cellRenderer(value); - } - return _.get(value, col.name); - }), - }; - }); - return result; + let data = unPaginatedData || (await loader(first, max, search)); + + if (!isPaginated) { + setUnPaginatedData(data); + data = data.slice(first, first + max); + } + + return convertToColumns(data); }, (result) => { setRows(result); @@ -194,9 +187,24 @@ export function KeycloakDataTable({ return ""; }; + const convertToColumns = (data: T[]) => { + return data!.map((value) => { + return { + data: value, + selected: !!selected.find((v) => (v as any).id === (value as any).id), + cells: columns.map((col) => { + if (col.cellRenderer) { + return col.cellRenderer(value); + } + return _.get(value, col.name); + }), + }; + }); + }; + const filter = (search: string) => { setFilteredData( - rows!.filter((row) => + convertToColumns(unPaginatedData!).filter((row) => row.cells.some( (cell) => cell && @@ -255,7 +263,7 @@ export function KeycloakDataTable({ return ( <> - {rows && isPaginated && ( + {rows && ( ({ inputGroupName={ searchPlaceholderKey ? `${ariaLabelKey}input` : undefined } - inputGroupOnEnter={setSearch} + inputGroupOnEnter={ + isPaginated ? setSearch : (search) => filter(search) + } inputGroupPlaceholder={t(searchPlaceholderKey || "")} searchTypeComponent={searchTypeComponent} toolbarItem={toolbarItem} > - {!loading && rows.length > 0 && ( + {!loading && (filteredData || rows).length > 0 && ( @@ -296,38 +306,6 @@ export function KeycloakDataTable({ {loading && } )} - {rows && (rows.length > 0 || !emptyState) && !isPaginated && ( - filter(search)} - inputGroupPlaceholder={t(searchPlaceholderKey || "")} - toolbarItem={toolbarItem} - searchTypeComponent={searchTypeComponent} - > - {!loading && (filteredData || rows).length > 0 && ( - - )} - {!loading && filteredData && filteredData.length === 0 && ( - - )} - {loading && } - - )} <>{!loading && rows?.length === 0 && search === "" && emptyState} );