import type { Meta, StoryObj } from '@storybook/react'; import React, { useState } from 'react'; import { Button } from '../../Button'; import { Table } from '../Table'; import { IconButton } from '../../IconButton'; import type { TableColumn } from '../types'; import { SnapshotContainer } from '../../../test-utils/SnapshotsContainer'; import { Select } from '../../Select'; import { action } from 'storybook/actions'; interface DataRow { id: number; accountPayable: string; supplierName: string; amount: string; } const data: DataRow[] = [ { id: 0, accountPayable: '401AIRBNB', supplierName: 'Airbnb', amount: '2€', }, { id: 1, accountPayable: '401DELOITTE', supplierName: 'Deloitte', amount: '324$', }, { id: 2, accountPayable: '401MAILCHIMP', supplierName: 'Mailchimp', amount: '13.29€', }, { id: 3, accountPayable: '401APPLE', supplierName: 'Apple', amount: '0€', }, ]; const moreData: DataRow[] = [ { id: 0, accountPayable: '401DELOITTE', supplierName: 'Deloitte GmbH', amount: '2€', }, { id: 1, accountPayable: '401DELOITTE', supplierName: 'Deloitte', amount: '324$', }, { id: 2, accountPayable: '401MAILCHIMP', supplierName: 'Mailchimp', amount: '13.29€', }, { id: 3, accountPayable: '401MAILCHIMP', supplierName: 'Mailchimp', amount: '45.2€', }, { id: 4, accountPayable: '401APPLE', supplierName: 'Apple', amount: '0€', }, ]; const columns: TableColumn[] = [ { id: 'accountPayable', header: 'Account payable', renderCell: ({ accountPayable }) => accountPayable, }, { id: 'supplierName', header: 'Supplier name', renderCell: ({ supplierName }) => supplierName, }, { id: 'amount', header: 'Amount', align: 'right', width: '20%', renderCell(row) { return {row.amount}; }, }, ]; const meta: Meta> = { title: 'Data display/Table', component: Table, args: { columns, data, getRowId: (row: DataRow) => String(row.id), }, }; export default meta; type Story = StoryObj>; export const Default: Story = {}; export const Compact: Story = { args: { rowHeight: 'compact', }, }; export const GroupBy: Story = { args: { data: moreData, groupBy: (row) => row.accountPayable, renderGroupedRowHeader(value, aggregatedOptions) { return

{`${value} ${aggregatedOptions.length}`}

; }, }, }; export const Clickable: Story = { render: (args) => { const [activeRow, setActiveRow] = useState(); return ( setActiveRow(row.id)} getIsRowActive={(row) => row.id === activeRow} /> ); }, }; export const WithActiveRows: Story = { args: { getIsRowActive: (row: DataRow) => row.id === 0 || row.id === 2, }, }; export const WithDisabledRows: Story = { args: { getIsRowDisabled: (row: DataRow) => row.id === 0 || row.id === 2, }, }; export const WithCheckableRows: Story = { args: { getIsRowCheckable: (row: DataRow) => row.id !== 0 && row.id !== 2, }, }; export const WithSort: Story = { args: { columns: [ { id: 'accountPayable', header: 'Account payable', renderCell: ({ accountPayable }) => accountPayable, getSortValue: (item) => item.accountPayable, }, { id: 'supplierName', header: 'Supplier name', renderCell: ({ supplierName }) => supplierName, }, { id: 'amount', header: 'Amount', align: 'right', width: '20%', renderCell(row) { return {row.amount}; }, getSortValue: (item) => parseInt(item.amount), }, ], }, }; export const WithFooter: Story = { args: { footer:
{ setSelectedRowsIds((options) => { if (checked) { return options.concat(id); } return options.filter((optionId) => optionId !== id); }); }} onAllRowsSelectionChange={(_, ids, checked) => { setSelectedRowsIds(checked ? ids : []); }} /> ); }, }; export const SelectableAndDisabled: Story = { render: (args) => { const [selectedRowIds, setSelectedRowsIds] = useState([]); return (
{ setSelectedRowsIds((options) => { if (checked) { return options.concat(id); } return options.filter((optionId) => optionId !== id); }); }} onAllRowsSelectionChange={(_, ids, checked) => { setSelectedRowsIds(checked ? ids : []); }} getIsRowDisabled={(row) => row.id === 1} /> ); }, }; export const WithRowHovered: Story = { args: { columns: [ { id: 'accountPayable', header: 'Account payable', renderCell: ({ accountPayable }) => accountPayable, }, { id: 'supplierName', header: 'Supplier name', renderCell: ({ supplierName }) => supplierName, }, { id: 'amount', header: '', align: 'right', width: '20%', renderCell(row, { isRowHovered }) { return isRowHovered ? ( console.log('edit')} aria-label="Edit" /> ) : (
); }, }, ], }, }; export const Snapshot: Story = { parameters: { chromatic: { disableSnapshot: false }, }, render: () => ( ), }; const suppliers = [ { key: 'Mailchimp', label: 'Mailchimp' }, { key: 'Apple', label: 'Apple' }, { key: 'Deloitte', label: 'Deloitte' }, { key: 'Deloitte GmbH', label: 'Deloitte GmbH' }, { key: 'Airbnb', label: 'Airbnb' }, ]; export const WithDropdownInside: Story = { args: { columns: [ { id: 'supplierName', header: 'Supplier Name', renderCell: ({ supplierName }) => (