|
1 | | -import type { Meta, StoryObj } from '@storybook/react'; |
| 1 | +import type { Meta, StoryContext, StoryObj } from '@storybook/react'; |
| 2 | +import { expect, within } from '@storybook/test'; |
2 | 3 | import { useMemo } from 'react'; |
3 | 4 | import { type LoaderFunctionArgs, useLoaderData, useSearchParams } from 'react-router'; |
4 | 5 | import { columnConfigs, columns } from './data-table-stories.components'; |
@@ -252,10 +253,200 @@ function DataTableWithBazzaFilters() { |
252 | 253 | ); |
253 | 254 | } |
254 | 255 |
|
| 256 | +// --- DataTableWithScrolling --- |
| 257 | +function DataTableWithScrolling() { |
| 258 | + // Get the loader data (filtered/paginated/sorted data from server) |
| 259 | + const loaderData = useLoaderData<DataResponse>(); |
| 260 | + const [searchParams, setSearchParams] = useSearchParams(); |
| 261 | + |
| 262 | + // Initialize data from loader response |
| 263 | + const data = loaderData?.data ?? []; |
| 264 | + const pageCount = loaderData?.meta.pageCount ?? 0; |
| 265 | + const facetedCounts = loaderData?.facetedCounts ?? {}; |
| 266 | + |
| 267 | + // Convert facetedCounts to the correct type for useDataTableFilters (Map-based) |
| 268 | + const facetedOptionCounts = useMemo(() => { |
| 269 | + const result: Partial<Record<string, Map<string, number>>> = {}; |
| 270 | + Object.entries(facetedCounts).forEach(([col, valueObj]) => { |
| 271 | + result[col] = new Map(Object.entries(valueObj)); |
| 272 | + }); |
| 273 | + return result; |
| 274 | + }, [facetedCounts]); |
| 275 | + |
| 276 | + // --- Bazza UI Filter Setup --- |
| 277 | + // 1. Initialize filters state with useFilterSync (syncs with URL) |
| 278 | + const [filters, setFilters] = useFilterSync(); |
| 279 | + |
| 280 | + // --- Read pagination and sorting directly from URL --- |
| 281 | + // Use larger page size to ensure scrolling is needed |
| 282 | + const pageIndex = Number.parseInt(searchParams.get('page') ?? '0', 10); |
| 283 | + const pageSize = Number.parseInt(searchParams.get('pageSize') ?? '20', 10); |
| 284 | + const sortField = searchParams.get('sortField'); |
| 285 | + const sortOrder = (searchParams.get('sortOrder') || 'asc') as 'asc' | 'desc'; |
| 286 | + |
| 287 | + // --- Pagination and Sorting State --- |
| 288 | + const pagination = { pageIndex, pageSize }; |
| 289 | + const sorting = sortField ? [{ id: sortField, desc: sortOrder === 'desc' }] : []; |
| 290 | + |
| 291 | + // --- Event Handlers: update URL directly --- |
| 292 | + const handlePaginationChange: OnChangeFn<PaginationState> = (updaterOrValue) => { |
| 293 | + const next = typeof updaterOrValue === 'function' ? updaterOrValue(pagination) : updaterOrValue; |
| 294 | + searchParams.set('page', next.pageIndex.toString()); |
| 295 | + searchParams.set('pageSize', next.pageSize.toString()); |
| 296 | + setSearchParams(searchParams); |
| 297 | + }; |
| 298 | + |
| 299 | + const handleSortingChange: OnChangeFn<SortingState> = (updaterOrValue) => { |
| 300 | + const next = typeof updaterOrValue === 'function' ? updaterOrValue(sorting) : updaterOrValue; |
| 301 | + if (next.length > 0) { |
| 302 | + searchParams.set('sortField', next[0].id); |
| 303 | + searchParams.set('sortOrder', next[0].desc ? 'desc' : 'asc'); |
| 304 | + } else { |
| 305 | + searchParams.delete('sortField'); |
| 306 | + searchParams.delete('sortOrder'); |
| 307 | + } |
| 308 | + setSearchParams(searchParams); |
| 309 | + }; |
| 310 | + |
| 311 | + // --- Bazza UI Filter Setup --- |
| 312 | + const bazzaProcessedColumns = useMemo(() => columnConfigs, []); |
| 313 | + |
| 314 | + // Define a filter strategy (replace with your actual strategy if needed) |
| 315 | + const filterStrategy = 'server' as const; |
| 316 | + |
| 317 | + // Setup filter actions and strategy (controlled mode) |
| 318 | + const { |
| 319 | + columns: filterColumns, |
| 320 | + actions, |
| 321 | + strategy, |
| 322 | + } = useDataTableFilters({ |
| 323 | + columnsConfig: bazzaProcessedColumns, |
| 324 | + filters, |
| 325 | + onFiltersChange: setFilters, |
| 326 | + faceted: facetedOptionCounts, |
| 327 | + strategy: filterStrategy, |
| 328 | + data, |
| 329 | + }); |
| 330 | + |
| 331 | + // --- TanStack Table Setup --- |
| 332 | + const table = useReactTable({ |
| 333 | + data, |
| 334 | + columns, |
| 335 | + pageCount, |
| 336 | + state: { |
| 337 | + pagination, |
| 338 | + sorting, |
| 339 | + }, |
| 340 | + onPaginationChange: handlePaginationChange, |
| 341 | + onSortingChange: handleSortingChange, |
| 342 | + getCoreRowModel: getCoreRowModel(), |
| 343 | + getPaginationRowModel: getPaginationRowModel(), |
| 344 | + getSortedRowModel: getSortedRowModel(), |
| 345 | + manualPagination: true, |
| 346 | + manualSorting: true, |
| 347 | + }); |
| 348 | + |
| 349 | + return ( |
| 350 | + <div className="space-y-4"> |
| 351 | + <div> |
| 352 | + <h1 className="text-2xl font-bold mb-4">Data Table with Scrolling and Sticky Header</h1> |
| 353 | + <p className="text-gray-600 mb-6"> |
| 354 | + This demonstrates the table with vertical scrolling and a sticky header that remains visible while scrolling |
| 355 | + through table rows. The table is contained within a fixed-height container. |
| 356 | + </p> |
| 357 | + </div> |
| 358 | + |
| 359 | + {/* Bazza UI Filter Interface */} |
| 360 | + <DataTableFilter columns={filterColumns} filters={filters} actions={actions} strategy={strategy} /> |
| 361 | + |
| 362 | + <div className="h-[500px] overflow-hidden"> |
| 363 | + {/* Data Table */} |
| 364 | + <DataTable table={table} columns={columns.length} pageCount={pageCount} /> |
| 365 | + </div> |
| 366 | + </div> |
| 367 | + ); |
| 368 | +} |
| 369 | + |
255 | 370 | // --- Test Functions --- |
256 | 371 | const testInitialRenderServerSide = testInitialRender('Issues Table (Bazza UI Server Filters via Loader)'); |
257 | 372 | const testPaginationServerSide = testPagination({ serverSide: true }); |
258 | 373 |
|
| 374 | +/** |
| 375 | + * Test scrolling functionality and sticky header |
| 376 | + */ |
| 377 | +const testScrolling = async ({ canvasElement }: StoryContext) => { |
| 378 | + const canvas = within(canvasElement); |
| 379 | + |
| 380 | + // Wait for table to render |
| 381 | + await new Promise((resolve) => setTimeout(resolve, 500)); |
| 382 | + |
| 383 | + // Find the table container |
| 384 | + const tableContainer = canvasElement.querySelector('[class*="rounded-md border"]'); |
| 385 | + expect(tableContainer).toBeInTheDocument(); |
| 386 | + |
| 387 | + // Find the scrollable area (the div inside Table component) |
| 388 | + const scrollableArea = tableContainer?.querySelector('[class*="overflow-auto"]') as HTMLElement | null; |
| 389 | + expect(scrollableArea).toBeInTheDocument(); |
| 390 | + |
| 391 | + // Verify scrollable area exists and has content |
| 392 | + if (!scrollableArea) { |
| 393 | + throw new Error('Scrollable area not found'); |
| 394 | + } |
| 395 | + |
| 396 | + // Get initial scroll position |
| 397 | + const initialScrollTop = scrollableArea.scrollTop; |
| 398 | + expect(initialScrollTop).toBe(0); |
| 399 | + |
| 400 | + // Verify scroll height is greater than client height (content is scrollable) |
| 401 | + const isScrollable = scrollableArea.scrollHeight > scrollableArea.clientHeight; |
| 402 | + expect(isScrollable).toBe(true); |
| 403 | + |
| 404 | + // Find the table header |
| 405 | + const header = canvasElement.querySelector('thead'); |
| 406 | + expect(header).toBeInTheDocument(); |
| 407 | + |
| 408 | + if (!header) { |
| 409 | + throw new Error('Table header not found'); |
| 410 | + } |
| 411 | + |
| 412 | + // Get header position before scrolling |
| 413 | + const headerBeforeScroll = header.getBoundingClientRect(); |
| 414 | + const headerTopBefore = headerBeforeScroll.top; |
| 415 | + |
| 416 | + // Scroll down |
| 417 | + scrollableArea.scrollTop = 200; |
| 418 | + await new Promise((resolve) => setTimeout(resolve, 100)); |
| 419 | + |
| 420 | + // Verify that we scrolled (browser may round the scroll position, so check for reasonable scroll amount) |
| 421 | + expect(scrollableArea.scrollTop).toBeGreaterThan(0); |
| 422 | + expect(scrollableArea.scrollTop).toBeGreaterThan(100); // Verify we scrolled a reasonable amount |
| 423 | + |
| 424 | + // Verify header is still visible and sticky |
| 425 | + const headerAfterScroll = header.getBoundingClientRect(); |
| 426 | + expect(headerAfterScroll).toBeDefined(); |
| 427 | + |
| 428 | + // The header should have sticky positioning |
| 429 | + const headerStyles = window.getComputedStyle(header); |
| 430 | + expect(headerStyles.position).toBe('sticky'); |
| 431 | + expect(headerStyles.top).toBe('0px'); |
| 432 | + |
| 433 | + // Verify header position relative to container hasn't changed (it's sticky) |
| 434 | + const headerTopAfter = headerAfterScroll.top; |
| 435 | + // Header should remain at the top of the scrollable container |
| 436 | + expect(headerTopAfter).toBeGreaterThanOrEqual(headerTopBefore - 10); // Allow small margin for rounding |
| 437 | + |
| 438 | + // Scroll back to top |
| 439 | + scrollableArea.scrollTop = 0; |
| 440 | + await new Promise((resolve) => setTimeout(resolve, 100)); |
| 441 | + |
| 442 | + expect(scrollableArea.scrollTop).toBe(0); |
| 443 | +}; |
| 444 | + |
| 445 | +/** |
| 446 | + * Test initial render for scrolling story |
| 447 | + */ |
| 448 | +const testInitialRenderScrolling = testInitialRender('Data Table with Scrolling and Sticky Header'); |
| 449 | + |
259 | 450 | // --- Story Configuration --- |
260 | 451 | const meta: Meta<typeof DataTableWithBazzaFilters> = { |
261 | 452 | title: 'Data Table/Server Driven Filters', |
@@ -432,3 +623,32 @@ function DataTableWithBazzaFilters() { |
432 | 623 | await testFiltering(context); |
433 | 624 | }, |
434 | 625 | }; |
| 626 | + |
| 627 | +export const WithScrolling: Story = { |
| 628 | + args: {}, |
| 629 | + parameters: { |
| 630 | + docs: { |
| 631 | + description: { |
| 632 | + story: |
| 633 | + 'Demonstrates the data table with vertical scrolling and a sticky header. The table is contained within a fixed-height container (500px) and uses a larger page size (20 rows) to ensure scrolling is needed. The header remains visible while scrolling through table rows.', |
| 634 | + }, |
| 635 | + }, |
| 636 | + }, |
| 637 | + render: () => <DataTableWithScrolling />, |
| 638 | + decorators: [ |
| 639 | + withReactRouterStubDecorator({ |
| 640 | + routes: [ |
| 641 | + { |
| 642 | + path: '/', |
| 643 | + Component: DataTableWithScrolling, |
| 644 | + loader: handleDataFetch, |
| 645 | + }, |
| 646 | + ], |
| 647 | + }), |
| 648 | + ], |
| 649 | + play: async (context) => { |
| 650 | + // Run the tests in sequence |
| 651 | + await testInitialRenderScrolling(context); |
| 652 | + await testScrolling(context); |
| 653 | + }, |
| 654 | +}; |
0 commit comments