import React from 'react'; import { storybookArgTypes, storybookExcludedControlParams, StoryMetaType, } from '@lg-tools/storybook-utils'; import { StoryFn } from '@storybook/react'; import { Button } from '@leafygreen-ui/button'; import { css } from '@leafygreen-ui/emotion'; import LeafyGreenProvider from '@leafygreen-ui/leafygreen-provider'; import { DarkModeProps } from '@leafygreen-ui/lib'; import { Cell, HeaderCell } from '../Cell'; import { HeaderRow, Row } from '../Row'; import Table from '../Table/Table'; import TableBody from '../TableBody/TableBody'; import TableHead from '../TableHead/TableHead'; import useLeafyGreenTable, { LeafyGreenTableCell, LeafyGreenTableRow, } from '../useLeafyGreenTable'; import { makeData, Person } from '../utils/makeData.testutils'; import { AnyDict } from '../utils/types'; import { ColumnDef, ExpandedState, flexRender, HeaderGroup, LGColumnDef, RowProps, } from '..'; const rowCells = ( <> 1 Est mollitia laborum dolores dolorem corporis explicabo nobis enim omnis. Minima excepturi accusantium iure culpa. MongoDB 7.85 Complicated ); const meta: StoryMetaType = { title: 'Composition/Data Display/Table/Row', component: Row, argTypes: { disabled: { control: 'boolean' }, }, parameters: { default: null, controls: { exclude: [ ...storybookExcludedControlParams, 'ref', 'children', 'row', 'virtualRow', ], }, chromatic: { disableSnapshot: true, }, // This is needed as a workaround to make arg spreading performant // https://github.com/storybookjs/storybook/issues/11657 docs: { source: { type: 'code' }, }, generate: { combineArgs: { darkMode: [false, true], disabled: [false, true], // @ts-ignore - this is a table prop shouldTruncate: [false, true], verticalAlignment: ['top', 'center'], shouldAlternateRowColor: [false, true], }, excludeCombinations: [ { // @ts-ignore - this is a table prop shouldTruncate: true, verticalAlignment: 'center', }, ], decorator: (Instance, ctx) => { return ( {Array.from({ length: 3 }).map((_, index) => ( {rowCells} ))}
); }, }, }, }; export default meta; const Template: StoryFn = args => { const data = makeData(false, 100); const columns = Object.keys(data[0]); return ( {columns.map((columnName: string) => ( {columnName} ))} {data.map((row: AnyDict) => ( {Object.keys(row).map((cellKey: string, index: number) => { return {row[cellKey]}; })} ))}
); }; export const DisabledRows = Template.bind({}); DisabledRows.args = { disabled: true, }; export const DisabledNestedRows: StoryFn = ({ row, ...rest }) => { const tableContainerRef = React.useRef(null); const data = React.useState(() => makeData(false, 100, 5, 3))[0]; const [expanded, setExpanded] = React.useState({}); const columns = React.useMemo>>( () => [ { accessorKey: 'id', header: 'ID', size: 60, }, { accessorKey: 'firstName', header: 'First Name', cell: info => info.getValue(), }, { accessorFn: row => row.lastName, id: 'lastName', cell: info => info.getValue(), header: () => Last Name, }, { accessorKey: 'age', header: () => 'Age', size: 50, }, { accessorKey: 'visits', header: () => Visits, size: 50, }, { accessorKey: 'status', header: 'Status', size: 90, }, ], [], ); const table = useLeafyGreenTable({ data, columns, state: { expanded, }, onExpandedChange: setExpanded, }); const { rows } = table.getRowModel(); return ( <>

{table.getRowModel().rows.length} total rows

Expanded rows: {JSON.stringify(expanded, null, 2)}
{table.getHeaderGroups().map((headerGroup: HeaderGroup) => ( {headerGroup.headers.map(header => { return ( {flexRender( header.column.columnDef.header, header.getContext(), )} ); })} ))} {rows.map((row: LeafyGreenTableRow) => { return ( {row .getVisibleCells() .map((cell: LeafyGreenTableCell) => { return ( {flexRender( cell.column.columnDef.cell, cell.getContext(), )} ); })} ); })}
); }; DisabledNestedRows.args = { disabled: true, }; export const ClickableRows = Template.bind({}); ClickableRows.args = { onClick: () => { // eslint-disable-next-line no-console console.log('row clicked!'); }, }; export const DisabledClickableRows = Template.bind({}); DisabledClickableRows.args = { onClick: () => { // eslint-disable-next-line no-console console.log('row clicked!'); }, disabled: true, }; export const DisabledSelectableRows: StoryFn< RowProps & DarkModeProps > = ({ darkMode, ...args }: DarkModeProps & RowProps) => { const data = React.useState(() => makeData(false, 100))[0]; const [rowSelection, setRowSelection] = React.useState({}); const columns = React.useMemo>>( () => [ { accessorKey: 'id', header: 'ID', size: 60, }, { accessorKey: 'firstName', header: 'First Name', cell: info => info.getValue(), }, { accessorFn: row => row.lastName, id: 'lastName', cell: info => info.getValue(), header: () => Last Name, }, { accessorKey: 'age', header: () => 'Age', size: 50, }, { accessorKey: 'visits', header: () => Visits, size: 50, }, { accessorKey: 'status', header: 'Status', size: 90, }, ], [], ); const table = useLeafyGreenTable({ data, columns, state: { rowSelection, }, onRowSelectionChange: setRowSelection, enableRowSelection: !args.disabled, hasSelectableRows: true, }); 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) => { return ( {row.getVisibleCells().map(cell => { return ( {flexRender( cell.column.columnDef.cell, cell.getContext(), )} ); })} ); })}
); }; DisabledSelectableRows.argTypes = { darkMode: storybookArgTypes.darkMode, }; DisabledSelectableRows.args = { disabled: true, }; export const Generated = () => <>;