import React, { Fragment, useState } from 'react'; import { StoryFn } from '@storybook/react'; import { Button } from '@leafygreen-ui/button'; import { css, cx } from '@leafygreen-ui/emotion'; import { Cell, ExpandedContent, flexRender, HeaderCell, type HeaderGroup, HeaderRow, type LeafyGreenTableRow, type LGColumnDef, Row, Table, TableBody, TableHead, type TableProps, useLeafyGreenTable, } from '..'; import { makeKitchenSinkData, Person } from './makeData.testutils'; type StoryTableProps = TableProps; export const DynamicDataComponent: StoryFn = args => { const tableContainerRef = React.useRef(null); const [data] = useState(() => makeKitchenSinkData(10)); const [showEmoji, setShowEmoji] = useState(true); const columns = React.useMemo>>( () => [ { accessorKey: 'dateCreated', header: 'Date Created', enableSorting: true, cell: info => (info.getValue() as Date).toLocaleDateString('en-us', { year: 'numeric', month: 'short', day: 'numeric', }), }, { accessorKey: 'frequency', header: 'Frequency', }, { accessorKey: 'clusterType', header: 'Cluster Type', }, { accessorKey: 'mdbVersion', header: 'MongoDB Version', enableSorting: true, size: 90, cell: info => { return `${info.getValue()} ${showEmoji ? 'with 🥬' : ''}`; }, }, ], [showEmoji], ); const table = useLeafyGreenTable({ data, columns, }); const { rows } = table.getRowModel(); return (
{table.getHeaderGroups().map((headerGroup: HeaderGroup) => ( {headerGroup.headers.map((header, index) => { return ( {flexRender( header.column.columnDef.header, header.getContext(), )} ); })} ))} {rows.map((row: LeafyGreenTableRow) => { const isExpandedContent = row.isExpandedContent ?? false; return ( {!isExpandedContent && ( {row.getVisibleCells().map(cell => { return ( {flexRender( cell.column.columnDef.cell, cell.getContext(), )} ); })} )} {isExpandedContent && } ); })}
); };