|
| 1 | +import React from 'react' |
| 2 | + |
| 3 | +import { createColumnHelper, getCoreRowModel, useReactTable } from '@tanstack/react-table' |
| 4 | + |
| 5 | +import { TanstackTable } from '@genseki/react' |
| 6 | +import { Typography } from '@genseki/react/v2' |
| 7 | + |
| 8 | +import { PlaygroundCard } from '../../../components/card' |
| 9 | + |
| 10 | +type User = { |
| 11 | + id: number |
| 12 | + fname: string |
| 13 | + lname: string |
| 14 | + food: string |
| 15 | +} |
| 16 | + |
| 17 | +const columnHelper = createColumnHelper<User>() |
| 18 | + |
| 19 | +const columns = [ |
| 20 | + columnHelper.accessor('id', { |
| 21 | + header: 'ID', |
| 22 | + cell: (props) => <p>{props.getValue()}</p>, |
| 23 | + }), |
| 24 | + columnHelper.accessor('fname', { |
| 25 | + header: 'First Name', |
| 26 | + cell: (props) => <p>{props.getValue()}</p>, |
| 27 | + }), |
| 28 | + columnHelper.accessor('lname', { |
| 29 | + header: 'Last Name', |
| 30 | + cell: (props) => <p>{props.getValue()}</p>, |
| 31 | + }), |
| 32 | + columnHelper.accessor('food', { |
| 33 | + header: 'Favorite Food', |
| 34 | + cell: (props) => <p>{props.getValue()}</p>, |
| 35 | + }), |
| 36 | +] |
| 37 | + |
| 38 | +function BasicTable() { |
| 39 | + const users: User[] = [ |
| 40 | + { |
| 41 | + id: 1, |
| 42 | + fname: 'Supakorn', |
| 43 | + lname: 'Netsuwan', |
| 44 | + food: 'Hamburger', |
| 45 | + }, |
| 46 | + { |
| 47 | + id: 2, |
| 48 | + fname: 'Jane', |
| 49 | + lname: 'Doe', |
| 50 | + food: 'Pizza', |
| 51 | + }, |
| 52 | + { |
| 53 | + id: 3, |
| 54 | + fname: 'John', |
| 55 | + lname: 'Smith', |
| 56 | + food: 'Sushi', |
| 57 | + }, |
| 58 | + { |
| 59 | + id: 4, |
| 60 | + fname: 'Emily', |
| 61 | + lname: 'Johnson', |
| 62 | + food: 'Tacos', |
| 63 | + }, |
| 64 | + { |
| 65 | + id: 5, |
| 66 | + fname: 'Michael', |
| 67 | + lname: 'Brown', |
| 68 | + food: 'Pasta', |
| 69 | + }, |
| 70 | + ] |
| 71 | + |
| 72 | + const table = useReactTable<User>({ |
| 73 | + getCoreRowModel: getCoreRowModel(), |
| 74 | + data: users, |
| 75 | + columns, |
| 76 | + }) |
| 77 | + |
| 78 | + return ( |
| 79 | + <div> |
| 80 | + <TanstackTable table={table} /> |
| 81 | + </div> |
| 82 | + ) |
| 83 | +} |
| 84 | + |
| 85 | +function SortableTable() { |
| 86 | + const users: User[] = [ |
| 87 | + { |
| 88 | + id: 1, |
| 89 | + fname: 'Supakorn', |
| 90 | + lname: 'Netsuwan', |
| 91 | + food: 'Hamburger', |
| 92 | + }, |
| 93 | + { |
| 94 | + id: 2, |
| 95 | + fname: 'Jane', |
| 96 | + lname: 'Doe', |
| 97 | + food: 'Pizza', |
| 98 | + }, |
| 99 | + { |
| 100 | + id: 3, |
| 101 | + fname: 'John', |
| 102 | + lname: 'Smith', |
| 103 | + food: 'Sushi', |
| 104 | + }, |
| 105 | + { |
| 106 | + id: 4, |
| 107 | + fname: 'Emily', |
| 108 | + lname: 'Johnson', |
| 109 | + food: 'Tacos', |
| 110 | + }, |
| 111 | + { |
| 112 | + id: 5, |
| 113 | + fname: 'Michael', |
| 114 | + lname: 'Brown', |
| 115 | + food: 'Pasta', |
| 116 | + }, |
| 117 | + ] |
| 118 | + |
| 119 | + const table = useReactTable<User>({ |
| 120 | + getCoreRowModel: getCoreRowModel(), |
| 121 | + data: users, |
| 122 | + columns, |
| 123 | + }) |
| 124 | + |
| 125 | + return ( |
| 126 | + <div> |
| 127 | + <TanstackTable |
| 128 | + table={table} |
| 129 | + configuration={{ |
| 130 | + sortBy: [['id'], ['fname'], ['lname'], ['food']], |
| 131 | + }} |
| 132 | + /> |
| 133 | + </div> |
| 134 | + ) |
| 135 | +} |
| 136 | + |
| 137 | +function LoadingTable() { |
| 138 | + const users: User[] = [] |
| 139 | + |
| 140 | + const table = useReactTable<User>({ |
| 141 | + getCoreRowModel: getCoreRowModel(), |
| 142 | + data: users, |
| 143 | + columns, |
| 144 | + }) |
| 145 | + |
| 146 | + return ( |
| 147 | + <div> |
| 148 | + <TanstackTable table={table} isLoading={true} /> |
| 149 | + </div> |
| 150 | + ) |
| 151 | +} |
| 152 | + |
| 153 | +function EmptyTable() { |
| 154 | + const users: User[] = [] |
| 155 | + |
| 156 | + const table = useReactTable<User>({ |
| 157 | + getCoreRowModel: getCoreRowModel(), |
| 158 | + data: users, |
| 159 | + columns, |
| 160 | + }) |
| 161 | + |
| 162 | + return ( |
| 163 | + <div> |
| 164 | + <TanstackTable table={table} /> |
| 165 | + </div> |
| 166 | + ) |
| 167 | +} |
| 168 | + |
| 169 | +function ErrorTable() { |
| 170 | + const users: User[] = [] |
| 171 | + |
| 172 | + const table = useReactTable<User>({ |
| 173 | + getCoreRowModel: getCoreRowModel(), |
| 174 | + data: users, |
| 175 | + columns, |
| 176 | + }) |
| 177 | + |
| 178 | + return ( |
| 179 | + <div> |
| 180 | + <TanstackTable |
| 181 | + table={table} |
| 182 | + isError={true} |
| 183 | + errorMessage="Failed to load data. Please try again later." |
| 184 | + /> |
| 185 | + </div> |
| 186 | + ) |
| 187 | +} |
| 188 | + |
| 189 | +export const TableSection = React.memo(function () { |
| 190 | + return ( |
| 191 | + <div className="grid gap-8"> |
| 192 | + <PlaygroundCard title="Basic Table" categoryTitle="Component"> |
| 193 | + <Typography type="body" className="text-muted-foreground mb-4"> |
| 194 | + A simple table with basic data display functionality. |
| 195 | + </Typography> |
| 196 | + |
| 197 | + <div className="p-4 bg-background w-full rounded-lg"> |
| 198 | + <BasicTable /> |
| 199 | + </div> |
| 200 | + </PlaygroundCard> |
| 201 | + |
| 202 | + <PlaygroundCard title="Sortable Table" categoryTitle="Component"> |
| 203 | + <Typography type="body" className="text-muted-foreground mb-4"> |
| 204 | + Table with sortable columns. Click on column headers to sort. |
| 205 | + </Typography> |
| 206 | + <div className="p-4 bg-background w-full rounded-lg"> |
| 207 | + <SortableTable /> |
| 208 | + </div> |
| 209 | + </PlaygroundCard> |
| 210 | + |
| 211 | + <PlaygroundCard title="Loading State" categoryTitle="Component"> |
| 212 | + <Typography type="body" className="text-muted-foreground mb-4"> |
| 213 | + Table displaying a loading state while data is being fetched. |
| 214 | + </Typography> |
| 215 | + <div className="p-4 bg-background w-full rounded-lg"> |
| 216 | + <LoadingTable /> |
| 217 | + </div> |
| 218 | + </PlaygroundCard> |
| 219 | + |
| 220 | + <PlaygroundCard title="Empty State" categoryTitle="Component"> |
| 221 | + <Typography type="body" className="text-muted-foreground mb-4"> |
| 222 | + Table displaying an empty state when no data is available. |
| 223 | + </Typography> |
| 224 | + <div className="p-4 bg-background w-full rounded-lg"> |
| 225 | + <EmptyTable /> |
| 226 | + </div> |
| 227 | + </PlaygroundCard> |
| 228 | + |
| 229 | + <PlaygroundCard title="Error State" categoryTitle="Component"> |
| 230 | + <Typography type="body" className="text-muted-foreground mb-4"> |
| 231 | + Table displaying an error state when data fetching fails. |
| 232 | + </Typography> |
| 233 | + <div className="p-4 bg-background w-full rounded-lg"> |
| 234 | + <ErrorTable /> |
| 235 | + </div> |
| 236 | + </PlaygroundCard> |
| 237 | + </div> |
| 238 | + ) |
| 239 | +}) |
| 240 | + |
| 241 | +TableSection.displayName = 'TableSection' |
0 commit comments