{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "tw-data-table-grid",
  "type": "registry:ui",
  "title": "react-tw/data-table-grid.tsx",
  "description": "The look of the WAI-ARIA APG editable grid of the React DataTable, as `<ui-data-table>`'s; the Copied from @purpurabog/ui 2.3.0; the package is the maintained source, this copy is yours to change. Icons from the Carbon Design System (Apache-2.0, © IBM Corp.; see THIRD-PARTY-NOTICES.md in the package).",
  "dependencies": [
    "@purpurabog/ui@^2.3.0"
  ],
  "registryDependencies": [
    "@kanok-ui/core-grid",
    "@kanok-ui/tw-theme",
    "@kanok-ui/tw-utils"
  ],
  "files": [
    {
      "path": "src/react-tw/data-table-grid.tsx",
      "type": "registry:ui",
      "target": "components/ui/react-tw/data-table-grid.tsx",
      "content": "import type { ReactNode } from 'react';\nimport { cellDraft } from '../core/grid.ts';\nimport { DataTable as DataTablePrimitive } from '@purpurabog/ui/headless';\nimport { cn } from './lib/utils.ts';\n\n/**\n * The look of the WAI-ARIA APG editable grid of the React DataTable, as `<ui-data-table>`'s; the\n * behaviour is the headless DataTable's (`useDataTable`'s grid, `DataTable.Cell` / `CellInput` /\n * `CellSelect`): once a header sets `editable` the table is `role=grid` with one Tab stop among the\n * body cells (arrows, Home / End, Ctrl+Home / End move it), controls inside cells leave the Tab order\n * and Enter / Space on their cell activates them. On an editable cell (pencil after the value on\n * hover / focus) Enter, F2, a double click or typing opens the editor; Enter saves, Escape cancels,\n * Tab / Shift+Tab save and edit the next / previous editable cell, leaving the cell saves; `validate`\n * keeps it open with its message. A save calls `onCellEdit` with the new `rows` for the app to set.\n */\nexport type DataTableEditOption = DataTablePrimitive.DataTableEditOption;\n\n/** What `onCellEdit` gets: the saved value and `rows` with it (set them, or keep yours to refuse it). */\nexport type DataTableCellEdit<R> = DataTablePrimitive.DataTableCellEdit<R>;\n\nconst chevronDown = <path d=\"M8 11 3 6l.7-.7L8 9.6l4.3-4.3.7.7z\" />;\nconst pencil = (\n    <path d=\"M2 13h12v1H2zm10.7-8.3L11.3 3.3a1 1 0 0 0-1.4 0L4 9.2V12h2.8l5.9-5.9a1 1 0 0 0 0-1.4ZM6.4 11H5V9.6l4-4L10.4 7Zm4.7-4.7L9.7 4.9l1-1 1.4 1.4Z\" />\n);\n\nconst editorClasses = cn(\n    'm-0 box-border block h-full min-h-[calc(var(--_row,48px)-2px)] w-full rounded-none border-0 bg-surface-1 px-4 text-ink type-body-sm [font-size:var(--ui-field-font-size,14px)]! [text-align:inherit]',\n    'outline-[length:var(--ui-focus-width,2px)] outline-solid -outline-offset-2 outline-focus aria-invalid:outline-error',\n);\n\n/** In a grid every body cell can be the Tab stop: the focus ring of <ui-data-table>'s editable grid. */\nexport const gridFocusRing = 'focus-visible:outline-[length:var(--ui-focus-width,2px)] focus-visible:outline-solid focus-visible:-outline-offset-2 focus-visible:outline-focus';\n\n/** The cell being edited: a text / number box or a select, with its error below. */\nfunction CellEditor({ column, label }: Readonly<{ column: DataTablePrimitive.DataTableColumn; label: string }>) {\n    if (column.editable === 'select') {\n        return (\n            <>\n                <DataTablePrimitive.CellSelect aria-label={label} className={cn(editorClasses, 'cursor-pointer appearance-none pe-12')} />\n                <svg viewBox=\"0 0 16 16\" aria-hidden=\"true\" className=\"pointer-events-none absolute end-4 top-[calc(50%-8px)] size-4 fill-ink forced-colors:fill-[CanvasText]\">\n                    {chevronDown}\n                </svg>\n            </>\n        );\n    }\n    // the browser's 1px above and below a text box, as <ui-data-table>'s editor keeps it\n    return <DataTablePrimitive.CellInput aria-label={label} className={cn(editorClasses, 'py-px')} />;\n}\n\nexport interface GridDataCellProps {\n    rowId: string;\n    /** The cell's value (an editable select column's own shows its option's label). */\n    value: unknown;\n    column: DataTablePrimitive.DataTableColumn;\n    /** The cell's place in its row. */\n    index: number;\n    className: string;\n    /** The cell's content when not editing. */\n    content: () => ReactNode;\n    /** \", Lease\": names the editor after the row. */\n    rowName: string;\n}\n\n/** A data cell (inside `DataTable.Row`): the editor while editing, else the value (and a pencil when editable). */\nexport function GridDataCell({ rowId, value, column, index, className, content, rowName }: Readonly<GridDataCellProps>) {\n    const table = DataTablePrimitive.useDataTableContext();\n    const { grid } = table;\n    const focusRing = grid.enabled && gridFocusRing;\n    if (!column.editable) {\n        return (\n            <DataTablePrimitive.Cell index={index} column={column.key} className={cn(className, focusRing)}>\n                {content()}\n            </DataTablePrimitive.Cell>\n        );\n    }\n    const editable = cn(className, focusRing, 'group/cell relative cursor-text');\n    if (grid.editingAt(rowId, column.key)) {\n        return (\n            <DataTablePrimitive.Cell index={index} column={column.key} className={cn(editable, 'p-0')}>\n                <CellEditor column={column} label={column.header + rowName} />\n                <DataTablePrimitive.CellError className=\"absolute start-0 top-full z-1 whitespace-nowrap bg-surface-1 px-2 py-1 text-error-text shadow-[0_2px_6px_rgb(0_0_0/0.3)] type-caption\" />\n            </DataTablePrimitive.Cell>\n        );\n    }\n    const shown = column.editable === 'select' && column.options ? (column.options.find((o) => o.value === cellDraft(value))?.label ?? content()) : content();\n    return (\n        <DataTablePrimitive.Cell index={index} column={column.key} className={editable}>\n            <DataTablePrimitive.CellValue>{shown}</DataTablePrimitive.CellValue>\n            <svg\n                data-edit-icon=\"\"\n                viewBox=\"0 0 16 16\"\n                aria-hidden=\"true\"\n                className=\"ms-2 inline size-4 cursor-pointer align-text-bottom fill-ink-muted opacity-0 group-hover/cell:opacity-100 group-focus/cell:opacity-100 forced-colors:fill-[CanvasText]\"\n            >\n                {pencil}\n            </svg>\n        </DataTablePrimitive.Cell>\n    );\n}\n"
    }
  ]
}
