import React, { Fragment, useState } from 'react'; import { storybookExcludedControlParams, StoryMetaType, } from '@lg-tools/storybook-utils'; import { StoryFn } from '@storybook/react'; import { expect, userEvent, waitFor, within } from '@storybook/test'; import { Badge } from '@leafygreen-ui/badge'; import { css } from '@leafygreen-ui/emotion'; import { Icon } from '@leafygreen-ui/icon'; import { IconButton } from '@leafygreen-ui/icon-button'; import { getTestUtils } from './testing/getTestUtils'; import { makeKitchenSinkData, Person } from './utils/makeData.testutils'; import { DynamicDataComponent } from './utils/stories.testutils'; import { Cell, ExpandedContent, flexRender, getLgIds, HeaderCell, type HeaderGroup, HeaderRow, type LeafyGreenTableRow, type LGColumnDef, Row, Table, TableBody, TableHead, TableProps, useLeafyGreenTable, } from '.'; const meta: StoryMetaType = { title: 'Composition/Data Display/Table/Interactions', component: Table, parameters: { default: 'Template', controls: { exclude: [ ...storybookExcludedControlParams, 'table', 'children', 'value', 'onSelectChange', 'tableContainerClassName', 'baseFontSize', ], }, // This is needed as a workaround to make arg spreading performant // https://github.com/storybookjs/storybook/issues/11657 docs: { source: { type: 'code' }, }, }, }; export default meta; type StoryTableProps = TableProps; const Template: StoryFn = args => { const [data] = useState(() => makeKitchenSinkData(50)); 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: 'encryptorEnabled', header: 'Encryptor', cell: info => ( {info.getValue() ? 'Enabled' : 'Not enabled'} ), }, { accessorKey: 'mdbVersion', header: 'MongoDB Version', enableSorting: true, size: 90, }, { id: 'actions', header: '', size: 90, cell: _ => { return ( <> ); }, }, ], [], ); const table = useLeafyGreenTable({ data, columns, }); const { rows } = table.getRowModel(); return (
{table.getHeaderGroups().map((headerGroup: HeaderGroup) => ( {headerGroup.headers.map(header => { 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 && } ); })}
); }; export const StickyHeader = { render: (args: StoryTableProps) =>