|
| 1 | +// Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 | + |
| 3 | +import {useMemo, useState, type Key} from 'react'; |
| 4 | +import type {Meta, StoryObj} from '@storybook/react'; |
| 5 | +import {createReorderable} from '@astryxdesign/core'; |
| 6 | +import { |
| 7 | + Table, |
| 8 | + pixel, |
| 9 | + useTableColumnSettings, |
| 10 | + useTableColumnSettingsState, |
| 11 | + useTableColumnsVirtualization, |
| 12 | + useTableSelection, |
| 13 | + useTableStickyColumns, |
| 14 | + type TableColumn, |
| 15 | +} from '@astryxdesign/core/Table'; |
| 16 | +import {VStack} from '@astryxdesign/core/Layout'; |
| 17 | +import {Text} from '@astryxdesign/core/Text'; |
| 18 | +import {useTableDragAndDropColumns} from '@astryxdesign/lab'; |
| 19 | + |
| 20 | +interface Task extends Record<string, unknown> { |
| 21 | + id: string; |
| 22 | + title: string; |
| 23 | + owner: string; |
| 24 | + status: string; |
| 25 | + due: string; |
| 26 | + points: number; |
| 27 | +} |
| 28 | + |
| 29 | +const TASKS: Task[] = [ |
| 30 | + { |
| 31 | + id: 't1', |
| 32 | + title: 'Draft the release notes', |
| 33 | + owner: 'Ava Chen', |
| 34 | + status: 'Open', |
| 35 | + due: 'Mon', |
| 36 | + points: 3, |
| 37 | + }, |
| 38 | + { |
| 39 | + id: 't2', |
| 40 | + title: 'Review the theme tokens', |
| 41 | + owner: 'Liam Park', |
| 42 | + status: 'Open', |
| 43 | + due: 'Tue', |
| 44 | + points: 5, |
| 45 | + }, |
| 46 | + { |
| 47 | + id: 't3', |
| 48 | + title: 'Check the empty states', |
| 49 | + owner: 'Zoe Vega', |
| 50 | + status: 'Done', |
| 51 | + due: 'Wed', |
| 52 | + points: 2, |
| 53 | + }, |
| 54 | + { |
| 55 | + id: 't4', |
| 56 | + title: 'Record the demo', |
| 57 | + owner: 'Max Ross', |
| 58 | + status: 'Open', |
| 59 | + due: 'Thu', |
| 60 | + points: 8, |
| 61 | + }, |
| 62 | +]; |
| 63 | + |
| 64 | +const COLUMNS: TableColumn<Task>[] = [ |
| 65 | + {key: 'title', header: 'Task', width: pixel(240)}, |
| 66 | + {key: 'owner', header: 'Owner', width: pixel(160)}, |
| 67 | + {key: 'status', header: 'Status', width: pixel(120)}, |
| 68 | + {key: 'due', header: 'Due', width: pixel(120)}, |
| 69 | + {key: 'points', header: 'Points', width: pixel(120), align: 'end'}, |
| 70 | +]; |
| 71 | + |
| 72 | +function moveKey(keys: ReadonlyArray<string>, key: Key, to: number): string[] { |
| 73 | + const rest = keys.filter(item => item !== key); |
| 74 | + return [...rest.slice(0, to), String(key), ...rest.slice(to)]; |
| 75 | +} |
| 76 | + |
| 77 | +function byKeys<T extends Record<string, unknown>>( |
| 78 | + columns: TableColumn<T>[], |
| 79 | + keys: ReadonlyArray<string>, |
| 80 | +): TableColumn<T>[] { |
| 81 | + return keys.flatMap(key => columns.filter(column => column.key === key)); |
| 82 | +} |
| 83 | + |
| 84 | +const meta: Meta = { |
| 85 | + title: 'Lab/TableDragAndDropColumns', |
| 86 | + tags: ['autodocs'], |
| 87 | +}; |
| 88 | + |
| 89 | +export default meta; |
| 90 | + |
| 91 | +function DefaultDemo() { |
| 92 | + const [order, setOrder] = useState(() => COLUMNS.map(column => column.key)); |
| 93 | + const reorderable = useMemo( |
| 94 | + () => |
| 95 | + createReorderable({ |
| 96 | + onReorder: ({key, to}) => setOrder(keys => moveKey(keys, key, to)), |
| 97 | + }), |
| 98 | + [], |
| 99 | + ); |
| 100 | + const dragColumns = useTableDragAndDropColumns<Task>({ |
| 101 | + reorderable, |
| 102 | + columnKeys: order, |
| 103 | + }); |
| 104 | + return ( |
| 105 | + <VStack gap={2}> |
| 106 | + <Table |
| 107 | + aria-label="Tasks" |
| 108 | + data={TASKS} |
| 109 | + columns={byKeys(COLUMNS, order)} |
| 110 | + idKey="id" |
| 111 | + plugins={{dragColumns}} |
| 112 | + /> |
| 113 | + <Text type="supporting" data-testid="order"> |
| 114 | + {order.join(' ')} |
| 115 | + </Text> |
| 116 | + </VStack> |
| 117 | + ); |
| 118 | +} |
| 119 | + |
| 120 | +/** |
| 121 | + * Focus the handle at the end of a column's header and press Space or Enter, |
| 122 | + * then Left and Right, to move the column one place at a time. Or tap the |
| 123 | + * handle, then the hint's moves. Escape or Done ends reorder mode. |
| 124 | + */ |
| 125 | +export const Default: StoryObj = { |
| 126 | + render: () => <DefaultDemo />, |
| 127 | +}; |
| 128 | + |
| 129 | +function ColumnSettingsDemo() { |
| 130 | + const [active, setActive] = useState<ReadonlyArray<string>>([ |
| 131 | + 'title', |
| 132 | + 'status', |
| 133 | + 'due', |
| 134 | + 'points', |
| 135 | + ]); |
| 136 | + const settings = useTableColumnSettingsState({ |
| 137 | + columns: COLUMNS.map(column => ({ |
| 138 | + key: column.key, |
| 139 | + label: String(column.header), |
| 140 | + isAlwaysVisible: column.key === 'title', |
| 141 | + })), |
| 142 | + activeColumnKeys: active, |
| 143 | + onChangeActiveColumnKeys: setActive, |
| 144 | + }); |
| 145 | + const columnSettings = useTableColumnSettings<Task>( |
| 146 | + settings.columnSettingsConfig, |
| 147 | + ); |
| 148 | + const [selected, setSelected] = useState<ReadonlySet<string>>(new Set()); |
| 149 | + const selection = useTableSelection<Task>({ |
| 150 | + getIsItemSelected: task => selected.has(task.id), |
| 151 | + onSelectItem: ({item, isSelected}) => |
| 152 | + setSelected(current => { |
| 153 | + const next = new Set(current); |
| 154 | + if (isSelected) { |
| 155 | + next.add(item.id); |
| 156 | + } else { |
| 157 | + next.delete(item.id); |
| 158 | + } |
| 159 | + return next; |
| 160 | + }), |
| 161 | + onSelectAll: ({isAllSelected}) => |
| 162 | + setSelected( |
| 163 | + isAllSelected ? new Set(TASKS.map(task => task.id)) : new Set(), |
| 164 | + ), |
| 165 | + getIsAllSelected: () => selected.size === TASKS.length, |
| 166 | + getRowLabel: task => task.title, |
| 167 | + }); |
| 168 | + const reorderable = useMemo( |
| 169 | + () => |
| 170 | + createReorderable({ |
| 171 | + onReorder: ({key, to}) => setActive(keys => moveKey(keys, key, to)), |
| 172 | + // The task column starts every row and stays in place. |
| 173 | + isReorderable: key => key !== 'title', |
| 174 | + // Each handle starts its header cell, and shows while the cell is |
| 175 | + // hovered or holds the focus where the pointer is fine. |
| 176 | + handle: {placement: 'start', revealBehavior: 'adaptive'}, |
| 177 | + }), |
| 178 | + [], |
| 179 | + ); |
| 180 | + const dragColumns = useTableDragAndDropColumns<Task>({ |
| 181 | + reorderable, |
| 182 | + columnKeys: settings.activeColumnKeys, |
| 183 | + }); |
| 184 | + const sticky = useTableStickyColumns<Task>({startKeys: ['title']}); |
| 185 | + return ( |
| 186 | + <VStack width={560} gap={2}> |
| 187 | + <Table |
| 188 | + aria-label="Tasks" |
| 189 | + data={TASKS} |
| 190 | + columns={COLUMNS} |
| 191 | + idKey="id" |
| 192 | + plugins={{columnSettings, selection, sticky, dragColumns}} |
| 193 | + /> |
| 194 | + <Text type="supporting" data-testid="order"> |
| 195 | + {settings.activeColumnKeys.join(' ')} |
| 196 | + </Text> |
| 197 | + </VStack> |
| 198 | + ); |
| 199 | +} |
| 200 | + |
| 201 | +/** |
| 202 | + * Column settings own the order: the moves count the active columns, and a |
| 203 | + * hidden column keeps its place in the settings. The task column is pinned |
| 204 | + * and stays first, and the selection column takes no part. Each handle |
| 205 | + * starts its header cell and shows while the cell is hovered or focused. |
| 206 | + */ |
| 207 | +export const WithColumnSettingsSelectionAndPinnedColumn: StoryObj = { |
| 208 | + render: () => <ColumnSettingsDemo />, |
| 209 | +}; |
| 210 | + |
| 211 | +interface Metrics extends Record<string, unknown> { |
| 212 | + id: string; |
| 213 | + [metric: string]: string | number; |
| 214 | +} |
| 215 | + |
| 216 | +const METRIC_COLUMNS: TableColumn<Metrics>[] = Array.from( |
| 217 | + {length: 60}, |
| 218 | + (_, index) => ({ |
| 219 | + key: `m${index + 1}`, |
| 220 | + header: `Metric ${index + 1}`, |
| 221 | + width: pixel(120), |
| 222 | + }), |
| 223 | +); |
| 224 | + |
| 225 | +const METRIC_ROWS: Metrics[] = Array.from({length: 20}, (_, row) => ({ |
| 226 | + id: `r${row + 1}`, |
| 227 | + ...Object.fromEntries( |
| 228 | + METRIC_COLUMNS.map((column, index) => [ |
| 229 | + column.key, |
| 230 | + (row + 1) * (index + 1), |
| 231 | + ]), |
| 232 | + ), |
| 233 | +})); |
| 234 | + |
| 235 | +function WindowedDemo() { |
| 236 | + const [order, setOrder] = useState(() => |
| 237 | + METRIC_COLUMNS.map(column => column.key), |
| 238 | + ); |
| 239 | + const reorderable = useMemo( |
| 240 | + () => |
| 241 | + createReorderable({ |
| 242 | + onReorder: ({key, to}) => setOrder(keys => moveKey(keys, key, to)), |
| 243 | + }), |
| 244 | + [], |
| 245 | + ); |
| 246 | + const dragColumns = useTableDragAndDropColumns<Metrics>({ |
| 247 | + reorderable, |
| 248 | + columnKeys: order, |
| 249 | + }); |
| 250 | + const virtualization = useTableColumnsVirtualization({ |
| 251 | + initialViewportWidth: 800, |
| 252 | + }); |
| 253 | + return ( |
| 254 | + <VStack width={800} gap={2}> |
| 255 | + <Table |
| 256 | + aria-label="Metrics" |
| 257 | + data={METRIC_ROWS} |
| 258 | + columns={byKeys(METRIC_COLUMNS, order)} |
| 259 | + idKey="id" |
| 260 | + virtualization={virtualization} |
| 261 | + plugins={{dragColumns}} |
| 262 | + dividers="grid" |
| 263 | + /> |
| 264 | + <Text type="supporting" data-testid="order"> |
| 265 | + {order.slice(0, 12).join(' ')} |
| 266 | + </Text> |
| 267 | + </VStack> |
| 268 | + ); |
| 269 | +} |
| 270 | + |
| 271 | +/** |
| 272 | + * Sixty columns stay windowed. Each move reports its index among all the |
| 273 | + * columns, and screen readers hear the column's place among all of them. |
| 274 | + */ |
| 275 | +export const WindowedColumns: StoryObj = { |
| 276 | + render: () => <WindowedDemo />, |
| 277 | +}; |
0 commit comments