Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 | import React, { useCallback, useMemo, useState } from 'react';
import { Box } from '@mui/material';
import { Story } from '@storybook/react';
import CellRenderer from '../components/table/TableCellRenderer';
import Table, { EnhancedTopBarSelectionProps, Props as TableProps } from '../components/table/index';
import { RendererProps } from '../components/table/types';
export default {
title: 'Table',
component: Table,
argTypes: {
density: {
options: ['comfortable', 'compact', 'roomy'],
control: { type: 'radio' },
},
},
};
function Renderer(props: RendererProps<any>): JSX.Element {
const { column } = props;
if (column.key === 'middlename') {
return <CellRenderer {...props} sx={{ fontStyle: 'italic' }} />;
}
return <CellRenderer {...props} />;
}
const TableWithState = (
args: Omit<TableProps<{ name: string; lastname: string }>, 'rows'> & { rowCount: number; currentPage?: number }
) => {
const [selectedRows, setSelectedRows] = useState<any>([]);
const rows: any = useMemo(() => {
return Array(args.rowCount)
.fill({ name: '', middlename: '', lastname: '', occupation: '' })
.map((i, j) => {
if (j % 2 === 0) {
return {
name: `Constanza_${j}-${args.currentPage ?? ''}`,
middlename: '',
lastname: 'Uanini',
occupation: 'Artist',
date: '2023-02-03',
pets: 5,
};
} else if (j % 3 === 0) {
return {
name: `Carlos_${j}-${args.currentPage ?? ''}`,
middlename: '--',
lastname: 'Thurber',
occupation: 'Freelancer',
available: 'false',
date: '2023-04-12',
pets: 10,
};
} else {
return {
name: `Jane${j}-${args.currentPage ?? ''}`,
middlename: 'John',
lastname: 'Doe',
occupation: 'Business analyst',
available: true,
date: '2023-04-27',
pets: 12,
previousStudies: 'natural Sciences, social Sciences',
};
}
});
}, [args.rowCount, args.currentPage]);
return (
<Box paddingTop={args.showTopBar ? '50px' : 0} display='flex' flexGrow={1} flexDirection='column'>
<Table
{...args}
rows={rows}
Renderer={Renderer}
selectedRows={selectedRows}
setSelectedRows={setSelectedRows}
topBarButtons={[
{
buttonType: 'productive',
buttonText: 'Click',
onButtonClick: () => window.alert('click'),
},
{
buttonType: 'passive',
buttonText: 'Disabled',
onButtonClick: () => window.alert('you should not see this'),
disabled: true,
},
{
buttonType: 'passive',
buttonText: 'Tooltip',
onButtonClick: () => window.alert('you should not see this either'),
disabled: true,
tooltipTitle:
'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.',
},
]}
tableComments='* Data collected in 2024'
/>
</Box>
);
};
const Template: Story<Omit<TableProps<{ name: string; lastname: string }>, 'rows'> & { rowCount: number }> = (args) => (
<TableWithState {...args} />
);
export const PageChangeCallback: Story<
Omit<TableProps<{ name: string; lastname: string }>, 'rows'> & { rowCount: number }
> = (args) => {
const [currentPage, setCurrentPage] = useState(1);
const onPageChange = useCallback((newPage: number) => {
// this is just showing off that the page number is being controlled outside the table component
const customizedPage = newPage * 2;
alert(`New Page: ${customizedPage}`);
setCurrentPage(customizedPage);
}, []);
return <TableWithState {...args} onPageChange={onPageChange} currentPage={currentPage} />;
};
export const Default = Template.bind({});
export const Selectable = Template.bind({});
export const Presorted = Template.bind({});
export const ShowTopBar = Template.bind({});
export const ShowTopBarV2 = Template.bind({});
Default.args = {
orderBy: 'name',
columns: [
{ key: 'name', name: 'Name', type: 'string' },
{
key: 'middlename',
name: 'Middlename',
type: 'string',
tooltipTitle: 'Middle name is optional',
sx: { fontStyle: 'italic' },
},
{ key: 'lastname', name: 'Lastname', type: 'string' },
{ key: 'occupation', name: 'Occupation', type: 'string' },
{ key: 'available', name: 'Available', type: 'boolean' },
{ key: 'previousStudies', name: 'Previous Studies', type: 'string', alignment: 'right' },
{ key: 'date', name: 'Date', type: 'string', tooltipTitle: 'Right aligend with tooltip', alignment: 'right' },
{ key: 'pets', name: 'Pets', type: 'string', alignment: 'right' },
],
rowCount: 201,
showTopBar: false,
booleanFalseText: 'No',
booleanTrueText: 'Yes',
editText: 'Edit',
renderNumSelectedText: (num) => `${num} selected`,
renderPaginationText: (from, to, total) => `${from} to ${to} of ${total}`,
density: 'comfortable',
};
Selectable.args = {
...Default.args,
showCheckbox: true,
controlledOnSelect: true,
};
Presorted.args = {
...Default.args,
isPresorted: true,
};
ShowTopBar.args = {
...Default.args,
showCheckbox: true,
controlledOnSelect: true,
showTopBar: true,
};
const enhancedTopBarSelectionConfig: EnhancedTopBarSelectionProps = {
renderEnhancedNumSelectedText(selectedCount: number, pageCount: number): string {
switch (true) {
case selectedCount === 1 && pageCount === 1: {
return `${selectedCount} row selected.`;
}
case selectedCount > 1 && pageCount === 1: {
return `${selectedCount} rows selected.`;
}
case selectedCount === 1 && pageCount > 1: {
return `${selectedCount} row selected across ${pageCount} pages.`;
}
case selectedCount > 1 && pageCount > 1: {
return `${selectedCount} rows selected across ${pageCount} pages.`;
}
}
return '';
},
renderSelectAllText(rowsCount: number): string {
return `Select all ${rowsCount} rows`;
},
renderSelectNoneText(): string {
return 'Clear selection';
},
};
ShowTopBarV2.args = {
...Default.args,
showCheckbox: true,
controlledOnSelect: true,
showTopBar: true,
enhancedTopBarSelectionConfig,
};
PageChangeCallback.args = {
...Default.args,
maxItemsPerPage: 10,
totalRowCount: 279,
};
|