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 Radio button

RadioGroup.Root คือ role="radiogroup" ที่ครอบ <input type="radio"> จริงซึ่งใช้ name เดียวกัน browser จึงให้จุดหยุด Tab จุดเดียว ปุ่มลูกศร และการส่งฟอร์ม part เพิ่มค่าของกลุ่ม สถานะอ่านอย่างเดียว onValueChange และ attribute data-* ให้คุณใช้ใส่ style

ส่วนที่คุณทำเองคือ <label> ของแต่ละตัวเลือก วงกลมที่วาดเอง label ของกลุ่ม ข้อความช่วยเหลือหรือ error การจัดวาง และ style ทั้งหมด แบบมี style ดูที่ Radio button

ติดตั้ง

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

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

วิธีใช้

ซ่อนแต่ละ input ไว้ใต้วงกลมที่คุณวาดข้าง ๆ แล้วครอบทั้งสองด้วย <label>

import { RadioGroup } from '@purpurabog/ui/headless'; export default function Example() { return ( <RadioGroup.Root aria-label="การชำระเงิน" name="payment" className="grid gap-2"> <label className="relative flex w-fit cursor-pointer items-center gap-2 text-sm text-ink"> <RadioGroup.Item value="transfer" className="peer sr-only" /> <span aria-hidden="true" className="grid size-[18px] shrink-0 place-items-center rounded-full border border-ink after:size-2 after:rounded-full after:bg-ink after:opacity-0 peer-focus-visible:outline-2 peer-focus-visible:outline-offset-1 peer-focus-visible:outline-focus peer-data-checked:after:opacity-100" /> โอนผ่านธนาคาร </label> <label className="relative flex w-fit cursor-pointer items-center gap-2 text-sm text-ink"> <RadioGroup.Item value="card" className="peer sr-only" /> <span aria-hidden="true" className="grid size-[18px] shrink-0 place-items-center rounded-full border border-ink after:size-2 after:rounded-full after:bg-ink after:opacity-0 peer-focus-visible:outline-2 peer-focus-visible:outline-offset-1 peer-focus-visible:outline-focus peer-data-checked:after:opacity-100" /> บัตรเครดิต </label> </RadioGroup.Root> ); }

ใส่ name ให้กลุ่มเมื่ออยู่ในฟอร์ม ถ้าไม่ใส่ Root จะสร้างชื่อให้เอง เพื่อให้ตัวเลือกยังเป็นกลุ่มเดียวกัน

โครงสร้าง

<RadioGroup.Root> <label> <RadioGroup.Item value="…" /> {/* วงกลมและข้อความของคุณ */} </label> </RadioGroup.Root>

ค่า

เก็บค่าเองด้วย value คู่กับ onValueChange หรือให้ค่าเริ่มต้นด้วย defaultValue ถ้าไม่มีทั้งสองอย่าง จะไม่มีตัวเลือกไหนถูกเลือก ค่า '' ก็ไม่เลือกอะไรเช่นกัน ฟอร์มจะส่ง value ของตัวเลือกที่เลือกไว้ภายใต้ name

import { useState } from 'react'; import { RadioGroup } from '@purpurabog/ui/headless'; const methods = [ { value: 'transfer', label: 'โอนผ่านธนาคาร' }, { value: 'card', label: 'บัตรเครดิต' }, { value: 'cash', label: 'ชำระเงินสดที่สาขา' }, ]; // วงกลมที่วาดต่อจาก input ที่ซ่อนไว้ (เป็น peer ของมัน) มีจุดตรงกลางขณะถูกเลือก function Circle() { return <span aria-hidden="true" className="grid size-[18px] shrink-0 place-items-center rounded-full border border-ink after:size-2 after:rounded-full after:bg-ink after:opacity-0 peer-focus-visible:outline-2 peer-focus-visible:outline-offset-1 peer-focus-visible:outline-focus peer-data-checked:after:opacity-100" />; } export default function Example() { const [payment, setPayment] = useState('card'); const [sent, setSent] = useState<Record<string, FormDataEntryValue>>(); return ( <form className="grid justify-items-start gap-3" onSubmit={(e) => { e.preventDefault(); setSent(Object.fromEntries(new FormData(e.currentTarget))); }} > <span id="payment-value-label" className="text-xs text-ink-muted">การชำระเงิน</span> <RadioGroup.Root aria-labelledby="payment-value-label" name="payment" value={payment} onValueChange={setPayment} className="grid gap-2"> {methods.map((m) => ( <label key={m.value} className="relative flex w-fit cursor-pointer items-center gap-2 text-sm text-ink"> <RadioGroup.Item value={m.value} className="peer sr-only" /> <Circle /> {m.label} </label> ))} </RadioGroup.Root> <p className="text-xs text-ink-muted">ค่า: {payment || '(ไม่มี)'}</p> <div className="flex gap-px"> <button type="submit" className="h-10 bg-interactive px-4 text-sm text-on-primary hover:bg-blue-hover focus-visible:outline-2 focus-visible:outline-offset-1 focus-visible:outline-focus">ส่ง</button> <button type="button" onClick={() => setPayment('')} className="h-10 px-4 text-sm text-link hover:bg-ghost-hover focus-visible:outline-2 focus-visible:-outline-offset-2 focus-visible:outline-focus">ล้างค่า</button> </div> {sent ? <p className="text-xs text-ink-muted">ส่งแล้ว: {JSON.stringify(sent)}</p> : null} </form> ); }

แนวการวางและตำแหน่งข้อความ

orientation ตั้งแค่ data-orientation ปุ่มลูกศรของ browser ใช้ได้ทั้งสองแนว และการจัดวางเป็น CSS ของคุณ ถ้าต้องการให้ข้อความอยู่ก่อนวงกลม ให้วางข้อความไว้ก่อน Item ใน <label>

import { RadioGroup } from '@purpurabog/ui/headless'; const lengths = [ { value: '6', label: '6 เดือน' }, { value: '12', label: '1 ปี' }, { value: '36', label: '3 ปี' }, ]; function Circle() { return <span aria-hidden="true" className="grid size-[18px] shrink-0 place-items-center rounded-full border border-ink after:size-2 after:rounded-full after:bg-ink after:opacity-0 peer-focus-visible:outline-2 peer-focus-visible:outline-offset-1 peer-focus-visible:outline-focus peer-data-checked:after:opacity-100" />; } export default function Example() { return ( <div className="grid gap-6"> <RadioGroup.Root aria-label="ระยะเวลาสัญญา" name="length" orientation="horizontal" defaultValue="12" className="flex flex-wrap gap-6"> {lengths.map((l) => ( <label key={l.value} className="relative flex cursor-pointer items-center gap-2 text-sm text-ink"> <RadioGroup.Item value={l.value} className="peer sr-only" /> <Circle /> {l.label} </label> ))} </RadioGroup.Root> {/* ข้อความก่อนวงกลม: วางไว้ก่อน Item */} <RadioGroup.Root aria-label="ระยะเวลาสัญญา ข้อความอยู่ก่อน" name="length-text-first" orientation="horizontal" defaultValue="12" className="flex flex-wrap gap-6"> {lengths.map((l) => ( <label key={l.value} className="relative flex cursor-pointer items-center gap-2 text-sm text-ink"> {l.label} <RadioGroup.Item value={l.value} className="peer sr-only" /> <Circle /> </label> ))} </RadioGroup.Root> </div> ); }

อ่านอย่างเดียวและปิดใช้งาน

  • disabled ที่ Item ปิดใช้งานตัวเลือกนั้นตัวเดียว ถ้าตั้งที่ Root จะปิดใช้งานทุกตัวเลือก และไม่มีค่าถูกส่ง
  • readOnly คงค่าไว้และยังส่งไปกับฟอร์ม การคลิกและปุ่มลูกศรไม่เปลี่ยนอะไร แต่ปุ่มลูกศรยังย้ายโฟกัสเพื่ออ่านตัวเลือกได้ (เหมือน Carbon React) กลุ่มได้ aria-readonly
import { RadioGroup } from '@purpurabog/ui/headless'; // อ่านอย่างเดียว: วงสีเทา, ปิดใช้งาน: ทุกอย่างเป็นสีเทา function Circle() { return <span aria-hidden="true" className="grid size-[18px] shrink-0 place-items-center rounded-full border border-ink after:size-2 after:rounded-full after:bg-ink after:opacity-0 peer-focus-visible:outline-2 peer-focus-visible:outline-offset-1 peer-focus-visible:outline-focus peer-data-checked:after:opacity-100 peer-data-readonly:border-ink-subtle peer-data-disabled:border-ink-disabled peer-data-disabled:after:bg-ink-disabled" />; } const row = 'relative flex w-fit cursor-pointer items-center gap-2 text-sm text-ink has-data-readonly:cursor-default has-data-disabled:cursor-not-allowed has-data-disabled:text-ink-disabled'; export default function Example() { return ( <RadioGroup.Root aria-label="การชำระเงิน" defaultValue="transfer" readOnly className="grid gap-2"> <label className={row}> <RadioGroup.Item value="transfer" className="peer sr-only" /> <Circle /> โอนผ่านธนาคาร </label> <label className={row}> <RadioGroup.Item value="cash" disabled className="peer sr-only" /> <Circle /> ชำระเงินสดที่สาขา </label> </RadioGroup.Root> ); }

required ตั้งแค่ aria-required ที่กลุ่ม และ invalid ตั้ง aria-invalid ที่กลุ่ม กับ data-invalid ที่กลุ่มและทุกตัวเลือก ให้ตรวจค่าและแสดง error เอง (ฟอร์มและการตรวจค่า) ใส่ id ให้ข้อความ แล้วใส่ไว้ใน aria-describedby ของกลุ่ม ทุกตัวเลือกจะได้ aria-describedby เดียวกัน screen reader จึงได้ยินข้อความนั้นที่ตัวเลือกที่โฟกัสไปถึง

ใส่ style

import { useState } from 'react'; import { RadioGroup } from '@purpurabog/ui/headless'; const methods = [ { value: 'transfer', label: 'โอนผ่านธนาคาร' }, { value: 'card', label: 'บัตรเครดิต' }, { value: 'cash', label: 'เงินสด (วันนี้ปิด)', disabled: true }, ]; export default function Example() { const [payment, setPayment] = useState(''); const invalid = payment === ''; return ( <div className="grid gap-2"> <span id="payment-style-label" className="text-xs text-ink-muted">การชำระเงิน</span> <RadioGroup.Root aria-labelledby="payment-style-label" aria-describedby={invalid ? 'payment-style-error' : undefined} value={payment} onValueChange={setPayment} invalid={invalid} required orientation="horizontal" className="flex flex-col gap-2 data-[orientation=horizontal]:flex-row data-[orientation=horizontal]:gap-6" > {methods.map((m) => ( <label key={m.value} className="relative flex cursor-pointer items-center gap-2 text-sm text-ink has-data-disabled:cursor-not-allowed has-data-disabled:text-ink-disabled"> <RadioGroup.Item value={m.value} disabled={m.disabled} className="peer sr-only" /> <span aria-hidden="true" className="grid size-[18px] shrink-0 place-items-center rounded-full border border-ink after:size-2 after:rounded-full after:bg-ink after:opacity-0 peer-focus-visible:outline-2 peer-focus-visible:outline-offset-1 peer-focus-visible:outline-focus peer-data-checked:after:opacity-100 peer-data-invalid:border-error peer-data-disabled:border-ink-disabled" /> {m.label} </label> ))} </RadioGroup.Root> {invalid ? <p id="payment-style-error" className="text-xs text-error-text">เลือกวิธีชำระเงิน</p> : null} </div> ); } /* className="circle" ที่ span ที่คุณวาดต่อจากแต่ละ input */ [role="radiogroup"][data-orientation="horizontal"] { display: flex; gap: 24px; } input[data-checked] + .circle { border-width: 4px; } input[data-invalid] + .circle { border-color: #da1e28; } input[data-readonly] + .circle { border-color: #c6c6c6; } input:focus-visible + .circle { outline: 2px solid #0f62fe; }

Parts

RadioGroup.Root

<div role="radiogroup"> รับ prop ของ div ได้ทุกตัว ยกเว้น defaultValue และ role

Prop Type ค่าเริ่มต้น
name string สร้างให้ ชื่อที่ส่งไปกับฟอร์ม ใช้ร่วมกันทุกตัวเลือก
value string ค่าของตัวเลือกที่เลือก (controlled) '' คือไม่เลือกอะไร
defaultValue string '' ค่าเริ่มต้น (uncontrolled)
onValueChange (value: string, event: ChangeEvent<HTMLInputElement>) => void หลังผู้ใช้เลือกตัวเลือกอื่น
orientation 'vertical' | 'horizontal' 'vertical' ตั้งแค่ data-orientation
disabled boolean false ปิดใช้งานทุกตัวเลือก (aria-disabled ที่กลุ่ม)
readOnly boolean false คงค่าไว้ การคลิกและปุ่มลูกศรไม่เปลี่ยนอะไร (aria-readonly)
required boolean aria-required ที่กลุ่ม
invalid boolean aria-invalid ที่กลุ่ม และ data-invalid ที่กลุ่มและทุกตัวเลือก
Attribute เมื่อ
data-orientation มีเสมอ: vertical หรือ horizontal
data-disabled ปิดใช้งาน
data-readonly อ่านอย่างเดียว
data-invalid ตั้ง invalid

RadioGroup.useRadioGroupContext() คืนสถานะของกลุ่ม (name, value, disabled, readOnly, invalid, select …) สำหรับสร้างตัวเลือกแบบอื่น

RadioGroup.Item

<input type="radio"> ที่ถูกเลือกเมื่อ value ตรงกับค่าของกลุ่ม รับ prop ของ input ได้ทุกตัว ยกเว้น type, name, checked และ defaultChecked มี aria-describedby เป็นของกลุ่มถ้าคุณไม่ได้ใส่เอง

Prop Type ค่าเริ่มต้น
value string ค่าของตัวเลือกนี้ (ต้องใส่)
disabled boolean ตัวเลือกนี้ตัวเดียว กลุ่มที่ปิดใช้งานจะปิดทุกตัวเลือก
Attribute เมื่อ
data-checked ถูกเลือก
data-disabled ตัวเลือกนี้หรือกลุ่มปิดใช้งาน
data-readonly กลุ่มอ่านอย่างเดียว
data-invalid กลุ่ม invalid

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

ปุ่ม ทำอะไร
Tab เข้ากลุ่มที่ตัวเลือกที่เลือกไว้ (ถ้าไม่มีก็ตัวแรก) และออกจากกลุ่มเมื่อกด Tab อีกครั้ง
↓ / → ไปตัวเลือกถัดไปและเลือก วนจากตัวสุดท้ายกลับไปตัวแรก
↑ / ← ไปตัวเลือกก่อนหน้าและเลือก วนจากตัวแรกไปตัวสุดท้าย
Space เลือกตัวเลือกที่โฟกัสอยู่

browser ให้ role ของ radio สถานะที่เลือก จุดหยุด Tab จุดเดียว และปุ่มลูกศร โดยข้ามตัวเลือกที่ปิดใช้งาน ส่วน part ตั้ง role="radiogroup", aria-required, aria-readonly, aria-disabled และ aria-invalid ที่กลุ่ม

ส่วนที่คุณเพิ่มเอง

  • ชื่อของกลุ่ม: aria-label หรือ aria-labelledby ที่ชี้ไปที่ label ที่มองเห็น
  • <label> ของแต่ละตัวเลือก
  • ข้อความช่วยเหลือและ error ผูกด้วย aria-describedby ที่ Root
  • style ของโฟกัสที่มองเห็นได้บนวงกลมที่คุณวาด (เพราะ input ถูกซ่อนไว้)