|
| 1 | +import dataLarge from '@sb/mockData/Friends500.json'; |
1 | 2 | import type { Meta, StoryObj } from '@storybook/react'; |
2 | 3 | import TableGrowingMode from '@ui5/webcomponents/dist/types/TableGrowingMode.js'; |
3 | 4 | import TableSelectionMode from '@ui5/webcomponents/dist/types/TableSelectionMode.js'; |
| 5 | +import type { TableVirtualizerPropTypes } from '@ui5/webcomponents-react'; |
4 | 6 | import { SegmentedButton, SegmentedButtonItem } from '@ui5/webcomponents-react'; |
5 | | -import { useState } from 'react'; |
| 7 | +import { useEffect, useState } from 'react'; |
6 | 8 | import { TableCell } from '../TableCell/index.js'; |
7 | 9 | import { TableGrowing } from '../TableGrowing/index.js'; |
8 | 10 | import { TableHeaderCell } from '../TableHeaderCell/index.js'; |
9 | 11 | import { TableHeaderRow } from '../TableHeaderRow/index.js'; |
10 | 12 | import { TableRow } from '../TableRow/index.js'; |
11 | 13 | import { TableSelection } from '../TableSelection/index.js'; |
| 14 | +import { TableVirtualizer } from '../TableVirtualizer/index.js'; |
12 | 15 | import { Table } from './index.js'; |
13 | 16 |
|
14 | 17 | const meta = { |
@@ -182,3 +185,72 @@ export const WithSelection: Story = { |
182 | 185 | ); |
183 | 186 | } |
184 | 187 | }; |
| 188 | + |
| 189 | +const dataLargeWithPosition = dataLarge.map((item, index) => ({ ...item, position: index })); |
| 190 | + |
| 191 | +export const VirtualizedTableRows: Story = { |
| 192 | + args: { className: 'tableHeightContentDensity' }, |
| 193 | + render(args) { |
| 194 | + const [data, setData] = useState(dataLargeWithPosition.slice(0, 9)); |
| 195 | + const [isCozy, setIsCozy] = useState(true); |
| 196 | + |
| 197 | + const handleRangeChange: TableVirtualizerPropTypes['onRangeChange'] = (e) => { |
| 198 | + const { first, last } = e.detail; |
| 199 | + |
| 200 | + // overscanCount = 2 |
| 201 | + const overscanCountStart = Math.max(first - 2, 0); |
| 202 | + const overscanCountEnd = Math.min(last + 2, dataLargeWithPosition.length); |
| 203 | + setData(dataLargeWithPosition.slice(overscanCountStart, overscanCountEnd)); |
| 204 | + }; |
| 205 | + |
| 206 | + // adjust row height according to content-density mode (only for demo purposes) |
| 207 | + useEffect(() => { |
| 208 | + const body = document.body; |
| 209 | + if (!body) return; |
| 210 | + |
| 211 | + const observer = new MutationObserver((mutationsList) => { |
| 212 | + mutationsList.forEach((mutation) => { |
| 213 | + if (mutation.type === 'attributes' && mutation.attributeName === 'class') { |
| 214 | + setIsCozy(!body.classList.contains('ui5-content-density-compact')); |
| 215 | + } |
| 216 | + }); |
| 217 | + }); |
| 218 | + observer.observe(body, { attributes: true, attributeFilter: ['class'] }); |
| 219 | + return () => { |
| 220 | + observer.disconnect(); |
| 221 | + }; |
| 222 | + }, []); |
| 223 | + |
| 224 | + return ( |
| 225 | + <Table |
| 226 | + {...args} |
| 227 | + headerRow={ |
| 228 | + <TableHeaderRow sticky> |
| 229 | + <TableHeaderCell>Name</TableHeaderCell> |
| 230 | + <TableHeaderCell>Age</TableHeaderCell> |
| 231 | + <TableHeaderCell>Friend Name</TableHeaderCell> |
| 232 | + <TableHeaderCell>Friend Age</TableHeaderCell> |
| 233 | + </TableHeaderRow> |
| 234 | + } |
| 235 | + features={<TableVirtualizer rowCount={500} rowHeight={isCozy ? 44 : 32} onRangeChange={handleRangeChange} />} |
| 236 | + > |
| 237 | + {data.map((row) => ( |
| 238 | + <TableRow key={row.position} position={row.position}> |
| 239 | + <TableCell> |
| 240 | + <span>{row.name}</span> |
| 241 | + </TableCell> |
| 242 | + <TableCell> |
| 243 | + <span>{row.age}</span> |
| 244 | + </TableCell> |
| 245 | + <TableCell> |
| 246 | + <span>{row.friend.name}</span> |
| 247 | + </TableCell> |
| 248 | + <TableCell> |
| 249 | + <span>{row.friend.age}</span> |
| 250 | + </TableCell> |
| 251 | + </TableRow> |
| 252 | + ))} |
| 253 | + </Table> |
| 254 | + ); |
| 255 | + } |
| 256 | +}; |
0 commit comments