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 Structured list

StructuredList คือการเลือกแถวของ Structured list แบบมี style ในรูป React parts ที่ไม่มี style ส่วนอื่นของ structured list เป็น markup ตารางธรรมดา (thead, tbody, th, td) parts จึงมีแค่สิ่งที่การเลือกต้องใช้ คือ <table> (Root) แถวที่เลือกได้ (Row ที่มี value คลิกตรงไหนของแถวก็เลือก) และ Radio ของแถว (คีย์บอร์ด: Tab หยุดที่เดียว ปุ่มลูกศรย้ายและเลือก)

ส่วนที่คุณเขียนเอง: เซลล์ แถวหัว ไอคอน radio และ style ทั้งหมด สถานะแสดงเป็น attribute data-* ให้ใช้จัด style

ติดตั้ง

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

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

วิธีใช้

import { StructuredList } from '@purpurabog/ui/headless'; const plans = [ { value: 'basic', name: 'พื้นฐาน', price: 'ฟรี' }, { value: 'team', name: 'ทีม', price: '฿390 / คน' }, { value: 'enterprise', name: 'องค์กร', price: 'ติดต่อฝ่ายขาย' }, ]; export default function Example() { return ( <StructuredList.Root aria-label="เลือกแพ็กเกจ" values={plans.map((p) => p.value)} defaultValue="team" className="w-full max-w-md border-collapse text-sm text-ink-muted"> <thead> <tr className="text-start text-ink"> <th className="w-10"> <span className="sr-only">เลือก</span> </th> <th className="px-2 py-2 text-start font-semibold">แพ็กเกจ</th> <th className="px-2 py-2 text-start font-semibold">ราคา</th> </tr> </thead> <tbody> {plans.map((plan) => ( <StructuredList.Row key={plan.value} value={plan.value} className="cursor-pointer border-t border-hairline hover:bg-ghost-hover data-selected:bg-surface-2 data-selected:text-ink focus-within:outline-2 focus-within:-outline-offset-2 focus-within:outline-focus"> <td className="py-3 ps-4"> <StructuredList.Radio aria-label={plan.name} className="accent-ink" /> </td> <td className="px-2 py-3">{plan.name}</td> <td className="px-2 py-3">{plan.price}</td> </StructuredList.Row> ))} </tbody> </StructuredList.Root> ); }

โครงสร้าง

<StructuredList.Root> <thead> <tr><th /></tr> </thead> <tbody> <StructuredList.Row value="…"> <td><StructuredList.Radio /></td> <td /> </StructuredList.Row> </tbody> </StructuredList.Root>

list ที่ไม่ต้องเลือก

ถ้าไม่ต้องเลือกก็ไม่ต้องใช้ part เลย เขียนตารางเองได้ Root และ Row ที่ไม่มี value สร้าง <table> และ <tr> ธรรมดา ใช้ได้เมื่อ list เดียวกันเปิดปิดการเลือกได้

import { StructuredList } from '@purpurabog/ui/headless'; const terms = [ ['คู่สัญญา', 'บริษัท เอซีเอ็ม จำกัด'], ['ลงนามเมื่อ', '30 กันยายน 2569'], ['ระยะเวลา', '12 เดือน'], ]; export default function Example() { return ( <StructuredList.Root aria-label="สรุปสัญญา" className="w-full max-w-md border-collapse text-sm"> <tbody> {terms.map(([term, value]) => ( <StructuredList.Row key={term} className="border-t border-hairline last:border-b"> <th scope="row" className="whitespace-nowrap px-4 py-3 text-start font-normal text-ink-muted">{term}</th> <td className="px-2 py-3 text-ink">{value}</td> </StructuredList.Row> ))} </tbody> </StructuredList.Root> ); }

ควบคุมค่าเอง

ส่ง value และ onValueChange เพื่อเก็บตัวเลือกเอง หรือใช้ defaultValue ให้ Root เก็บให้ values คือค่าของแถวที่เลือกได้ตามลำดับ ใช้บอก "1 จาก 3" ของ radio และบอกว่าแถวไหนรับ Tab เมื่อยังไม่มีแถวที่เลือก value ที่ไม่อยู่ใน values จะไม่เลือกแถวใด

import { useState } from 'react'; import { StructuredList } from '@purpurabog/ui/headless'; const methods = [ { value: 'email', name: 'PDF ที่ลงนามแล้วทางอีเมล', arrives: 'วันนี้' }, { value: 'post', name: 'ไปรษณีย์ลงทะเบียน', arrives: '3–5 วัน' }, { value: 'courier', name: 'แมสเซนเจอร์', arrives: 'วันถัดไป' }, ]; export default function Example() { const [method, setMethod] = useState(''); return ( <div className="grid max-w-md gap-4"> <StructuredList.Root aria-label="วิธีส่งเอกสาร" values={methods.map((m) => m.value)} value={method} onValueChange={setMethod} className="w-full border-collapse text-sm text-ink-muted"> <tbody> {methods.map((m) => ( <StructuredList.Row key={m.value} value={m.value} className="cursor-pointer border-t border-hairline last:border-b hover:bg-ghost-hover data-selected:bg-surface-2 data-selected:text-ink focus-within:outline-2 focus-within:-outline-offset-2 focus-within:outline-focus"> <td className="py-2 ps-4"> <StructuredList.Radio aria-label={m.name} className="accent-ink" /> </td> <td className="px-2 py-2">{m.name}</td> <td className="px-2 py-2">{m.arrives}</td> </StructuredList.Row> ))} </tbody> </StructuredList.Root> <div className="flex gap-2"> <button type="button" onClick={() => setMethod('email')} className="h-8 border border-interactive px-3 text-sm text-interactive hover:bg-interactive hover:text-on-primary focus-visible:outline-2 focus-visible:outline-focus"> ใช้วิธีที่แนะนำ </button> <button type="button" onClick={() => setMethod('')} className="h-8 px-3 text-sm text-link hover:bg-ghost-hover focus-visible:outline-2 focus-visible:outline-focus"> ล้าง </button> </div> <p className="text-xs text-ink-muted">ค่า: {method || '(ไม่มี)'}</p> </div> ); }

onValueChange ทำงานเฉพาะเมื่อผู้ใช้เลือกแถว ไม่ทำงานเมื่อคุณเปลี่ยน value เอง

แต่ง style

ซ่อน radio จริงไว้ แล้ววาดไอคอน radio แบบ Carbon จาก data-selected ของแถว ใส่ class group ที่แถวเพื่อให้ไอคอนอ่านค่านี้ได้

import { StructuredList } from '@purpurabog/ui/headless'; const plans = [ { value: 'basic', name: 'พื้นฐาน', users: 'ไม่เกิน 5 คน' }, { value: 'team', name: 'ทีม', users: 'ไม่เกิน 50 คน' }, { value: 'enterprise', name: 'องค์กร', users: 'ไม่จำกัด' }, ]; export default function Example() { return ( <StructuredList.Root aria-label="แพ็กเกจ" values={plans.map((p) => p.value)} defaultValue="basic" className="w-full max-w-md border-collapse text-sm text-ink-muted"> <thead> <tr> <th className="w-8 py-2 ps-4"> <span className="sr-only">เลือก</span> </th> <th className="px-4 py-2 text-start font-semibold text-ink">แพ็กเกจ</th> <th className="px-4 py-2 text-start font-semibold text-ink">ผู้ใช้</th> </tr> </thead> <tbody> {plans.map((plan) => ( <StructuredList.Row key={plan.value} value={plan.value} className="group cursor-pointer border-t border-hairline last:border-b hover:bg-ghost-hover hover:text-ink data-selected:bg-surface-2 data-selected:text-ink focus-within:outline-2 focus-within:-outline-offset-2 focus-within:outline-focus"> <td className="py-3 ps-4 align-top"> <StructuredList.Radio aria-label={plan.name} className="sr-only" /> <svg viewBox="0 0 16 16" aria-hidden="true" className="mt-0.5 size-4 fill-icon-primary"> <circle cx="8" cy="8" r="6.5" fill="none" stroke="currentColor" /> <circle cx="8" cy="8" r="3" className="radio-dot hidden group-data-selected:block" /> </svg> </td> <td className="px-4 py-3">{plan.name}</td> <td className="px-4 py-3">{plan.users}</td> </StructuredList.Row> ))} </tbody> </StructuredList.Root> ); } tr[data-selectable] { cursor: pointer; } tr[data-selectable]:hover { background: #e8e8e8; } tr[data-selected] { background: #e0e0e0; color: #161616; } tr[data-selectable]:focus-within { outline: 2px solid #0f62fe; outline-offset: -2px; } tr .radio-dot { display: none; } tr[data-selected] .radio-dot { display: block; }

Parts

StructuredList.Root

สร้าง <table> รับ prop ทุกตัวของ <table>

Prop Type ค่าเริ่มต้น
values readonly string[] [] ค่าของแถวที่เลือกได้ตามลำดับ: ขนาดของกลุ่ม radio และ Tab แรก
value string ค่าของแถวที่เลือก (ควบคุมเอง) ค่าที่ไม่อยู่ใน values จะไม่เลือกแถวใด
defaultValue string '' ค่าเริ่มต้น (ไม่ควบคุมเอง)
onValueChange (value: string) => void หลังผู้ใช้เลือกแถวอื่น

StructuredList.useStructuredListContext() คืน value, focusValue (แถวที่ radio รับ Tab), values และ select(value) ให้ component ใดก็ได้ที่อยู่ใน Root

StructuredList.Row

สร้าง <tr> ถ้ามี value จะเลือกได้: คลิกตรงไหนของแถวก็เลือก และมี aria-selected ถ้าไม่มีก็เป็นแถวธรรมดา (สำหรับแถวหัวหรือ list ที่อ่านอย่างเดียว)

Prop Type ค่าเริ่มต้น
value string ทำให้แถวเลือกได้ และใช้ระบุแถวใน value ของ Root
Attribute เมื่อ
data-selectable แถวมี value
data-selected แถวนี้คือแถวที่เลือก

StructuredList.useRowContext() คืน value และ selected ของแถว ให้ component ใดก็ได้ที่อยู่ใน Row ที่เลือกได้

StructuredList.Radio

สร้าง <input type="radio"> ที่ถูกติ๊กเมื่อแถวของมันถูกเลือก ต้องอยู่ใน Row ที่เลือกได้ รับ prop ทุกตัวของ input ยกเว้น type, value, checked และ defaultChecked ตั้งชื่อด้วย aria-label

Attribute เมื่อ
data-selected แถวของมันถูกเลือก

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

  • radio ทั้งหมดมี Tab หยุดที่เดียว: radio ของแถวที่เลือก ถ้าไม่มีก็แถวแรก
  • ลูกศรขึ้น ลง ซ้าย ขวา ย้าย focus และการเลือกไปแถวก่อนหน้าหรือถัดไป วนกลับเมื่อสุดปลาย Space เลือกแถวที่ focus อยู่
  • radio แต่ละตัวมี aria-posinset และ aria-setsize จาก values screen reader จึงอ่านว่า "2 จาก 3"
  • สิ่งที่คุณต้องใส่เอง: ชื่อตาราง (aria-label ที่ Root) ชื่อของแต่ละ radio (ข้อความหลักของแถว) และชื่อสำหรับ screen reader ของเซลล์หัวคอลัมน์ radio
  • เมื่อซ่อน radio ให้ยังอยู่ใน accessibility tree: ใช้ sr-only ไม่ใช่ display: none