✨(react) add empty placeholder customization props
We want to be able to customize the empty placeholder label, add a call to action and choose whether we want to display the image as well. Closes #27
This commit is contained in:
5
.changeset/stale-weeks-vanish.md
Normal file
5
.changeset/stale-weeks-vanish.md
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
---
|
||||||
|
"@openfun/cunningham-react": minor
|
||||||
|
---
|
||||||
|
|
||||||
|
add empty placeholder customization props
|
||||||
@@ -512,6 +512,51 @@ describe("<SimpleDataGrid/>", () => {
|
|||||||
screen.getByRole("img", { name: /illustration of an empty table/i });
|
screen.getByRole("img", { name: /illustration of an empty table/i });
|
||||||
screen.getByText(/this table is empty/i);
|
screen.getByText(/this table is empty/i);
|
||||||
});
|
});
|
||||||
|
it("should render an empty grid with custom label and cta", async () => {
|
||||||
|
const rows: Row[] = [];
|
||||||
|
render(
|
||||||
|
<CunninghamProvider>
|
||||||
|
<SimpleDataGrid
|
||||||
|
columns={[
|
||||||
|
{
|
||||||
|
field: "firstName",
|
||||||
|
headerName: "First name",
|
||||||
|
},
|
||||||
|
]}
|
||||||
|
rows={rows}
|
||||||
|
emptyPlaceholderLabel="Custom empty label"
|
||||||
|
emptyCta={<button>Custom empty CTA</button>}
|
||||||
|
/>
|
||||||
|
</CunninghamProvider>,
|
||||||
|
);
|
||||||
|
|
||||||
|
screen.getByRole("img", { name: /illustration of an empty table/i });
|
||||||
|
expect(screen.queryByText(/this table is empty/i)).not.toBeInTheDocument();
|
||||||
|
screen.getByText(/Custom empty label/i);
|
||||||
|
screen.getByRole("button", { name: /Custom empty CTA/i });
|
||||||
|
});
|
||||||
|
it("should render an empty grid without image", async () => {
|
||||||
|
const rows: Row[] = [];
|
||||||
|
render(
|
||||||
|
<CunninghamProvider>
|
||||||
|
<SimpleDataGrid
|
||||||
|
columns={[
|
||||||
|
{
|
||||||
|
field: "firstName",
|
||||||
|
headerName: "First name",
|
||||||
|
},
|
||||||
|
]}
|
||||||
|
rows={rows}
|
||||||
|
hideEmptyPlaceholderImage={true}
|
||||||
|
/>
|
||||||
|
</CunninghamProvider>,
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(
|
||||||
|
screen.queryByRole("img", { name: /illustration of an empty table/i }),
|
||||||
|
).not.toBeInTheDocument();
|
||||||
|
screen.getByText(/this table is empty/i);
|
||||||
|
});
|
||||||
it("should render a loading grid even if rows are empty", async () => {
|
it("should render a loading grid even if rows are empty", async () => {
|
||||||
const rows: Row[] = [];
|
const rows: Row[] = [];
|
||||||
render(
|
render(
|
||||||
|
|||||||
@@ -26,6 +26,46 @@ export const Empty = () => {
|
|||||||
/>
|
/>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
export const EmptyCustomWithButton = () => {
|
||||||
|
return (
|
||||||
|
<DataGrid
|
||||||
|
columns={[
|
||||||
|
{
|
||||||
|
field: "firstName",
|
||||||
|
headerName: "First name",
|
||||||
|
highlight: true,
|
||||||
|
},
|
||||||
|
]}
|
||||||
|
rows={[]}
|
||||||
|
emptyPlaceholderLabel="This table is empty, create the first object"
|
||||||
|
emptyCta={
|
||||||
|
<Button
|
||||||
|
color="secondary"
|
||||||
|
icon={<span className="material-icons">add</span>}
|
||||||
|
>
|
||||||
|
Create object
|
||||||
|
</Button>
|
||||||
|
}
|
||||||
|
/>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export const EmptyCustomNoImage = () => {
|
||||||
|
return (
|
||||||
|
<DataGrid
|
||||||
|
columns={[
|
||||||
|
{
|
||||||
|
field: "firstName",
|
||||||
|
headerName: "First name",
|
||||||
|
highlight: true,
|
||||||
|
},
|
||||||
|
]}
|
||||||
|
rows={[]}
|
||||||
|
hideEmptyPlaceholderImage={true}
|
||||||
|
emptyPlaceholderLabel="This table is empty :("
|
||||||
|
/>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
export const Loading = () => {
|
export const Loading = () => {
|
||||||
return (
|
return (
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import React, { useMemo } from "react";
|
import React, { ReactNode, useMemo } from "react";
|
||||||
import classNames from "classnames";
|
import classNames from "classnames";
|
||||||
import {
|
import {
|
||||||
flexRender,
|
flexRender,
|
||||||
@@ -52,6 +52,9 @@ export interface BaseProps<T extends Row = Row> {
|
|||||||
enableRowSelection?: TableOptions<T>["enableRowSelection"];
|
enableRowSelection?: TableOptions<T>["enableRowSelection"];
|
||||||
onRowSelectionChange?: (newSelection: RowSelectionState) => void;
|
onRowSelectionChange?: (newSelection: RowSelectionState) => void;
|
||||||
rowSelection?: RowSelectionState;
|
rowSelection?: RowSelectionState;
|
||||||
|
emptyPlaceholderLabel?: string;
|
||||||
|
emptyCta?: ReactNode;
|
||||||
|
hideEmptyPlaceholderImage?: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface DataGridProps<T extends Row = Row> extends BaseProps<T> {
|
export interface DataGridProps<T extends Row = Row> extends BaseProps<T> {
|
||||||
@@ -74,6 +77,9 @@ export const DataGrid = <T extends Row>({
|
|||||||
onRowSelectionChange,
|
onRowSelectionChange,
|
||||||
rowSelection,
|
rowSelection,
|
||||||
tableOptions,
|
tableOptions,
|
||||||
|
emptyPlaceholderLabel,
|
||||||
|
emptyCta,
|
||||||
|
hideEmptyPlaceholderImage,
|
||||||
displayHeader = true,
|
displayHeader = true,
|
||||||
}: DataGridProps<T>) => {
|
}: DataGridProps<T>) => {
|
||||||
const { t } = useCunningham();
|
const { t } = useCunningham();
|
||||||
@@ -143,8 +149,12 @@ export const DataGrid = <T extends Row>({
|
|||||||
if (showEmptyPlaceholder) {
|
if (showEmptyPlaceholder) {
|
||||||
return (
|
return (
|
||||||
<div className="c__datagrid__empty-placeholder fs-h3 clr-greyscale-900 fw-bold">
|
<div className="c__datagrid__empty-placeholder fs-h3 clr-greyscale-900 fw-bold">
|
||||||
<img src={emptyImageUrl} alt={t("components.datagrid.empty_alt")} />
|
{!hideEmptyPlaceholderImage && (
|
||||||
{t("components.datagrid.empty")}
|
<img src={emptyImageUrl} alt={t("components.datagrid.empty_alt")} />
|
||||||
|
)}
|
||||||
|
|
||||||
|
{emptyPlaceholderLabel ?? t("components.datagrid.empty")}
|
||||||
|
{emptyCta}
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user