All files / core/blong-browser/src/widgets/table paginator.tsx

50% Statements 6/12
100% Branches 10/10
28.57% Functions 2/7
50% Lines 6/12

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                              9x                                           5x 5x 5x 5x                                                                                                                         20x                      
/**
 * The paginator a list table shows under its rows.
 *
 * Paging is the caller's, not the widget's: the component says how many rows there
 * are and where the page starts, and this asks for another page. Kept on its own so
 * the table's own render reads as rows and columns rather than five arrow buttons.
 *
 * The total is optional. A read that reports it (the generic `find` does when the
 * caller asks for a count) gives the "page / pages" answer and a last-page jump; a
 * read that answers a bare page leaves both out, and the forward arrows follow
 * `hasMore` — a full page may have more behind it.
 */
import {Button} from '../../components/Button/Button.js';
 
/** The page sizes a reader may pick. */
const PAGE_SIZES = [10, 25, 50, 100];
 
export function TablePaginator({
    first,
    pageSize,
    total,
    hasMore,
    onFirstChange,
    onPageSizeChange,
}: {
    /** The index of the first row on screen. */
    first: number;
    pageSize: number;
    /** How many rows the whole result holds, when the read reported one. */
    total?: number;
    /** Whether a further page exists, when the total is unknown. */
    hasMore: boolean;
    onFirstChange: (first: number) => void;
    onPageSizeChange: (pageSize: number) => void;
}) {
    // A last page is only addressable when the total is known: `hasMore` says
    // there is one more page, not which index it starts at.
    const pageCount = total === undefined ? undefined : Math.max(1, Math.ceil(total / pageSize));
    const lastPageFirst = pageCount === undefined ? first : (pageCount - 1) * pageSize;
    const atEnd = pageCount === undefined ? !hasMore : first + pageSize >= total!;
    return (
        <div
            className="blong-table-paginator"
            style={{
                display: 'flex',
                alignItems: 'center',
                gap: '0.25rem',
                padding: '0.5rem',
                justifyContent: 'center',
                userSelect: 'none',
                flexShrink: 0,
            }}
        >
            <Button
                icon="pi pi-angle-double-left"
                className="p-button-text p-button-sm"
                type="button"
                disabled={first === 0}
                onClick={() => onFirstChange(0)}
            />
            <Button
                icon="pi pi-angle-left"
                className="p-button-text p-button-sm"
                type="button"
                disabled={first === 0}
                onClick={() => onFirstChange(Math.max(0, first - pageSize))}
            />
            <span style={{padding: '0 0.5rem', fontSize: '0.875rem'}}>
                {Math.floor(first / pageSize) + 1}
                {pageCount === undefined ? '' : ` / ${pageCount}`}
            </span>
            <Button
                icon="pi pi-angle-right"
                className="p-button-text p-button-sm"
                type="button"
                disabled={atEnd}
                onClick={() => onFirstChange(first + pageSize)}
            />
            <Button
                icon="pi pi-angle-double-right"
                className="p-button-text p-button-sm"
                type="button"
                disabled={pageCount === undefined || atEnd}
                onClick={() => onFirstChange(lastPageFirst)}
            />
            <select
                value={pageSize}
                onChange={e => {
                    onPageSizeChange(Number(e.target.value));
                    onFirstChange(0);
                }}
                style={{
                    marginLeft: '0.5rem',
                    background: 'var(--surface-overlay)',
                    color: 'var(--text-color)',
                    border: '1px solid var(--surface-border)',
                    borderRadius: 'var(--border-radius)',
                    padding: '0.2rem',
                }}
            >
                {PAGE_SIZES.map(n => (
                    <option
                        key={n}
                        value={n}
                    >
                        {n}
                    </option>
                ))}
            </select>
        </div>
    );
}