master @ 34 LINES
[ HISTORY ] [ UP ]
┌─ TS ───────────────────────────────────────────────────────────────────────┐
│ // Numbered pager: 1 ... 4 5 [6] 7 8 ... 20. First and last pages always │
│ // show; a sliding window surrounds the current page. │
│ export interface Props { │
│ current: number; │
│ total: number; │
│ href: (n: number) => string; │
│ } │
│ │
│ export type PageItem = │
│ | { kind: "page"; n: number; current: boolean } │
│ | { kind: "gap" }; │
│ │
│ export function paginationItems(current: number, total: number, windowSize = 5): P │
│ ageItem[] { │
│ const cur = Math.max(1, Math.min(total, current)); │
│ if (total < 1) return []; │
│ │
│ const half = Math.floor(windowSize / 2); │
│ let start = Math.max(1, cur - half); │
│ const end = Math.min(total, start + windowSize - 1); │
│ start = Math.max(1, end - windowSize + 1); │
│ │
│ const items: PageItem[] = []; │
│ if (start > 1) { │
│ items.push({ kind: "page", n: 1, current: cur === 1 }); │
│ if (start > 2) items.push({ kind: "gap" }); │
│ } │
│ for (let n = start; n <= end; n++) items.push({ kind: "page", n, current: n === │
│ cur }); │
│ if (end < total) { │
│ if (end < total - 1) items.push({ kind: "gap" }); │
│ items.push({ kind: "page", n: total, current: cur === total }); │
│ } │
│ return items; │
│ } │
└────────────────────────────────────────────────────────────────────────────────────┘
┌─ TS ─────────────────────────────────┐
│ // Numbered pager: 1 ... 4 5 [6] 7 8 ... 20. │
│ First and last pages always │
│ // show; a sliding window surrounds the curr │
│ ent page. │
│ export interface Props { │
│ current: number; │
│ total: number; │
│ href: (n: number) => string; │
│ } │
│ │
│ export type PageItem = │
│ | { kind: "page"; n: number; current: bool │
│ ean } │
│ | { kind: "gap" }; │
│ │
│ export function paginationItems(current: num │
│ ber, total: number, windowSize = 5): PageIte │
│ m[] { │
│ const cur = Math.max(1, Math.min(total, cu │
│ rrent)); │
│ if (total < 1) return []; │
│ │
│ const half = Math.floor(windowSize / 2); │
│ let start = Math.max(1, cur - half); │
│ const end = Math.min(total, start + window │
│ Size - 1); │
│ start = Math.max(1, end - windowSize + 1); │
│ │
│ const items: PageItem[] = []; │
│ if (start > 1) { │
│ items.push({ kind: "page", n: 1, current │
│ : cur === 1 }); │
│ if (start > 2) items.push({ kind: "gap" │
│ }); │
│ } │
│ for (let n = start; n <= end; n++) items.p │
│ ush({ kind: "page", n, current: n === cur }) │
│ ; │
│ if (end < total) { │
│ if (end < total - 1) items.push({ kind: │
│ "gap" }); │
│ items.push({ kind: "page", n: total, cur │
│ rent: cur === total }); │
│ } │
│ return items; │
│ } │
└──────────────────────────────────────────────┘
──────────────────────────────────────────────────────────────────────────────────────
OOKNET
────────────────────────────────────────────────
OOKNET