Guest User

Untitled

a guest
May 15th, 2023
27
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.45 KB | None | 0 0
  1.  
  2. ```
  3. <script lang="ts">
  4. let rowVirtualizer: ReturnType<typeof createVirtualizer>;
  5. let scrollElement: Element;
  6. onMount(() => {
  7. rowVirtualizer = createVirtualizer({
  8. count: candidates.length,
  9. getScrollElement: () => scrollElement,
  10. estimateSize: () => 36,
  11. getItemKey: (index: number) => candidates[index].id,
  12. overscan: 10,
  13. debug: dev
  14. });
  15. });
  16.  
  17. /**
  18. * Get rows currently in the viewport (+ overscan).
  19. */
  20. $: virtualRows = $rowVirtualizer?.getVirtualItems() ?? [];
  21.  
  22. /**
  23. * Compute the paddingTop that ensures that the table has the correct fake size
  24. * to render the proper scrollbar size.
  25. */
  26. $: paddingTop = virtualRows.length > 0 ? virtualRows?.[0]?.start || 0 : 0;
  27. $: paddingBottom =
  28. virtualRows.length > 0
  29. ? $rowVirtualizer.getTotalSize() - (virtualRows?.[virtualRows.length - 1]?.end || 0)
  30. : 0;
  31. </script>
  32.  
  33.  
  34. <div bind:this={scrollElement} class="root">
  35. <Table style="height: {$rowVirtualizer?.getTotalSize()}px">
  36. <TableHead>
  37. ...
  38. </TableHead>
  39.  
  40. <tbody>
  41. {#if $rowVirtualizer}
  42.  
  43. {#if paddingTop > 0}
  44. <tr>
  45. <td style:height="${paddingTop}px" />
  46. </tr>
  47. {/if}
  48.  
  49. {#each virtualRows as virtualRow}
  50. {@const row = rows[virtualRow.index]}
  51.  
  52. <TableRow data-testid={`candidate-row-${row.getValue('email')}`}>
  53. {#each row.getVisibleCells() as cell, index}
  54. <TableCell
  55. data-testid={cell.column.columnDef.id}
  56. focusable
  57. variant={cell.column.columnDef.meta?.variant}
  58. selected={isSamePosition($activeCellStore.position, { row: row.index, col: index })}
  59. on:click={() => activeCellStore.select({ row: row.index, col: index })}
  60. on:dblclick={() => activeCellStore.setMode('edit')}
  61. >
  62. <svelte:component
  63. this={flexRender(cell.column.columnDef.cell, {
  64. ...cell.getContext(),
  65. position: { row: row.index, col: index }
  66. })}
  67. />
  68. </TableCell>
  69. {/each}
  70. <TableCell />
  71. </TableRow>
  72. {/each}
  73.  
  74. {#if paddingBottom > 0}
  75. <tr>
  76. <td style:height="{paddingBottom}px" />
  77. </tr>
  78. {/if}
  79. {/if}
  80. </tbody>
  81. </Table>
  82. </div>
  83.  
  84. <style>
  85. .root {
  86. flex: 1;
  87. overflow: auto;
  88. scroll-padding-block-start: var(--spacing-11);
  89. }
  90. </style>
  91. ```
Advertisement
Add Comment
Please, Sign In to add comment