CSV source adapter
retab-ui/csv-sourceFreeComponent
Bridges the document-source model to the CSV viewer: csvAnchorToTarget and sourceToCsvCell for csv_cell anchors.
Install it
npx shadcn@latest add https://ui.retab.com/r/csv-source.jsonSource
1"use client";23import * as React from "react";45import type { Source, SourceAnchor } from "@/lib/document-source";6import type {7 CsvCellAddress,8 CsvViewerHandle,9} from "@/components/ui/csv-viewer";1011export interface SourceTarget {12 scrollTo?: (source: Source, options: { behavior: ScrollBehavior }) => void;13}1415/** Spreadsheet column letter → 0-based index ("A" → 0, "C" → 2, "AA" → 26). */16export function columnLetterToIndex(letter: string): number | null {17 if (!/^[A-Za-z]+$/.test(letter)) return null;18 let n = 0;19 for (const ch of letter.toUpperCase()) {20 n = n * 26 + (ch.charCodeAt(0) - 64);21 if (!Number.isSafeInteger(n)) return null;22 }23 return n - 1;24}2526/**27 * A `csv_cell` anchor → 0-based data coordinates. `row` is the 1-based data28 * row (the header is not a data row); `column` is a letter.29 */30export function csvAnchorToTarget(anchor: SourceAnchor): CsvCellAddress | null {31 if (anchor.kind === "csv_cell") {32 const columnIndex = columnLetterToIndex(anchor.column);33 if (34 columnIndex == null ||35 !Number.isInteger(anchor.row) ||36 anchor.row < 137 ) {38 return null;39 }40 return {41 rowIndex: anchor.row - 1,42 columnIndex,43 };44 }45 return null;46}4748/** A stable source target over a `CsvViewer` ref. */49export function useCsvSourceTarget(50 viewerRef: React.RefObject<CsvViewerHandle | null>,51): SourceTarget {52 return React.useMemo<SourceTarget>(53 () => ({54 scrollTo: (source: Source, options) => {55 const target = csvAnchorToTarget(source.anchor);56 if (target) viewerRef.current?.scrollToCell(target, options);57 },58 }),59 [viewerRef],60 );61}6263/**64 * The `activeCell` prop for `CsvViewer` derived from a source.65 */66export function sourceToCsvCell(67 source: Source | undefined,68): CsvCellAddress | null {69 return source ? csvAnchorToTarget(source.anchor) : null;70}
What it pulls in
Other registry items
Files it writes
More from retab-ui
All 130 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| Alertalert | retab-ui | Components | Free | 1 dep | |
| Alert Dialogalert-dialog | retab-ui | Components | Free | 1 dep | |
| Aspect Ratioaspect-ratio | retab-ui | Components | Free | no deps | |
| Attachment Sidebarattachment-sidebar | retab-ui | Components | Free | 1 dep | |
| Autocompleteautocomplete | retab-ui | Components | Free | 2 deps | |
| Calendarcalendar | retab-ui | Components | Free | 2 deps | |
| Code Viewercode-viewer | retab-ui | Components | Free | 2 deps · 32 files | |
| Commandcommand | retab-ui | Components | Free | 2 deps |
