added search empty state (#435)

This commit is contained in:
Erik Jan de Wit 2021-03-16 13:42:05 +01:00 committed by GitHub
parent e4d83d0fe3
commit 7d4adc683f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 33 additions and 11 deletions

View file

@ -14,6 +14,8 @@
"delete": "Delete", "delete": "Delete",
"remove": "Remove", "remove": "Remove",
"search": "Search", "search": "Search",
"noSearchResults": "No search results",
"noSearchResultsInstructions": "Click on the search bar above to search",
"next": "Next", "next": "Next",
"back": "Back", "back": "Back",
"finish": "Finish", "finish": "Finish",

View file

@ -17,6 +17,7 @@ import _ from "lodash";
import { PaginatingTableToolbar } from "./PaginatingTableToolbar"; import { PaginatingTableToolbar } from "./PaginatingTableToolbar";
import { TableToolbar } from "./TableToolbar"; import { TableToolbar } from "./TableToolbar";
import { asyncStateFetch } from "../../context/auth/AdminClient"; import { asyncStateFetch } from "../../context/auth/AdminClient";
import { ListEmptyState } from "../list-empty-state/ListEmptyState";
type Row<T> = { type Row<T> = {
data: T; data: T;
@ -212,11 +213,10 @@ export function KeycloakDataTable<T>({
}); });
const searchOnChange = (value: string) => { const searchOnChange = (value: string) => {
if (isPaginated) { if (value === "") {
setSearch(value); refresh();
} else {
filter(value);
} }
setSearch(value);
}; };
const Loading = () => ( const Loading = () => (
@ -242,7 +242,7 @@ export function KeycloakDataTable<T>({
return ( return (
<> <>
{rows && (rows.length > 0 || !emptyState) && isPaginated && ( {rows && isPaginated && (
<PaginatingTableToolbar <PaginatingTableToolbar
count={rows.length} count={rows.length}
first={first} first={first}
@ -262,7 +262,7 @@ export function KeycloakDataTable<T>({
searchTypeComponent={searchTypeComponent} searchTypeComponent={searchTypeComponent}
toolbarItem={toolbarItem} toolbarItem={toolbarItem}
> >
{!loading && ( {!loading && rows.length > 0 && (
<DataTable <DataTable
canSelectAll={canSelectAll} canSelectAll={canSelectAll}
onSelect={onSelect ? _onSelect : undefined} onSelect={onSelect ? _onSelect : undefined}
@ -273,6 +273,14 @@ export function KeycloakDataTable<T>({
ariaLabelKey={ariaLabelKey} ariaLabelKey={ariaLabelKey}
/> />
)} )}
{!loading && rows.length === 0 && search !== "" && (
<ListEmptyState
hasIcon={true}
isSearchVariant={true}
message={t("noSearchResults")}
instructions={t("noSearchResultsInstructions")}
/>
)}
{loading && <Loading />} {loading && <Loading />}
</PaginatingTableToolbar> </PaginatingTableToolbar>
)} )}
@ -282,12 +290,12 @@ export function KeycloakDataTable<T>({
searchPlaceholderKey ? `${ariaLabelKey}input` : undefined searchPlaceholderKey ? `${ariaLabelKey}input` : undefined
} }
inputGroupOnChange={searchOnChange} inputGroupOnChange={searchOnChange}
inputGroupOnClick={() => {}} inputGroupOnClick={() => filter(search)}
inputGroupPlaceholder={t(searchPlaceholderKey || "")} inputGroupPlaceholder={t(searchPlaceholderKey || "")}
toolbarItem={toolbarItem} toolbarItem={toolbarItem}
searchTypeComponent={searchTypeComponent} searchTypeComponent={searchTypeComponent}
> >
{!loading && ( {!loading && (filteredData || rows).length > 0 && (
<DataTable <DataTable
canSelectAll={canSelectAll} canSelectAll={canSelectAll}
onSelect={onSelect ? _onSelect : undefined} onSelect={onSelect ? _onSelect : undefined}
@ -298,10 +306,18 @@ export function KeycloakDataTable<T>({
ariaLabelKey={ariaLabelKey} ariaLabelKey={ariaLabelKey}
/> />
)} )}
{!loading && filteredData && filteredData.length === 0 && (
<ListEmptyState
hasIcon={true}
isSearchVariant={true}
message={t("noSearchResults")}
instructions={t("noSearchResultsInstructions")}
/>
)}
{loading && <Loading />} {loading && <Loading />}
</TableToolbar> </TableToolbar>
)} )}
<>{!loading && rows?.length === 0 && emptyState}</> <>{!loading && rows?.length === 0 && search === "" && emptyState}</>
</> </>
); );
} }

View file

@ -65,10 +65,14 @@ export const PaginatingTableToolbar = ({
toolbarItem={ toolbarItem={
<> <>
{toolbarItem} {toolbarItem}
<ToolbarItem variant="pagination">{pagination()}</ToolbarItem> {count !== 0 && (
<ToolbarItem variant="pagination">{pagination()}</ToolbarItem>
)}
</> </>
} }
toolbarItemFooter={<ToolbarItem>{pagination("bottom")}</ToolbarItem>} toolbarItemFooter={
count !== 0 && <ToolbarItem>{pagination("bottom")}</ToolbarItem>
}
inputGroupName={inputGroupName} inputGroupName={inputGroupName}
inputGroupPlaceholder={inputGroupPlaceholder} inputGroupPlaceholder={inputGroupPlaceholder}
inputGroupOnChange={inputGroupOnChange} inputGroupOnChange={inputGroupOnChange}