adds fake pagination to datatable (#472)

even when the backend doesn't support pagination we still show paginated results
This commit is contained in:
Erik Jan de Wit 2021-03-30 14:25:45 +02:00 committed by GitHub
parent 17a3fe51bb
commit 3e02fd1aa6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -139,6 +139,7 @@ export function KeycloakDataTable<T>({
const { t } = useTranslation(); const { t } = useTranslation();
const [selected, setSelected] = useState<T[]>([]); const [selected, setSelected] = useState<T[]>([]);
const [rows, setRows] = useState<Row<T>[]>(); const [rows, setRows] = useState<Row<T>[]>();
const [unPaginatedData, setUnPaginatedData] = useState<T[]>();
const [filteredData, setFilteredData] = useState<Row<T>[]>(); const [filteredData, setFilteredData] = useState<Row<T>[]>();
const [loading, setLoading] = useState(false); const [loading, setLoading] = useState(false);
@ -154,23 +155,15 @@ export function KeycloakDataTable<T>({
return asyncStateFetch( return asyncStateFetch(
async () => { async () => {
setLoading(true); setLoading(true);
const data = await loader(first, max, search);
const result = data!.map((value) => { let data = unPaginatedData || (await loader(first, max, search));
return {
data: value, if (!isPaginated) {
selected: !!selected.find( setUnPaginatedData(data);
(v) => (v as any).id === (value as any).id data = data.slice(first, first + max);
),
cells: columns.map((col) => {
if (col.cellRenderer) {
return col.cellRenderer(value);
} }
return _.get(value, col.name);
}), return convertToColumns(data);
};
});
return result;
}, },
(result) => { (result) => {
setRows(result); setRows(result);
@ -194,9 +187,24 @@ export function KeycloakDataTable<T>({
return ""; 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) => { const filter = (search: string) => {
setFilteredData( setFilteredData(
rows!.filter((row) => convertToColumns(unPaginatedData!).filter((row) =>
row.cells.some( row.cells.some(
(cell) => (cell) =>
cell && cell &&
@ -255,7 +263,7 @@ export function KeycloakDataTable<T>({
return ( return (
<> <>
{rows && isPaginated && ( {rows && (
<PaginatingTableToolbar <PaginatingTableToolbar
count={rows.length} count={rows.length}
first={first} first={first}
@ -269,18 +277,20 @@ export function KeycloakDataTable<T>({
inputGroupName={ inputGroupName={
searchPlaceholderKey ? `${ariaLabelKey}input` : undefined searchPlaceholderKey ? `${ariaLabelKey}input` : undefined
} }
inputGroupOnEnter={setSearch} inputGroupOnEnter={
isPaginated ? setSearch : (search) => filter(search)
}
inputGroupPlaceholder={t(searchPlaceholderKey || "")} inputGroupPlaceholder={t(searchPlaceholderKey || "")}
searchTypeComponent={searchTypeComponent} searchTypeComponent={searchTypeComponent}
toolbarItem={toolbarItem} toolbarItem={toolbarItem}
> >
{!loading && rows.length > 0 && ( {!loading && (filteredData || rows).length > 0 && (
<DataTable <DataTable
canSelectAll={canSelectAll} canSelectAll={canSelectAll}
onSelect={onSelect ? _onSelect : undefined} onSelect={onSelect ? _onSelect : undefined}
actions={convertAction()} actions={convertAction()}
actionResolver={actionResolver} actionResolver={actionResolver}
rows={rows} rows={filteredData || rows}
columns={columns} columns={columns}
ariaLabelKey={ariaLabelKey} ariaLabelKey={ariaLabelKey}
/> />
@ -296,38 +306,6 @@ export function KeycloakDataTable<T>({
{loading && <Loading />} {loading && <Loading />}
</PaginatingTableToolbar> </PaginatingTableToolbar>
)} )}
{rows && (rows.length > 0 || !emptyState) && !isPaginated && (
<TableToolbar
inputGroupName={
searchPlaceholderKey ? `${ariaLabelKey}input` : undefined
}
inputGroupOnEnter={(search) => filter(search)}
inputGroupPlaceholder={t(searchPlaceholderKey || "")}
toolbarItem={toolbarItem}
searchTypeComponent={searchTypeComponent}
>
{!loading && (filteredData || rows).length > 0 && (
<DataTable
canSelectAll={canSelectAll}
onSelect={onSelect ? _onSelect : undefined}
actions={convertAction()}
actionResolver={actionResolver}
rows={filteredData || rows}
columns={columns}
ariaLabelKey={ariaLabelKey}
/>
)}
{!loading && filteredData && filteredData.length === 0 && (
<ListEmptyState
hasIcon={true}
isSearchVariant={true}
message={t("noSearchResults")}
instructions={t("noSearchResultsInstructions")}
/>
)}
{loading && <Loading />}
</TableToolbar>
)}
<>{!loading && rows?.length === 0 && search === "" && emptyState}</> <>{!loading && rows?.length === 0 && search === "" && emptyState}</>
</> </>
); );