|
| 1 | +import React from 'react'; |
| 2 | +import { StoryObj, Meta } from '@storybook/react'; |
| 3 | +import { Table, TableHeader, TableBody, Row, Cell, Column, Skeleton } from '../Table'; |
| 4 | +import { Text } from '../../Text/Text'; |
| 5 | + |
| 6 | +const meta: Meta = { |
| 7 | + title: 'Experimental/Components/Table', |
| 8 | + component: Table, |
| 9 | + parameters: { |
| 10 | + layout: 'centered' |
| 11 | + }, |
| 12 | + args: { |
| 13 | + label: 'Files' |
| 14 | + } |
| 15 | +}; |
| 16 | + |
| 17 | +export default meta; |
| 18 | + |
| 19 | +type Story = StoryObj<typeof Table>; |
| 20 | + |
| 21 | +export const Default: Story = { |
| 22 | + render: () => { |
| 23 | + const columns: Array<{ id: string; name: string; isRowHeader?: boolean }> = [ |
| 24 | + { name: 'Name', id: 'name', isRowHeader: true }, |
| 25 | + { name: 'Type', id: 'type' }, |
| 26 | + { name: 'Date Modified', id: 'date' } |
| 27 | + ]; |
| 28 | + |
| 29 | + const rows: Array<{ id: number; name: string; date: string; type: string }> = [ |
| 30 | + { id: 1, name: 'Games', date: '6/7/2020', type: 'File folder' }, |
| 31 | + { id: 2, name: 'Program Files', date: '4/7/2021', type: 'File folder' }, |
| 32 | + { id: 3, name: 'bootmgr', date: '11/20/2010', type: 'System file' }, |
| 33 | + { id: 4, name: 'log.txt', date: '1/18/2016', type: 'Text Document' }, |
| 34 | + { id: 5, name: 'log.txt', date: '1/18/2016', type: 'Text Document' }, |
| 35 | + { id: 6, name: 'log.txt', date: '1/18/2016', type: 'Text Document' }, |
| 36 | + { id: 7, name: 'log.txt', date: '1/18/2016', type: 'Text Document' } |
| 37 | + ]; |
| 38 | + |
| 39 | + return ( |
| 40 | + <Table aria-label="Files" selectionMode="multiple" selectionBehavior="replace"> |
| 41 | + <TableHeader columns={columns}> |
| 42 | + {column => <Column isRowHeader={column.isRowHeader}>{column.name}</Column>} |
| 43 | + </TableHeader> |
| 44 | + <TableBody items={rows}> |
| 45 | + {item => <Row columns={columns}>{column => <Cell>{item[column.id]}</Cell>}</Row>} |
| 46 | + </TableBody> |
| 47 | + </Table> |
| 48 | + ); |
| 49 | + } |
| 50 | +}; |
| 51 | + |
| 52 | +export const Loading: Story = { |
| 53 | + render: () => { |
| 54 | + const columns: Array<{ id: string; name: string; isRowHeader?: boolean }> = [ |
| 55 | + { name: 'Name', id: 'name', isRowHeader: true }, |
| 56 | + { name: 'Type', id: 'type' }, |
| 57 | + { name: 'Date Modified', id: 'date' } |
| 58 | + ]; |
| 59 | + |
| 60 | + return ( |
| 61 | + <Table aria-label="Files"> |
| 62 | + <TableHeader columns={columns}> |
| 63 | + {column => <Column isRowHeader={column.isRowHeader}>{column.name}</Column>} |
| 64 | + </TableHeader> |
| 65 | + <TableBody items={[{ id: 1 }, { id: 2 }, { id: 3 }]}> |
| 66 | + {() => ( |
| 67 | + <Row columns={columns}> |
| 68 | + {() => ( |
| 69 | + <Cell> |
| 70 | + <Skeleton /> |
| 71 | + </Cell> |
| 72 | + )} |
| 73 | + </Row> |
| 74 | + )} |
| 75 | + </TableBody> |
| 76 | + </Table> |
| 77 | + ); |
| 78 | + } |
| 79 | +}; |
| 80 | + |
| 81 | +export const Empty: Story = { |
| 82 | + render: () => { |
| 83 | + const columns: Array<{ id: string; name: string; isRowHeader?: boolean }> = [ |
| 84 | + { name: 'Name', id: 'name', isRowHeader: true }, |
| 85 | + { name: 'Type', id: 'type' }, |
| 86 | + { name: 'Date Modified', id: 'date' } |
| 87 | + ]; |
| 88 | + |
| 89 | + return ( |
| 90 | + <Table aria-label="Files"> |
| 91 | + <TableHeader columns={columns}> |
| 92 | + {column => <Column isRowHeader={column.isRowHeader}>{column.name}</Column>} |
| 93 | + </TableHeader> |
| 94 | + <TableBody |
| 95 | + items={[]} |
| 96 | + renderEmptyState={() => ( |
| 97 | + <div style={{ padding: '1rem', textAlign: 'center' }}> |
| 98 | + <Text variant="body1">No results found</Text> |
| 99 | + </div> |
| 100 | + )} |
| 101 | + > |
| 102 | + {[]} |
| 103 | + </TableBody> |
| 104 | + </Table> |
| 105 | + ); |
| 106 | + } |
| 107 | +}; |
| 108 | + |
| 109 | +export const Async: Story = { |
| 110 | + render: () => { |
| 111 | + type Character = { name: string; height: number; mass: number; birth_year: string }; |
| 112 | + const emptyCharacter: Character = { |
| 113 | + name: '', |
| 114 | + height: 0, |
| 115 | + mass: 0, |
| 116 | + birth_year: '' |
| 117 | + }; |
| 118 | + const pageSize = 10; |
| 119 | + const [isLoading, setIsLoading] = React.useState(true); |
| 120 | + const [items, setItems] = React.useState<Character[]>( |
| 121 | + /* eslint-disable-next-line unicorn/no-new-array */ |
| 122 | + new Array(pageSize).fill(emptyCharacter).map((value, idx) => ({ ...value, name: idx.toString() })) |
| 123 | + ); |
| 124 | + |
| 125 | + React.useEffect(() => { |
| 126 | + let ignore = false; |
| 127 | + |
| 128 | + async function startFetching() { |
| 129 | + const res = await fetch(`https://swapi.py4e.com/api/people`); |
| 130 | + const json = await res.json(); |
| 131 | + |
| 132 | + if (!ignore) { |
| 133 | + setItems(json.results); |
| 134 | + } |
| 135 | + |
| 136 | + setIsLoading(false); |
| 137 | + } |
| 138 | + |
| 139 | + // eslint-disable-next-line no-void |
| 140 | + void startFetching(); |
| 141 | + |
| 142 | + return () => { |
| 143 | + ignore = true; |
| 144 | + }; |
| 145 | + }, []); |
| 146 | + |
| 147 | + const columns: Array<{ id: string; name: string; isRowHeader?: boolean }> = [ |
| 148 | + { name: 'Name', id: 'name', isRowHeader: true }, |
| 149 | + { name: 'Height', id: 'height' }, |
| 150 | + { name: 'Mass', id: 'mass' }, |
| 151 | + { name: 'Birth Year', id: 'birth_year' } |
| 152 | + ]; |
| 153 | + |
| 154 | + return ( |
| 155 | + <Table aria-label="Star Wars Characters"> |
| 156 | + <TableHeader columns={columns}> |
| 157 | + {column => <Column isRowHeader={column.isRowHeader}>{column.name}</Column>} |
| 158 | + </TableHeader> |
| 159 | + <TableBody items={items}> |
| 160 | + {item => ( |
| 161 | + <Row id={item.name} columns={columns}> |
| 162 | + {column => <Cell>{isLoading ? <Skeleton /> : item[column.id]}</Cell>} |
| 163 | + </Row> |
| 164 | + )} |
| 165 | + </TableBody> |
| 166 | + </Table> |
| 167 | + ); |
| 168 | + } |
| 169 | +}; |
0 commit comments