ui
ธีม รูปแบบ CarbonKanok ภาษา English
ภาพรวมติดตั้งการย้ายไป 1.0ธีมและ layerดีไซน์ Kanokปรับแต่งหน้าตาไอคอนฟอร์มและการตรวจค่าใช้กับ frameworkใช้กับ AIButtonText inputText areaNumber inputSearchSelectDropdownComboboxMulti-selectCheckboxRadio buttonToggleDate pickerTime pickerSliderFile uploaderModalSide panelTearsheetNotificationToastTooltipDefinition tooltipPopoverToggletipMenuData tableStructured listPaginationContained listTree viewTabsBreadcrumbLinkPagination navContent switcherAccordionProgress indicatorUI shellTagTileCardProduct cardCode snippetCopy buttonUser avatarTruncated textListLoadingInline loadingProgress barSkeletonBadge indicatorStatus indicatorGridHeadingLayerAction setPage headerBig numberEdit in placeแนะนำText inputText areaNumber inputSearchSelectDropdownComboboxMulti-selectCheckboxRadio buttonToggleDate pickerTime pickerSliderFile uploaderTabsPagination navContent switcherTooltipDefinition tooltipPopoverMenuAccordionProgress indicatorUI shellModalSide panelTearsheetNotificationToastData tableStructured listPaginationTree viewCode snippetCopy buttonTruncated textLoadingTagCardProduct cardEdit in place

Headless Data table

DataTable คือพฤติกรรมของ Data table แบบมี style โดยไม่มี class เลย ในแบบ TanStack Table: hook useDataTable(options) เก็บ state ทั้งหมด คือการเรียง การค้นหา หน้า การเลือก แถวที่เปิด คอลัมน์ที่ซ่อน ความกว้างคอลัมน์ และ grid ที่แก้ไขได้ แล้วคืนแถวที่จะแสดง action และ prop getter ให้ คุณเขียน <table> เอง ส่วน parts ใส่กฎคีย์บอร์ด focus และ ARIA ในจุดที่ต้องใช้

ส่วนที่คุณเขียนเอง: ทุกเซลล์ หัวตาราง toolbar ไอคอน ข้อความตอนไม่มีข้อมูล และ style ทั้งหมด สถานะแสดงเป็น attribute data-* ที่ parts

ติดตั้ง

ส่วน headless มากับแพ็กเกจเลย (ไม่มี item ของ shadcn; item ของ React + Tailwind สร้างบนมัน):

npm install @purpurabog/ui import { DataTable } from '@purpurabog/ui/headless';

วิธีใช้

import { DataTable } from '@purpurabog/ui/headless'; interface Contract { id: string; name: string; party: string; status: string; [key: string]: unknown; } const headers = [ { key: 'name', header: 'สัญญา' }, { key: 'party', header: 'คู่สัญญา' }, { key: 'status', header: 'สถานะ' }, ]; const rows: Contract[] = [ { id: '1', name: 'สัญญาบริการ', party: 'บริษัท เอซีเอ็ม จำกัด', status: 'ลงนามแล้ว' }, { id: '2', name: 'สัญญารักษาความลับ', party: 'โกลเบ็กซ์', status: 'ฉบับร่าง' }, { id: '3', name: 'ต่อสัญญาเช่า', party: 'อินิเทค', status: 'กำลังตรวจ' }, ]; const arrows = { ascending: '↑', descending: '↓', none: '↕' } as const; const head = 'h-12 bg-layer-accent text-start text-sm font-semibold text-ink'; const cell = 'h-12 border-b border-hairline px-4 text-sm text-ink-muted'; export default function Example() { const table = DataTable.useDataTable({ headers, rows, sortable: true, selectable: true, locale: 'th' }); return ( <DataTable.Root table={table}> <DataTable.Table aria-label="สัญญา" className="w-full border-collapse"> <thead> <tr> <td className={`${head} w-12 ps-4`}> <DataTable.SelectAll aria-label="เลือกทุกแถว" /> </td> {table.columns.map((column) => ( <DataTable.ColumnHeader key={column.key} column={column.key} className={head}> <DataTable.SortButton className="flex h-12 w-full items-center justify-between px-4 font-semibold hover:bg-layer-selected-hover focus-visible:outline-2 focus-visible:-outline-offset-2 focus-visible:outline-focus"> {column.header} <span aria-hidden="true">{arrows[table.sortDirectionOf(column.key)]}</span> </DataTable.SortButton> </DataTable.ColumnHeader> ))} </tr> </thead> <DataTable.Body> {table.pageRows.map((row) => ( <DataTable.Row key={row.id} rowId={row.id} className="hover:bg-ghost-hover data-selected:bg-layer-selected-1 data-selected:text-ink"> <DataTable.Cell index={0} className={cell}> <DataTable.SelectRow aria-label={`เลือกแถว, ${row.name}`} /> </DataTable.Cell> {table.columns.map((column, i) => ( <DataTable.Cell key={column.key} index={i + 1} column={column.key} className={cell}> {String(row[column.key])} </DataTable.Cell> ))} </DataTable.Row> ))} </DataTable.Body> </DataTable.Table> <p className="mt-2 text-xs text-ink-muted">ที่เลือก: {table.selectedIds.join(', ') || '(ไม่มี)'}</p> </DataTable.Root> ); }

โครงสร้าง

const table = DataTable.useDataTable({ headers, rows }); <DataTable.Root table={table}> <DataTable.EditHint /> <DataTable.Table> <thead> <tr> <td><DataTable.ExpandAll /></td> <td><DataTable.SelectAll /></td> <DataTable.ColumnHeader column="…"> <DataTable.SortButton /> <DataTable.ResizeHandle /> </DataTable.ColumnHeader> <DataTable.Filler /> </tr> </thead> <DataTable.Body> <DataTable.Row rowId="…"> <DataTable.Cell index={0}><DataTable.ExpandToggle /></DataTable.Cell> <DataTable.Cell index={1}><DataTable.SelectRow /></DataTable.Cell> <DataTable.Cell index={2} column="…"> <DataTable.CellValue /> <DataTable.CellInput /> or <DataTable.CellSelect /> <DataTable.CellError /> </DataTable.Cell> <DataTable.Filler /> </DataTable.Row> <DataTable.ExpandedRow rowId="…" /> </DataTable.Body> </DataTable.Table> </DataTable.Root>

ใช้เฉพาะ part ที่ต้องการ ตารางที่อ่านอย่างเดียวใช้แค่ Root, Table, Body, Row และ Cell หรือแม้แต่ <tr> และ <td> ธรรมดาครอบ table.pageRows

ค้นหาและแบ่งหน้า

table.setFilterText(text) กรองแถว (เซลล์ใดที่แสดงอยู่มีข้อความนั้น ไม่สนตัวพิมพ์เล็กใหญ่) และกลับไปหน้า 1 เมื่อมี pageSize (หรือ defaultPageSize) table.pageRows จะเป็นหนึ่งหน้า ควบคุมด้วย Pagination แบบ headless หรือปุ่มของคุณเองผ่าน table.setPagination

import { DataTable, Pagination } from '@purpurabog/ui/headless'; interface Contract { id: string; name: string; party: string; [key: string]: unknown; } const parties = ['เอซีเอ็ม', 'โกลเบ็กซ์', 'อินิเทค', 'อัมเบรลลา']; const headers = [ { key: 'name', header: 'สัญญา' }, { key: 'party', header: 'คู่สัญญา' }, ]; const rows: Contract[] = Array.from({ length: 23 }, (_, i) => ({ id: `c${i + 1}`, name: `สัญญาเลขที่ ${String(i + 1).padStart(3, '0')}`, party: parties[i % 4] })); const cell = 'h-10 border-b border-hairline px-4 text-sm text-ink-muted'; const arrow = 'size-8 text-ink hover:bg-ghost-hover focus-visible:outline-2 focus-visible:-outline-offset-2 focus-visible:outline-focus data-disabled:text-ink-disabled data-disabled:hover:bg-transparent'; export default function Example() { const table = DataTable.useDataTable({ headers, rows, defaultPageSize: 5 }); return ( <DataTable.Root table={table}> <div className="grid max-w-md gap-2"> <input type="search" aria-label="กรองสัญญา" placeholder="กรองสัญญา" value={table.filterText} onChange={(e) => table.setFilterText(e.target.value)} className="h-10 border-b border-border-strong-1 bg-field-1 px-4 text-sm text-ink placeholder:text-ink-placeholder focus-visible:outline-2 focus-visible:-outline-offset-2 focus-visible:outline-focus" /> <DataTable.Table aria-label="Contracts" className="w-full border-collapse"> <thead> <tr> {table.columns.map((column) => ( <DataTable.ColumnHeader key={column.key} column={column.key} className="h-10 bg-layer-accent px-4 text-start text-sm font-semibold text-ink"> {column.header} </DataTable.ColumnHeader> ))} </tr> </thead> <DataTable.Body> {table.pageRows.map((row) => ( <DataTable.Row key={row.id} rowId={row.id}> <DataTable.Cell index={0} className={cell}>{row.name}</DataTable.Cell> <DataTable.Cell index={1} className={cell}>{row.party}</DataTable.Cell> </DataTable.Row> ))} </DataTable.Body> </DataTable.Table> {table.filteredRows.length === 0 ? <p className="text-sm text-ink-muted">ไม่พบข้อมูลที่ค้นหา</p> : null} <Pagination.Root page={table.page} pageSize={table.pageSize} totalItems={table.filteredRows.length} onChange={table.setPagination} className="flex items-center gap-2 text-sm text-ink"> <span className="me-auto" role="status"> แสดง {table.filteredRows.length} จาก {table.rows.length} แถว · หน้า {table.page} จาก {table.pageCount} </span> <Pagination.Prev aria-label="หน้าก่อนหน้า" className={arrow}>‹</Pagination.Prev> <Pagination.Next aria-label="หน้าถัดไป" className={arrow}>›</Pagination.Next> </Pagination.Root> </div> </DataTable.Root> ); }

filterRows(row, text, headers) ใช้แทนการตรวจเดิม และ manualFiltering คงทุกแถวไว้ขณะคุณกรองบน server filterText / onFilterChange และ page / onPageChange ทำให้ควบคุมทั้งสองอย่างเองได้

แถวที่ขยายได้

เมื่อใส่ expandable ปุ่ม ExpandToggle ของแต่ละแถวจะเปิดแถวนั้น ให้ render ExpandedRow ใต้ทุกแถวที่เปิดอยู่ (table.isExpanded(id)) ExpandAll เปิดทุกแถวที่แสดง หรือปิดทั้งหมดเมื่อเปิดครบแล้ว ปุ่มเหล่านี้มี aria-expanded และ aria-controls ที่ชี้ไปยังแถวที่เปิด

import { DataTable } from '@purpurabog/ui/headless'; interface Contract { id: string; name: string; party: string; owner: string; [key: string]: unknown; } const headers = [ { key: 'name', header: 'สัญญา' }, { key: 'party', header: 'คู่สัญญา' }, ]; const rows: Contract[] = [ { id: '1', name: 'สัญญาบริการ', party: 'บริษัท เอซีเอ็ม จำกัด', owner: 'กัญญา ศ.' }, { id: '2', name: 'สัญญารักษาความลับ', party: 'โกลเบ็กซ์', owner: 'อนันต์ พ.' }, { id: '3', name: 'ต่อสัญญาเช่า', party: 'อินิเทค', owner: 'มาลี ก.' }, ]; const toggle = 'flex size-8 items-center justify-center text-ink hover:bg-ghost-hover focus-visible:outline-2 focus-visible:-outline-offset-2 focus-visible:outline-focus [&>svg]:transition-transform data-expanded:[&>svg]:rotate-90'; const chevron = ( <svg viewBox="0 0 16 16" aria-hidden="true" className="size-4 fill-current"> <path d="M11 8 6 13l-.7-.7L9.6 8 5.3 3.7 6 3z" /> </svg> ); const cell = 'h-12 border-b border-hairline px-4 text-sm text-ink-muted'; export default function Example() { const table = DataTable.useDataTable({ headers, rows, expandable: true, defaultExpandedIds: ['2'] }); return ( <DataTable.Root table={table}> <DataTable.Table aria-label="สัญญา" className="w-full border-collapse"> <thead> <tr className="bg-layer-accent text-start text-sm font-semibold text-ink"> <td className="w-12 ps-2"> <DataTable.ExpandAll aria-label={table.allExpanded ? 'ยุบทุกแถว' : 'ขยายทุกแถว'} className={toggle}>{chevron}</DataTable.ExpandAll> </td> {table.columns.map((column) => ( <DataTable.ColumnHeader key={column.key} column={column.key} className="h-12 px-4 text-start"> {column.header} </DataTable.ColumnHeader> ))} </tr> </thead> <DataTable.Body> {table.pageRows.flatMap((row) => { const parent = ( <DataTable.Row key={row.id} rowId={row.id} className="data-expanded:[&>td]:border-transparent"> <DataTable.Cell index={0} className={`${cell} ps-2`}> <DataTable.ExpandToggle aria-label={`ขยายแถว, ${row.name}`} className={toggle}>{chevron}</DataTable.ExpandToggle> </DataTable.Cell> <DataTable.Cell index={1} className={cell}>{row.name}</DataTable.Cell> <DataTable.Cell index={2} className={cell}>{row.party}</DataTable.Cell> </DataTable.Row> ); if (!table.isExpanded(row.id)) return [parent]; return [ parent, <DataTable.ExpandedRow key={`${row.id}-detail`} rowId={row.id}> <td colSpan={3} className="border-b border-hairline px-14 pb-6 pt-2 text-sm text-ink-muted"> ผู้รับผิดชอบ: {row.owner} </td> </DataTable.ExpandedRow>, ]; })} </DataTable.Body> </DataTable.Table> </DataTable.Root> ); }

คืนแถวเป็นรายการชั้นเดียว (อย่างที่นี่ใช้ flatMap) การเปิดแถวจะได้ไม่ mount แถวนั้นใหม่

เลือกหนึ่งแถว

เมื่อใส่ radio ตัว SelectRow จะเป็น radio ทุก radio หยุด Tab ที่เดียว (แถวที่เลือก ถ้าไม่มีก็แถวแรกที่ใช้งานได้) ปุ่มลูกศรย้ายไปแถวถัดไปที่ใช้งานได้และเลือก วนกลับเมื่อสุดปลาย SelectAll ไม่ render อะไร แถวที่มี disabled: true เลือกไม่ได้

import { DataTable } from '@purpurabog/ui/headless'; interface Template { id: string; name: string; type: string; disabled?: boolean; [key: string]: unknown; } const headers = [ { key: 'name', header: 'แม่แบบ' }, { key: 'type', header: 'ประเภท' }, ]; const rows: Template[] = [ { id: 'nda', name: 'NDA มาตรฐาน', type: 'รักษาความลับ' }, { id: 'lease', name: 'สัญญาเช่าสำนักงาน', type: 'อสังหาริมทรัพย์' }, { id: 'msa', name: 'สัญญาบริการหลัก (กำลังปรับปรุง)', type: 'บริการ', disabled: true }, ]; const cell = 'h-12 border-b border-hairline px-4 text-sm text-ink-muted'; export default function Example() { const table = DataTable.useDataTable({ headers, rows, radio: true }); return ( <DataTable.Root table={table}> <DataTable.Table aria-label="เลือกแม่แบบ" className="w-full border-collapse"> <thead> <tr className="h-12 bg-layer-accent text-sm font-semibold text-ink"> <th className="w-12"> <span className="sr-only">เลือก</span> </th> {table.columns.map((column) => ( <DataTable.ColumnHeader key={column.key} column={column.key} className="px-4 text-start"> {column.header} </DataTable.ColumnHeader> ))} </tr> </thead> <DataTable.Body> {table.pageRows.map((row) => ( <DataTable.Row key={row.id} rowId={row.id} className="data-selected:bg-layer-selected-1 data-selected:[&>td]:text-ink"> <DataTable.Cell index={0} className={`${cell} ps-4`}> <DataTable.SelectRow aria-label={`เลือกแถว, ${row.name}`} className="accent-ink" /> </DataTable.Cell> <DataTable.Cell index={1} className={cell}>{row.name}</DataTable.Cell> <DataTable.Cell index={2} className={cell}>{row.type}</DataTable.Cell> </DataTable.Row> ))} </DataTable.Body> </DataTable.Table> <p className="mt-2 text-xs text-ink-muted">แม่แบบ: {table.selectedIds[0] ?? '(ไม่มี)'}</p> </DataTable.Root> ); }

แสดงและซ่อนคอลัมน์

table.columns คือคอลัมน์ที่แสดง table.headers คือทั้งหมด toggleColumn(key, show) แสดงหรือซ่อนหนึ่งคอลัมน์ isColumnLocked(key) บอกว่าซ่อนไม่ได้ (hideable: false หรือเหลือคอลัมน์เดียว) และ resetColumns() กลับไปตาม hidden ของ header columnsChanged บอกว่าที่เลือกต่างจากค่านั้น

import { DataTable } from '@purpurabog/ui/headless'; interface Contract { id: string; name: string; party: string; status: string; updated: string; [key: string]: unknown; } const headers = [ { key: 'name', header: 'สัญญา', hideable: false }, { key: 'party', header: 'คู่สัญญา' }, { key: 'status', header: 'สถานะ' }, { key: 'updated', header: 'แก้ไขล่าสุด', hidden: true }, ]; const rows: Contract[] = [ { id: '1', name: 'สัญญาบริการ', party: 'บริษัท เอซีเอ็ม จำกัด', status: 'ลงนามแล้ว', updated: '2026-09-30' }, { id: '2', name: 'สัญญารักษาความลับ', party: 'โกลเบ็กซ์', status: 'ฉบับร่าง', updated: '2026-10-01' }, ]; const cell = 'h-10 border-b border-hairline px-4 text-sm text-ink-muted'; export default function Example() { const table = DataTable.useDataTable({ headers, rows }); return ( <DataTable.Root table={table}> <fieldset className="mb-3 flex flex-wrap items-center gap-4 text-sm text-ink"> <legend className="mb-1 text-xs text-ink-muted">คอลัมน์</legend> {table.headers.map((column) => ( <label key={column.key} className="flex items-center gap-2 has-disabled:text-ink-disabled"> <input type="checkbox" checked={!table.hiddenColumns.includes(column.key)} disabled={table.isColumnLocked(column.key)} onChange={(e) => table.toggleColumn(column.key, e.target.checked)} /> {column.header} </label> ))} <button type="button" disabled={!table.columnsChanged} onClick={table.resetColumns} className="h-8 px-3 text-link hover:bg-ghost-hover disabled:text-ink-disabled focus-visible:outline-2 focus-visible:outline-focus"> รีเซ็ต </button> </fieldset> <DataTable.Table aria-label="สัญญา" className="w-full border-collapse"> <thead> <tr className="h-10 bg-layer-accent text-sm font-semibold text-ink"> {table.columns.map((column) => ( <DataTable.ColumnHeader key={column.key} column={column.key} className="px-4 text-start"> {column.header} </DataTable.ColumnHeader> ))} </tr> </thead> <DataTable.Body> {table.pageRows.map((row) => ( <DataTable.Row key={row.id} rowId={row.id}> {table.columns.map((column, i) => ( <DataTable.Cell key={column.key} index={i} column={column.key} className={cell}> {String(row[column.key])} </DataTable.Cell> ))} </DataTable.Row> ))} </DataTable.Body> </DataTable.Table> </DataTable.Root> ); }

คอลัมน์ที่ซ่อนจะไม่ถูกค้นหาด้วย hiddenColumns / onColumnsChange ทำให้ควบคุมค่านี้เองและบันทึกไว้ได้

แก้ไขเซลล์ในตาราง

คอลัมน์ที่มี editable: 'text' | 'number' | 'select' ทำให้ตารางเป็น grid ตาม APG: Table ได้ role="grid" เซลล์หนึ่งในตัวตารางเป็นที่หยุด Tab และปุ่มลูกศร Home / End และ Ctrl + Home / End ย้ายมัน Enter, F2, ดับเบิลคลิก คลิกที่ element ที่มี data-edit-icon หรือพิมพ์ จะเปิดช่องแก้ไข ให้ render CellInput หรือ CellSelect ขณะเซลล์ถูกแก้ไข (useCellContext().editing) และ CellError สำหรับข้อความจาก validate onCellEdit ส่ง rows ใหม่ให้คุณ

import { useState } from 'react'; import { DataTable } from '@purpurabog/ui/headless'; interface Contract { id: string; name: string; amount: number; status: string; [key: string]: unknown; } const headers: DataTable.DataTableColumn[] = [ { key: 'name', header: 'ชื่อ', editable: 'text', validate: (value) => (String(value).trim() ? '' : 'ต้องใส่ชื่อ') }, { key: 'amount', header: 'จำนวนเงิน (บาท)', editable: 'number' }, { key: 'status', header: 'สถานะ', editable: 'select', options: [ { value: 'draft', label: 'ฉบับร่าง' }, { value: 'signed', label: 'ลงนามแล้ว' }, ] }, ]; const initialRows: Contract[] = [ { id: '1', name: 'สัญญาเช่าสำนักงาน 2569', amount: 250000, status: 'draft' }, { id: '2', name: 'NDA – เอซีเอ็ม', amount: 0, status: 'signed' }, ]; const editor = 'h-full w-full bg-surface-1 px-4 text-sm text-ink outline-2 -outline-offset-2 outline-focus aria-invalid:outline-error'; function CellContent({ column, value }: { column: DataTable.DataTableColumn; value: unknown }) { const { editing } = DataTable.useCellContext(); if (editing) { return ( <> {column.editable === 'select' ? <DataTable.CellSelect aria-label={column.header} className={editor} /> : <DataTable.CellInput aria-label={column.header} className={editor} />} <DataTable.CellError className="absolute start-0 top-full z-10 whitespace-nowrap bg-surface-1 px-2 py-1 text-xs text-error-text shadow" /> </> ); } const shown = column.options?.find((option) => option.value === value)?.label ?? String(value); return ( <> <DataTable.CellValue>{shown}</DataTable.CellValue> {column.editable ? <span data-edit-icon="" aria-hidden="true" className="ms-2 cursor-pointer opacity-0 group-hover:opacity-100 group-focus:opacity-100">✎</span> : null} </> ); } export default function Example() { const [rows, setRows] = useState(initialRows); const [last, setLast] = useState(''); const table = DataTable.useDataTable({ headers, rows, onCellEdit: (edit) => { setRows(edit.rows); setLast(`${edit.key}: ${String(edit.previous)} → ${edit.value}`); }, }); return ( <DataTable.Root table={table}> <DataTable.EditHint className="sr-only">กด Enter เพื่อแก้ไข</DataTable.EditHint> <DataTable.Table aria-label="สัญญา" className="w-full border-collapse"> <thead> <tr className="h-12 bg-layer-accent text-sm font-semibold text-ink"> {table.columns.map((column) => ( <DataTable.ColumnHeader key={column.key} column={column.key} className="px-4 text-start"> {column.header} </DataTable.ColumnHeader> ))} </tr> </thead> <DataTable.Body> {table.pageRows.map((row) => ( <DataTable.Row key={row.id} rowId={row.id}> {table.columns.map((column, i) => ( <DataTable.Cell key={column.key} index={i} column={column.key} className="group relative h-12 border-b border-hairline px-4 text-sm text-ink-muted data-editing:p-0 focus-visible:outline-2 focus-visible:-outline-offset-2 focus-visible:outline-focus" > <CellContent column={column} value={row[column.key]} /> </DataTable.Cell> ))} </DataTable.Row> ))} </DataTable.Body> </DataTable.Table> <p className="mt-2 text-xs text-ink-muted">แก้ไขล่าสุด: {last}</p> </DataTable.Root> ); }

ในช่องแก้ไข Enter บันทึก Escape ยกเลิก Tab / Shift + Tab บันทึกแล้วแก้เซลล์ถัดไป / ก่อนหน้าที่แก้ได้ และการออกจากเซลล์จะบันทึก ตัวควบคุมในเซลล์ (ลิงก์ ปุ่ม) จะออกจากลำดับ Tab เมื่อเป็น grid กด Enter หรือ Space ที่เซลล์ของมันเพื่อกด

ปรับความกว้างคอลัมน์

resizable (หรือ resizable ของคอลัมน์) ทำให้ ResizeHandle ทำงาน: เป็น range input จริงที่ขอบของหัวคอลัมน์ ลากได้ หรือใช้ลูกศรทีละ 2px, Shift / Page Up / Down ทีละ 16px, Home / End แคบสุด / กว้างสุด การปรับครั้งแรกจะตรึง layout อัตโนมัติเป็นความกว้างคงที่ (table.resize.fixed) แล้ว Filler จะกินพื้นที่ที่เหลือของแถว ผูก table.boxRef กับกล่องที่เลื่อนได้ คอลัมน์จะกว้างได้ไม่เกินกล่องนั้น

import { DataTable } from '@purpurabog/ui/headless'; interface Contract { id: string; name: string; owner: string; amount: string; [key: string]: unknown; } const headers = [ { key: 'name', header: 'ชื่อ', minWidth: 120 }, { key: 'owner', header: 'ผู้รับผิดชอบ' }, { key: 'amount', header: 'จำนวนเงิน (บาท)', resizable: false }, ]; const rows: Contract[] = [ { id: '1', name: 'สัญญาเช่าสำนักงาน 2569', owner: 'อนัญญา', amount: '250,000' }, { id: '2', name: 'สัญญาอนุญาตใช้ซอฟต์แวร์', owner: 'ชัย', amount: '48,000' }, ]; const cell = 'h-10 border-b border-hairline px-4 text-sm text-ink-muted'; export default function Example() { const table = DataTable.useDataTable({ headers, rows, resizable: true }); const { boxRef } = table; return ( <DataTable.Root table={table}> <div ref={boxRef} className="overflow-x-auto"> <DataTable.Table aria-label="สัญญา" style={table.resize.tableStyle} className={`w-full border-collapse ${table.resize.fixed ? 'table-fixed' : ''}`}> <thead> <tr className="h-10 bg-layer-accent text-sm font-semibold text-ink"> {table.columns.map((column) => ( <DataTable.ColumnHeader key={column.key} column={column.key} className="group relative px-4 text-start"> {column.header} <DataTable.ResizeHandle aria-label={`ปรับความกว้างคอลัมน์ ${column.header}`} valueText={(px) => `${px} พิกเซล`} className="peer absolute -end-3 top-0 z-10 m-0 h-full w-6 cursor-col-resize appearance-none bg-transparent opacity-0" /> <span aria-hidden="true" className="pointer-events-none absolute inset-y-0 end-0 w-0.5 group-hover:bg-border-strong-1 group-data-resizing:bg-interactive peer-focus-visible:bg-focus" /> </DataTable.ColumnHeader> ))} <DataTable.Filler /> </tr> </thead> <DataTable.Body> {table.pageRows.map((row) => ( <DataTable.Row key={row.id} rowId={row.id}> {table.columns.map((column, i) => ( <DataTable.Cell key={column.key} index={i} column={column.key} className={cell}> {String(row[column.key])} </DataTable.Cell> ))} <DataTable.Filler className={cell} /> </DataTable.Row> ))} </DataTable.Body> </DataTable.Table> </div> <p className="mt-2 text-xs text-ink-muted">ความกว้าง: {JSON.stringify(table.columnWidths)}</p> </DataTable.Root> ); }

columnWidths / onColumnResize ทำให้ควบคุมความกว้างเอง บันทึกและคืนค่าได้ คอลัมน์รับ width, minWidth (ค่าเริ่มต้น 50) และ maxWidth

แต่ง style

วาด checkbox เองจาก data-checked และ data-indeterminate หมุนลูกศรเรียงจาก data-direction และแรเงาแถวที่เลือกและแถวสลับสี

import { DataTable } from '@purpurabog/ui/headless'; interface Signer { id: string; name: string; role: string; [key: string]: unknown; } const headers = [ { key: 'name', header: 'ชื่อ' }, { key: 'role', header: 'บทบาท' }, ]; const rows: Signer[] = [ { id: '1', name: 'อนันต์ พ.', role: 'กรรมการผู้จัดการ' }, { id: '2', name: 'กัญญา ศ.', role: 'ที่ปรึกษากฎหมาย' }, { id: '3', name: 'สมชาย ด.', role: 'พยาน' }, ]; const cell = 'h-12 border-b border-hairline px-4 text-sm text-ink-muted'; function Box({ label, all = false }: { label: string; all?: boolean }) { const input = 'peer absolute inset-0 m-0 cursor-pointer opacity-0'; return ( <span className="relative inline-flex size-4"> {all ? <DataTable.SelectAll aria-label={label} className={input} /> : <DataTable.SelectRow aria-label={label} className={input} />} <span aria-hidden="true" className="pointer-events-none flex size-4 items-center justify-center border border-icon-primary text-[11px] leading-none text-on-primary peer-focus-visible:outline-2 peer-focus-visible:outline-offset-1 peer-focus-visible:outline-focus peer-data-checked:bg-icon-primary peer-data-checked:after:content-['✓'] peer-data-indeterminate:bg-icon-primary peer-data-indeterminate:after:content-['–']" /> </span> ); } export default function Example() { const table = DataTable.useDataTable({ headers, rows, sortable: true, selectable: true, locale: 'th' }); return ( <DataTable.Root table={table}> <DataTable.Table aria-label="ผู้ลงนาม" className="w-full border-collapse"> <thead> <tr className="bg-layer-accent text-sm text-ink"> <td className="w-12 ps-4"> <Box label="เลือกทุกแถว" all /> </td> {table.columns.map((column) => ( <DataTable.ColumnHeader key={column.key} column={column.key} className="p-0 text-start font-semibold"> <DataTable.SortButton className="group flex h-12 w-full items-center justify-between px-4 hover:bg-layer-selected-hover focus-visible:outline-2 focus-visible:-outline-offset-2 focus-visible:outline-focus data-[direction=ascending]:bg-layer-selected-hover data-[direction=descending]:bg-layer-selected-hover"> {column.header} <svg viewBox="0 0 32 32" aria-hidden="true" className="size-4 fill-icon-primary opacity-0 transition-transform group-hover:opacity-100 group-data-[direction=ascending]:opacity-100 group-data-[direction=descending]:rotate-180 group-data-[direction=descending]:opacity-100"> <path d="M16 4 6 14l1.41 1.41L15 7.83V28h2V7.83l7.59 7.58L26 14 16 4z" /> </svg> </DataTable.SortButton> </DataTable.ColumnHeader> ))} </tr> </thead> <DataTable.Body> {table.pageRows.map((row) => ( <DataTable.Row key={row.id} rowId={row.id} className="even:bg-layer-accent hover:bg-ghost-hover data-selected:bg-layer-selected-1 data-selected:[&>td]:text-ink"> <DataTable.Cell index={0} className={`${cell} ps-4`}> <Box label={`เลือกแถว, ${row.name}`} /> </DataTable.Cell> {table.columns.map((column, i) => ( <DataTable.Cell key={column.key} index={i + 1} column={column.key} className={cell}> {String(row[column.key])} </DataTable.Cell> ))} </DataTable.Row> ))} </DataTable.Body> </DataTable.Table> </DataTable.Root> ); } th[data-sortable] button:hover { background: #d1d1d1; } button[data-direction='ascending'] .arrow, button[data-direction='descending'] .arrow { opacity: 1; } button[data-direction='descending'] .arrow { transform: rotate(180deg); } tr[data-row-id]:nth-child(even) { background: #e0e0e0; } tr[data-selected] { background: #e0e0e0; color: #161616; } input[data-checked] + .box, input[data-indeterminate] + .box { background: #161616; } td[data-editable]:focus-visible { outline: 2px solid #0f62fe; outline-offset: -2px; } td[data-editing] { padding: 0; } th[data-resizing] { background: #c6c6c6; }

Hook

DataTable.useDataTable(options)

เก็บ state ทุกส่วนของ state ควบคุมเองได้ (sortKey, selectedIds …) หรือปล่อยให้ hook เก็บ (default…)

Option Type ค่าเริ่มต้น
headers DataTableColumn[] คอลัมน์: { key, header } และตัวเลือกของคอลัมน์ข้างล่าง (ต้องใส่)
rows R[] แถว: { id, …cells } ใส่ disabled เพื่อไม่ให้เลือก (ต้องใส่)
id string สร้างให้ ส่วนต้นของ id ที่ parts ใส่ให้ element
sortable boolean false ทุกคอลัมน์เรียงได้ ยกเว้น header ที่ตั้ง sortable: false
sortKey, sortDirection string, SortDirection การเรียง (ควบคุมเอง)
defaultSortKey, defaultSortDirection string, SortDirection '', 'none' การเรียงตอนเริ่ม
onSortChange (sort: { key, direction }) => void หลังกดปุ่มเรียง
manualSorting boolean false คง rows ตามลำดับของคุณ (เรียงบน server) หัวตารางยังแสดงการเรียง
sortRow (a, b, context) => number ใช้แทนการเปรียบเทียบเดิม
locale string 'en' ภาษาที่ใช้เรียง (ตารางภาษาไทยใส่ 'th')
selectable boolean false checkbox พร้อมช่องเลือกทั้งหมด
radio boolean false เลือกได้หนึ่งแถว มี radio ทุกแถว
selectedIds, defaultSelectedIds string[] [] แถวที่เลือก
onSelectionChange (ids: string[], rows: R[]) => void หลังการเลือกเปลี่ยน
filterText, defaultFilterText string '' คำค้น
onFilterChange (value: string) => void หลัง setFilterText
filterRows (row, text, headers) => boolean ใช้แทนการตรวจแถวของการค้นหา
manualFiltering boolean false คงทุกแถวไว้ (กรองบน server)
expandable boolean false แถวเปิดปิดได้
expandedIds, defaultExpandedIds string[] [] แถวที่เปิดอยู่
onExpandChange (change: { expandedIds, expanded, id? }) => void หลังแถวหนึ่ง (หรือทุกแถว เมื่อไม่มี id) เปิดหรือปิด
pageSize, defaultPageSize number 0 แถวต่อหน้า (0: หน้าเดียวทุกแถว)
page, defaultPage number 1 หน้า เริ่มที่ 1
onPageChange (change: { page, pageSize }) => void หลัง setPagination หรือเมื่อค้นหาใหม่ขณะอยู่หน้าหลัง ๆ
hiddenColumns, defaultHiddenColumns string[] header ที่มี hidden key ของคอลัมน์ที่ไม่แสดง
onColumnsChange (change: { hiddenColumns, key, hidden }) => void หลังแสดงหรือซ่อนคอลัมน์ (key เป็น '' หลังรีเซ็ต)
resizable boolean false ทุกคอลัมน์มีที่จับปรับความกว้าง
columnWidths, defaultColumnWidths Record<string, number> {} ความกว้างคอลัมน์เป็น px ตาม key
onColumnResize (change: { key, width, widths }) => void หลังปรับความกว้าง (ครั้งเดียวต่อการลากหรือการกดปุ่ม)
staticWidth boolean false ตารางที่ปรับความกว้างแล้วกว้างเท่าผลรวมคอลัมน์ ไม่ใช่เท่ากล่อง
onCellEdit (edit: { rowId, key, value, previous, row, rows }) => void เซลล์ที่แก้ถูกบันทึก: ตั้ง rows เป็น edit.rows

ตัวเลือกของคอลัมน์ในแต่ละ header: sortable, editable ('text' \| 'number' \| 'select'), options ({ value, label }[] for a select), validate(value, row), resizable, width, minWidth, maxWidth, hideable, hidden, pinned ('start' \| 'end').

สิ่งที่คืนมา แยกตามหัวข้อ:

แถว rows, filteredRows (ที่การค้นหาเหลือไว้), sortedRows, pageRows (แถวที่จะ render), rowById
คอลัมน์ headers (ทั้งหมด), columns (ที่แสดง), hiddenColumns, toggleColumn(key, show), resetColumns(), isColumnLocked(key), columnsChanged, pins
การเรียง sortKey, sortDirection, sortDirectionOf(key), isSortable(column), sortBy(key)
การค้นหา filterText, setFilterText(text)
หน้า paged, page, pageSize, pageCount, setPagination({ page, pageSize })
การเลือก selectable, radio, selectedIds, isSelected(id), setSelectedIds(ids), toggleRow(row, checked), toggleAll(), selectAll(), allSelected, someSelected, radioStop, getSelectAllProps(), getSelectRowProps(row)
แถวที่เปิด expandable, expandedIds, isExpanded(id), allExpanded, toggleExpanded(id), toggleAllExpanded(), expandedRowId(id)
ความกว้างและ grid columnWidths, resize (fixed, hasFiller, tableStyle, widthOf(column) …), grid (enabled …), tableRef, boxRef, id

DataTable.useDataTableContext() คืน object เดียวกันนี้ให้ component ใดก็ได้ที่อยู่ใน Root

Parts

DataTable.Root

ไม่ render element ของตัวเอง แค่ส่งตารางจาก hook ให้ลูก

Prop Type ค่าเริ่มต้น
table สิ่งที่ useDataTable คืนมา ต้องใส่

DataTable.Table

สร้าง <table> พร้อม tableRef ของ hook เป็น role="grid" เมื่อมีคอลัมน์ที่ editable รับ prop ทุกตัวของ <table>

DataTable.Body

สร้าง <tbody> เมื่อเป็น grid จะจัดการปุ่ม คลิก ดับเบิลคลิก และ focus ของเซลล์

DataTable.ColumnHeader

สร้าง <th scope="col"> ที่มี aria-sort เมื่อคอลัมน์เรียงได้ data-key และความกว้างที่ปรับแล้ว รับ prop ทุกตัวของ <th>

Prop Type ค่าเริ่มต้น
column string key ของคอลัมน์ (ต้องใส่)
Attribute เมื่อ
data-sortable คอลัมน์เรียงได้
data-resizing กำลังลากที่จับของมัน

DataTable.SortButton

สร้าง <button type="button"> ที่เลื่อนการเรียงของคอลัมน์: ไม่เรียง → น้อยไปมาก → มากไปน้อย → ไม่เรียง คอลัมน์ใหม่เริ่มที่น้อยไปมาก อธิบายขั้นถัดไปด้วย aria-describedby ได้ถ้าต้องการ

Attribute เมื่อ
data-direction มีเสมอ: none, ascending หรือ descending

DataTable.ResizeHandle

สร้าง <input type="range"> (ไม่ render อะไรถ้าคอลัมน์ปรับไม่ได้) ตั้งชื่อให้ด้วย ("ปรับความกว้างคอลัมน์ ชื่อ") รับ prop ทุกตัวของ input ยกเว้น type, value, min, max และ step

Prop Type ค่าเริ่มต้น
valueText (width: number) => string ความกว้างเป็นคำสำหรับเทคโนโลยีช่วยเหลือ ("120 พิกเซล")

DataTable.Row

สร้าง <tr> ที่มี data-row-id ซึ่ง grid และ radio ใช้หาแถว

Prop Type ค่าเริ่มต้น
rowId string id ของแถว (ต้องใส่)
Attribute เมื่อ
data-selected แถวถูกเลือก
data-expanded แถวเปิดอยู่

DataTable.Cell

สร้าง <td> เมื่อเป็น grid เซลล์หนึ่งในตัวตารางเป็นที่หยุด Tab ที่เหลือเป็น tabIndex={-1} เซลล์ที่แก้ไม่ได้เป็น aria-readonly

Prop Type ค่าเริ่มต้น
index number ลำดับของเซลล์ในแถว เริ่มที่ 0 นับทุกเซลล์ (ขยาย เลือก ข้อมูล) (ต้องใส่)
column string key ของคอลัมน์ สำหรับเซลล์ข้อมูล คอลัมน์ที่ editable ทำให้เซลล์แก้ไขได้
Attribute เมื่อ
data-editable เซลล์แก้ไขได้
data-editing เซลล์กำลังถูกแก้ไข
data-key key ของคอลัมน์ ที่เซลล์ที่แก้ได้

DataTable.useCellContext() คืน rowId, column และ editing ของเซลล์ ({ draft, error } ขณะแก้ไข ไม่อย่างนั้นเป็น null)

DataTable.CellValue, DataTable.EditHint

CellValue สร้าง <span> ครอบค่าของเซลล์ที่แก้ได้ คลิกที่มันจะ focus เซลล์ EditHint สร้าง <span> ที่มีข้อความอธิบายเซลล์ที่แก้ได้ทุกเซลล์ ("กด Enter เพื่อแก้ไข") เฉพาะเมื่อเป็น grid ให้ซ่อนจากสายตา

DataTable.CellInput, DataTable.CellSelect, DataTable.CellError

ช่องแก้ไขขณะเซลล์ถูกแก้: CellInput เป็น <input> (ข้อความ หรือตัวเลขพร้อม inputMode="decimal") CellSelect เป็น <select> ของ options ของคอลัมน์ (หรือ <option> ที่คุณใส่เอง) ทั้งสองเป็น aria-invalid และอธิบายด้วยข้อความ error ขณะ validate ไม่รับค่า CellError สร้าง <span role="alert"> พร้อมข้อความ และไม่ render อะไรเมื่อค่าถูกต้อง

DataTable.SelectAll

สร้าง checkbox จริง (Checkbox แบบ headless) ติ๊กเมื่อเลือกครบทุกแถวในขอบเขต เป็นแบบผสมเมื่อเลือกบางแถว กดแล้วจะล้างการเลือกถ้ามีแถวที่เลือก ไม่อย่างนั้นเลือกทุกแถวที่ใช้งานได้ที่แสดงอยู่ (แบบ Carbon) ไม่ render ในตาราง radio

Attribute เมื่อ
data-checked เลือกครบทุกแถวในขอบเขต
data-indeterminate เลือกบางแถว

DataTable.SelectRow

สร้าง checkbox จริงของแถว หรือ radio ในตาราง radio แถวที่ disabled จะมีช่องที่ disabled

Attribute เมื่อ
data-checked แถวถูกเลือก

DataTable.ExpandToggle, DataTable.ExpandAll

แต่ละตัวสร้าง <button type="button"> ที่มี aria-expanded และ aria-controls ชี้ไปที่แถวที่เปิด ExpandToggle เปิดหรือปิดแถวของมัน ExpandAll เปิดทุกแถวที่แสดง หรือปิดทั้งหมดเมื่อเปิดครบ ตั้งชื่อด้วย aria-label

Attribute เมื่อ
data-expanded แถวของมันเปิดอยู่ (ทุกแถว สำหรับ ExpandAll)

DataTable.ExpandedRow

สร้าง <tr> ใต้แถวที่เปิด พร้อม id ที่ปุ่มของมันควบคุม

Prop Type ค่าเริ่มต้น
rowId string id ของแถวที่เปิด (ต้องใส่)

DataTable.Filler

สร้าง <td aria-hidden="true"> ว่าง ที่กินพื้นที่ที่เหลือของแถวเมื่อคอลัมน์ถูกปรับความกว้างแล้ว (เฉพาะตอนนั้น)

คีย์บอร์ดและ accessibility

  • เมื่อไม่มีคอลัมน์ที่แก้ได้ parts เป็นของจริงทั้งหมด ปุ่มเรียง checkbox และปุ่มขยาย อยู่ในลำดับ Tab ทุกตัว radio ของตาราง radio หยุด Tab ที่เดียว ปุ่มลูกศรย้ายและเลือก วนกลับเมื่อสุดปลาย
  • เมื่อมีคอลัมน์ที่แก้ได้ ตารางเป็น grid ตาม WAI-ARIA: เซลล์ในตัวตารางหยุด Tab ที่เดียว ปุ่มลูกศร Home / End และ Ctrl + Home / End ย้ายมัน และ Enter หรือ Space กดตัวควบคุมในเซลล์
  • สิ่งที่คุณต้องใส่เอง: ชื่อตาราง (aria-label หรือ aria-labelledby ไปที่หัวข้อของคุณ) ชื่อของตัวเลือกและปุ่มขยาย (บอกชื่อแถว: "เลือกแถว, NDA") ชื่อสำหรับ screen reader ของคอลัมน์ radio และข้อความ "กด Enter เพื่อแก้ไข"
  • ประกาศสิ่งที่เปลี่ยนเอง: หลังเรียง ("เรียงตามคอลัมน์ สัญญา จากน้อยไปมาก") และเมื่อหยุดพิมพ์ค้นหา ("แสดง 3 จาก 12 แถว") ด้วยสถานะแบบ polite ใส่ aria-busy ที่ตารางขณะโหลดใหม่