import { Canvas, Meta, Controls } from "@storybook/addon-docs/blocks"; import * as DataTableStories from "./DataTable.stories"; # DataTable The DataTable component provides a powerful and flexible way to display tabular data with features like sorting, column resizing, row selection, and custom cell rendering. It's built on top of @tanstack/react-table and offers a comprehensive solution for displaying and interacting with structured data. ## Usage = { name: { header: "Title", enableSorting: true }, createdBy: { header: "Author" }, lastModified: { header: "Last Modified", enableSorting: true }, status: { header: "Status" } }; // Sample data const data = [ { id: "entry-1", name: "Getting Started Guide", createdBy: "John Doe", lastModified: "1 hour ago", status: "Published" }, { id: "entry-2", name: "User Documentation", createdBy: "Jane Smith", lastModified: "3 days ago", status: "Draft" }, { id: "entry-3", name: "API Reference", createdBy: "John Doe", lastModified: "1 week ago", status: "Published" } ]; const DataTableExample = () => { const [selectedRows, setSelectedRows] = useState\([]); const [sorting, setSorting] = useState\([]); return ( setSelectedRows(rows)} sorting={sorting} onSortingChange={setSorting} /> ); }; export default DataTableExample; ` } } additionalActions={[ { title: 'Open in GitHub', onClick: () => { window.open( 'https://github.com/webiny/webiny-js/blob/feat/new-admin-ui/packages/admin-ui/src/DataTable/DataTable.tsx', '_blank' ); }, } ]} /> ```tsx import React, { useState } from "react"; import { DataTable, DataTableColumns, DataTableSorting } from "@webiny/admin-ui"; // Define the data structure interface Entry { id: string; name: string; createdBy: string; lastModified: string; status: string; $selectable?: boolean; } // Define the columns structure const columns: DataTableColumns = { name: { header: "Title", enableSorting: true }, createdBy: { header: "Author" }, lastModified: { header: "Last Modified", enableSorting: true }, status: { header: "Status" } }; // Sample data const data = [ { id: "entry-1", name: "Getting Started Guide", createdBy: "John Doe", lastModified: "1 hour ago", status: "Published" }, { id: "entry-2", name: "User Documentation", createdBy: "Jane Smith", lastModified: "3 days ago", status: "Draft" }, { id: "entry-3", name: "API Reference", createdBy: "John Doe", lastModified: "1 week ago", status: "Published" } ]; const DataTableExample = () => { const [selectedRows, setSelectedRows] = useState([]); const [sorting, setSorting] = useState([]); return ( setSelectedRows(rows)} sorting={sorting} onSortingChange={setSorting} /> ); }; export default DataTableExample; ``` ## Examples ### Default = { name: { header: "Title", enableSorting: true }, createdBy: { header: "Author" }, lastModified: { header: "Last Modified", enableSorting: true }, status: { header: "Status" } }; // Sample data const data = [ { id: "entry-1", name: "Getting Started Guide", createdBy: "John Doe", lastModified: "1 hour ago", status: "Published" }, { id: "entry-2", name: "User Documentation", createdBy: "Jane Smith", lastModified: "3 days ago", status: "Draft" }, { id: "entry-3", name: "API Reference", createdBy: "John Doe", lastModified: "1 week ago", status: "Published" } ]; const DataTableExample = () => { return ( ); }; export default DataTableExample; ` } } /> ### Bordered = { name: { header: "Title", enableSorting: true }, createdBy: { header: "Author" }, lastModified: { header: "Last Modified", enableSorting: true }, status: { header: "Status" } }; // Sample data const data = [ { id: "entry-1", name: "Getting Started Guide", createdBy: "John Doe", lastModified: "1 hour ago", status: "Published" }, { id: "entry-2", name: "User Documentation", createdBy: "Jane Smith", lastModified: "3 days ago", status: "Draft" }, { id: "entry-3", name: "API Reference", createdBy: "John Doe", lastModified: "1 week ago", status: "Published" } ]; const BorderedDataTableExample = () => { return ( ); }; export default BorderedDataTableExample; ` } } /> ### With Sticky Header = { name: { header: "Title", enableSorting: true }, createdBy: { header: "Author" }, lastModified: { header: "Last Modified", enableSorting: true }, status: { header: "Status" } }; // Sample data const data = [ { id: "entry-1", name: "Getting Started Guide", createdBy: "John Doe", lastModified: "1 hour ago", status: "Published" }, { id: "entry-2", name: "User Documentation", createdBy: "Jane Smith", lastModified: "3 days ago", status: "Draft" }, { id: "entry-3", name: "API Reference", createdBy: "John Doe", lastModified: "1 week ago", status: "Published" } ]; const WithStickyHeaderDataTableExample = () => { return ( ); }; export default WithStickyHeaderDataTableExample; ` } } /> ### With Selectable Rows = { name: { header: "Title", enableSorting: true }, createdBy: { header: "Author" }, lastModified: { header: "Last Modified", enableSorting: true }, status: { header: "Status" } }; // Sample data const data = [ { id: "entry-1", name: "Getting Started Guide", createdBy: "John Doe", lastModified: "1 hour ago", status: "Published" }, { id: "entry-2", name: "User Documentation", createdBy: "Jane Smith", lastModified: "3 days ago", status: "Draft" }, { id: "entry-3", name: "API Reference", createdBy: "John Doe", lastModified: "1 week ago", status: "Published" } ]; const WithSelectableRowsDataTableExample = () => { const [selectedRows, setSelectedRows] = useState\([]); const [sorting, setSorting] = useState\([]); return ( setSelectedRows(rows)} /> ); }; export default WithSelectableRowsDataTableExample; ` } } /> ### With Loading State To demonstrate the loading state, we will simulate a 5 seconds delay before the data is loaded. = { name: { header: "Title", enableSorting: true }, createdBy: { header: "Author" }, lastModified: { header: "Last Modified", enableSorting: true }, status: { header: "Status" } }; // Sample data const data: Entry[] = [ { id: "entry-1", name: "Getting Started Guide", createdBy: "John Doe", lastModified: "1 hour ago", status: "Published" }, { id: "entry-2", name: "User Documentation", createdBy: "Jane Smith", lastModified: "3 days ago", status: "Draft" }, { id: "entry-3", name: "API Reference", createdBy: "John Doe", lastModified: "1 week ago", status: "Published" } ]; const WithLoadingDataTableExample = () => { const [data, setData] = useState\([]); const [loading, setLoading] = useState(true); useEffect(() => { const timer = setTimeout(() => { setData(data); setLoading(false); }, 5000); // simulate 5 seconds delay return () => clearTimeout(timer); }, []); return ( ); }; export default WithLoadingDataTableExample; ` } } /> ### With Custom Cell Renderer = { name: { header: "Title", enableSorting: true }, createdBy: { header: "Author" }, lastModified: { header: "Last Modified", enableSorting: true }, status: { header: "Status" } }; // Sample data const data: Entry[] = [ { id: "entry-1", name: "Getting Started Guide", createdBy: "John Doe", lastModified: "1 hour ago", status: "Published" }, { id: "entry-2", name: "User Documentation", createdBy: "Jane Smith", lastModified: "3 days ago", status: "Draft" }, { id: "entry-3", name: "API Reference", createdBy: "John Doe", lastModified: "1 week ago", status: "Published" } ]; const WithCustomCellRendererDataTableExample = () => { return ( { return (
} fallback={{entry.name.charAt(0)}} size={"xl"} />
{entry.name} Last updated: {entry.lastModified}
); } } }} data={data} /> ); }; export default WithCustomCellRendererDataTableExample; ` } } /> ### With Custom Column Size = { name: { header: "Title", enableSorting: true }, createdBy: { header: "Author" }, lastModified: { header: "Last Modified", enableSorting: true }, status: { header: "Status" } }; // Sample data const data = [ { id: "entry-1", name: "Getting Started Guide", createdBy: "John Doe", lastModified: "1 hour ago", status: "Published" }, { id: "entry-2", name: "User Documentation", createdBy: "Jane Smith", lastModified: "3 days ago", status: "Draft" }, { id: "entry-3", name: "API Reference", createdBy: "John Doe", lastModified: "1 week ago", status: "Published" } ]; const WithCustomColumnSizeDataTableExample = () => { return ( ); }; export default WithCustomColumnSize; ` } } /> ### With Custom Column Class Name = { name: { header: "Title", enableSorting: true }, createdBy: { header: "Author" }, lastModified: { header: "Last Modified", enableSorting: true }, status: { header: "Status" } }; // Sample data const data = [ { id: "entry-1", name: "Getting Started Guide", createdBy: "John Doe", lastModified: "1 hour ago", status: "Published" }, { id: "entry-2", name: "User Documentation", createdBy: "Jane Smith", lastModified: "3 days ago", status: "Draft" }, { id: "entry-3", name: "API Reference", createdBy: "John Doe", lastModified: "1 week ago", status: "Published" } ]; const WithCustomColumnClassNameDataTableExample = () => { return ( ); }; export default WithCustomColumnClassNameDataTableExample; ` } } /> ### With Sorting > ⚠️ Webiny’s DataTable component doesn’t perform sorting for you. > It only provides UI state (like sort indicators, sort direction, column clicked). > You need to sort the data manually based on the sorting state. = { name: { header: "Title", enableSorting: true }, createdBy: { header: "Author" }, lastModified: { header: "Last Modified", enableSorting: true }, status: { header: "Status" } }; // Sample data const data: Entry[] = [ { id: "entry-1", name: "Getting Started Guide", createdBy: "John Doe", lastModified: "1 hour ago", status: "Published" }, { id: "entry-2", name: "User Documentation", createdBy: "Jane Smith", lastModified: "3 days ago", status: "Draft" }, { id: "entry-3", name: "API Reference", createdBy: "John Doe", lastModified: "1 week ago", status: "Published" } ]; const WithSortingDataTableExample = () => { const [sorting, setSorting] = useState([ { id: "lastModified", desc: true } ]); return ( ); }; export default WithSortingDataTableExample; ` } } /> ### With Sub-rows Pass `getSubRows` to list rows under a row, e.g. the language versions of an entry. A row with sub-rows gets a toggle in its first column that shows and hides them, and each sub-row is indented under its parent. Every entry, sub-rows included, needs a unique `id`. Rows start collapsed. Pass `initialExpanded` to choose which rows start expanded: `true` for every row, or a map of entry ids. To control it yourself, pass `expanded` with `onExpandedChange`. = { name: { header: "Title" }, status: { header: "Status" } }; const data: Entry[] = [ { id: "entry-1", name: "Getting Started Guide", status: "Published", versions: [ { id: "entry-1-de", name: "Erste Schritte", status: "Draft" }, { id: "entry-1-hr", name: "Prvi koraci", status: "Published" } ] }, { id: "entry-2", name: "User Documentation", status: "Draft" } ]; const getSubRows = (row: Entry) => row.versions; const WithSubRowsDataTableExample = () => { return ( ); }; export default WithSubRowsDataTableExample; ` } } />