# All files / src/components/ui virtualized-grid.tsx

0% Statements 0/9
0% Branches 0/8
0% Functions 0/7
0% Lines 0/9

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149

     import React from 'react';

    interface VirtualizedGridProps<T> {
      items: T[];
      renderItem: (item: T, index: number) => React.ReactNode;
      columnCount?: number;
      rowHeight?: number;
      columnWidth?: number;
      gap?: number;
      className?: string;
    }

    /**
     * VirtualizedGrid Component
     *
     * Renders a grid of items.
     *
     * @example
     * ```tsx
     * <VirtualizedGrid
     *   items={movies}
     *   renderItem={(movie) => <MovieCard movie={movie} />}
     *   columnCount={6}
     *   rowHeight={300}
     * />
     * ```
     */
    export function VirtualizedGrid<T>({
      items,
      renderItem,
      columnCount = 6,
      gap = 16,
      className = ''}: VirtualizedGridProps<T>) {
      return (
        <div className={className} style={{ width: '100%' }}>
          <div
            style={{
              display: 'grid',
              gridTemplateColumns: `repeat(${columnCount}, 1fr)`,
              gap: `${gap}px`}}
          >
            {items.map((item, index) => (
              <div key={index}>
                {renderItem(item, index)}
              </div>
            ))}
          </div>
        </div>
      );
    }

    interface VirtualizedListProps<T> {
      items: T[];
      renderItem: (item: T, index: number) => React.ReactNode;
      itemHeight?: number;
      className?: string;
    }

    /**
     * VirtualizedList Component
     *
     * Renders a list of items.
     *
     * @example
     * ```tsx
     * <VirtualizedList
     *   items={users}
     *   renderItem={(user) => <UserRow user={user} />}
     *   itemHeight={60}
     * />
     * ```
     */
    export function VirtualizedList<T>({
      items,
      renderItem,
      className = ''}: VirtualizedListProps<T>) {
      return (
        <div className={className} style={{ width: '100%' }}>
          <div style={{ display: 'flex', flexDirection: 'column' }}>
            {items.map((item, index) => (
              <div key={index}>
                {renderItem(item, index)}
              </div>
            ))}
          </div>
        </div>
      );
    }

    interface ResponsiveVirtualizedGridProps<T> {
      items: T[];
      renderItem: (item: T, index: number) => React.ReactNode;
      minColumnWidth?: number;
      rowHeight?: number;
      gap?: number;
      className?: string;
    }

    /**
     * ResponsiveVirtualizedGrid Component
     *
     * Renders a grid of items with responsive column count.
     *
     * @example
     * ```tsx
     * <ResponsiveVirtualizedGrid
     *   items={movies}
     *   renderItem={(movie) => <MovieCard movie={movie} />}
     *   minColumnWidth={200}
     *   rowHeight={300}
     * />
     * ```
     */
    export function ResponsiveVirtualizedGrid<T>({
      items,
      renderItem,
      minColumnWidth = 200,
      gap = 16,
      className = ''}: ResponsiveVirtualizedGridProps<T>) {
      return (
        <div className={className} style={{ width: '100%' }}>
          <div
            style={{
              display: 'grid',
              gridTemplateColumns: `repeat(auto-fill, minmax(${minColumnWidth}px, 1fr))`,
              gap: `${gap}px`}}
          >
            {items.map((item, index) => (
              <div key={index}>
                {renderItem(item, index)}
              </div>
            ))}
          </div>
        </div>
      );
    }

    /**
     * Hook to determine if virtualization should be used
     *
     * @param itemCount - Number of items in the list
     * @param threshold - Minimum number of items to enable virtualization (default: 100)
     * @returns boolean indicating if virtualization should be used
     */
    export function useVirtualization(itemCount: number, threshold: number = 100): boolean {
      return itemCount >= threshold;
    }