Headless Dropdown
part ของ Dropdown ให้พฤติกรรมเดียวกับ Dropdown แบบมี style แต่ไม่มี style เลย ได้แก่ ปุ่มที่มี role="combobox" ซึ่งเปิด listbox ใน top layer คีย์บอร์ด (ลูกศร, Home / End, Page Up / Page Down, พิมพ์ตัวอักษรเพื่อกระโดด) ไฮไลต์ผ่าน aria-activedescendant การปิดเมื่อคลิกข้างนอก และ hidden input ที่ส่งค่าไปกับฟอร์ม
ส่วนที่คุณทำเองคือ markup ข้อความ label ข้อความช่วยเหลือหรือ error แถว "กำลังโหลด…" และ style ทั้งหมด part ต่าง ๆ บอกสถานะผ่าน attribute data-* ให้คุณใช้ใส่ style
ติดตั้ง
ส่วน headless มากับแพ็กเกจเลย (ไม่มี item ของ shadcn; item ของ React + Tailwind สร้างบนมัน):
npm install @purpurabog/ui
import { Dropdown } from '@purpurabog/ui/headless';
วิธีใช้
import { Dropdown } from '@purpurabog/ui/headless';
const types = [
{ value: 'sale', label: 'ซื้อขาย' },
{ value: 'lease', label: 'เช่า' },
];
export default function Example() {
return (
<div className="grid max-w-xs gap-2">
<Dropdown.Root items={types} name="type">
<Dropdown.Label className="text-xs text-ink-muted">ประเภทสัญญา</Dropdown.Label>
<Dropdown.Trigger className="group flex h-10 w-full items-center justify-between gap-2 border-b border-border-strong-1 bg-field-1 px-4 text-start text-sm text-ink hover:bg-field-hover-1 focus-visible:outline-2 focus-visible:-outline-offset-2 focus-visible:outline-focus data-open:outline-2 data-open:-outline-offset-2 data-open:outline-focus">
<Dropdown.Value placeholder="เลือกประเภท" className="data-placeholder:text-ink-placeholder" />
<svg viewBox="0 0 16 16" aria-hidden="true" className="size-4 shrink-0 fill-current transition-transform group-data-open:rotate-180">
<path d="M8 11 3 6l.7-.7L8 9.6l4.3-4.3.7.7z" />
</svg>
</Dropdown.Trigger>
<Dropdown.Popup className="bg-layer-1 text-sm text-ink shadow-lg">
{types.map((t) => (
<Dropdown.Option key={t.value} value={t.value} className="cursor-pointer px-4 py-3 text-ink-muted hover:bg-layer-hover-1 data-highlighted:bg-layer-hover-1 data-selected:bg-layer-selected-1 data-selected:text-ink">
{t.label}
</Dropdown.Option>
))}
</Dropdown.Popup>
</Dropdown.Root>
</div>
);
}
items ของ Root คือข้อมูล ปุ่มคีย์บอร์ดเลื่อนไปตามข้อมูลนี้ และการพิมพ์ค้นจากชื่อในนี้ ส่วน Option ใน Popup คือสิ่งที่ผู้ใช้เห็นและคลิก ให้ render Option หนึ่งตัวต่อหนึ่งรายการใน items โดยใช้ value เดียวกัน
โครงสร้าง
<Dropdown.Root items={items}>
<Dropdown.Label />
<Dropdown.Trigger>
<Dropdown.Value />
</Dropdown.Trigger>
<Dropdown.Popup>
<Dropdown.Option value="…" />
<Dropdown.Group>
<Dropdown.GroupLabel />
<Dropdown.GroupOptions>
<Dropdown.Option value="…" />
</Dropdown.GroupOptions>
</Dropdown.Group>
</Dropdown.Popup>
</Dropdown.Root>
บรรทัดที่สอง
ใส่บรรทัดที่สองไว้ใน Option การพิมพ์เพื่อกระโดดค้นเฉพาะ label ไม่ค้น description
import { Dropdown } from '@purpurabog/ui/headless';
const types = [
{ value: 'sale', label: 'ซื้อขาย', description: 'ผู้ซื้อได้กรรมสิทธิ์ในทรัพย์' },
{ value: 'lease', label: 'เช่า', description: 'ค่าเช่ารายเดือน มีกำหนดเวลา' },
{ value: 'mortgage', label: 'จำนอง', description: 'ใช้ทรัพย์เป็นหลักประกันเงินกู้' },
];
export default function Example() {
return (
<div className="grid max-w-xs gap-2">
<Dropdown.Root items={types} name="type">
<Dropdown.Label className="text-xs text-ink-muted">ประเภทสัญญา</Dropdown.Label>
<Dropdown.Trigger className="group flex h-10 w-full items-center justify-between gap-2 border-b border-border-strong-1 bg-field-1 px-4 text-start text-sm text-ink hover:bg-field-hover-1 focus-visible:outline-2 focus-visible:-outline-offset-2 focus-visible:outline-focus data-open:outline-2 data-open:-outline-offset-2 data-open:outline-focus">
<Dropdown.Value placeholder="เลือกประเภท" className="data-placeholder:text-ink-placeholder" />
<svg viewBox="0 0 16 16" aria-hidden="true" className="size-4 shrink-0 fill-current transition-transform group-data-open:rotate-180">
<path d="M8 11 3 6l.7-.7L8 9.6l4.3-4.3.7.7z" />
</svg>
</Dropdown.Trigger>
<Dropdown.Popup className="bg-layer-1 text-sm text-ink shadow-lg">
{types.map((t) => (
<Dropdown.Option key={t.value} value={t.value} className="flex cursor-pointer flex-col px-4 py-2 text-ink-muted hover:bg-layer-hover-1 data-highlighted:bg-layer-hover-1 data-selected:bg-layer-selected-1 data-selected:text-ink">
{t.label}
<small className="text-xs text-ink-helper">{t.description}</small>
</Dropdown.Option>
))}
</Dropdown.Popup>
</Dropdown.Root>
</div>
);
}
กลุ่มและรายการที่ปิดใช้งาน
items รับรายการเป็น { value, label, description?, disabled? } และกลุ่มเป็น { label, options, disabled? } รายการที่ disabled ยังแสดงอยู่ แต่ปุ่มคีย์บอร์ดข้ามไปและเลือกไม่ได้ กลุ่มที่ disabled ทำให้ทุกรายการในกลุ่ม disabled ด้วย isGroup ใช้แยกกลุ่มออกจากรายการ และ flattenOptions คืนทุกรายการตามลำดับ
import { Dropdown, isGroup, type ListItem } from '@purpurabog/ui/headless';
const branches: ListItem[] = [
{ label: 'กรุงเทพฯ', options: [
{ value: 'silom', label: 'สีลม', description: 'จันทร์–ศุกร์ 9:00–17:00' },
{ value: 'asok', label: 'อโศก', description: 'ทุกวัน 10:00–20:00' },
] },
{ label: 'ภาคเหนือ', options: [
{ value: 'chiangmai', label: 'เชียงใหม่' },
{ value: 'lampang', label: 'ลำปาง (ปิดแล้ว)', disabled: true },
] },
];
export default function Example() {
return (
<div className="grid max-w-xs gap-2">
<Dropdown.Root items={branches} name="branch">
<Dropdown.Label className="text-xs text-ink-muted">สาขา</Dropdown.Label>
<Dropdown.Trigger className="group flex h-10 w-full items-center justify-between gap-2 border-b border-border-strong-1 bg-field-1 px-4 text-start text-sm text-ink hover:bg-field-hover-1 focus-visible:outline-2 focus-visible:-outline-offset-2 focus-visible:outline-focus data-open:outline-2 data-open:-outline-offset-2 data-open:outline-focus">
<Dropdown.Value placeholder="เลือกสาขา" className="data-placeholder:text-ink-placeholder" />
<svg viewBox="0 0 16 16" aria-hidden="true" className="size-4 shrink-0 fill-current transition-transform group-data-open:rotate-180">
<path d="M8 11 3 6l.7-.7L8 9.6l4.3-4.3.7.7z" />
</svg>
</Dropdown.Trigger>
<Dropdown.Popup className="max-h-72 overflow-y-auto bg-layer-1 text-sm text-ink shadow-lg">
{branches.filter(isGroup).map((group) => (
<Dropdown.Group key={group.label}>
<Dropdown.GroupLabel className="px-4 pt-3 pb-1 text-xs font-semibold text-ink-muted">{group.label}</Dropdown.GroupLabel>
<Dropdown.GroupOptions>
{group.options.map((o) => (
<Dropdown.Option key={o.value} value={o.value} className="flex cursor-pointer flex-col px-4 py-2 text-ink-muted hover:bg-layer-hover-1 data-highlighted:bg-layer-hover-1 data-selected:bg-layer-selected-1 data-selected:text-ink data-disabled:cursor-not-allowed data-disabled:bg-transparent data-disabled:text-ink-disabled">
{o.label}
<small className="text-xs text-ink-helper">{o.description}</small>
</Dropdown.Option>
))}
</Dropdown.GroupOptions>
</Dropdown.Group>
))}
</Dropdown.Popup>
</Dropdown.Root>
</div>
);
}
ค่า
เก็บค่าเองด้วย value คู่กับ onValueChange หรือให้ค่าเริ่มต้นด้วย defaultValue แล้วให้ Root เก็บไว้ onValueChange ยิงเฉพาะเมื่อผู้ใช้เลือกรายการอื่น
import { useState } from 'react';
import { Dropdown } from '@purpurabog/ui/headless';
const types = [
{ value: 'sale', label: 'ซื้อขาย' },
{ value: 'lease', label: 'เช่า' },
{ value: 'mortgage', label: 'จำนอง' },
];
export default function Example() {
const [type, setType] = useState('lease');
const [sent, setSent] = useState<Record<string, FormDataEntryValue>>();
return (
<form
className="grid max-w-xs gap-2"
onSubmit={(e) => {
e.preventDefault();
setSent(Object.fromEntries(new FormData(e.currentTarget)));
}}
>
<Dropdown.Root items={types} name="type" value={type} onValueChange={setType}>
<Dropdown.Label className="text-xs text-ink-muted">ประเภทสัญญา</Dropdown.Label>
<Dropdown.Trigger className="group flex h-10 w-full items-center justify-between gap-2 border-b border-border-strong-1 bg-field-1 px-4 text-start text-sm text-ink hover:bg-field-hover-1 focus-visible:outline-2 focus-visible:-outline-offset-2 focus-visible:outline-focus data-open:outline-2 data-open:-outline-offset-2 data-open:outline-focus">
<Dropdown.Value placeholder="เลือกประเภท" className="data-placeholder:text-ink-placeholder" />
<svg viewBox="0 0 16 16" aria-hidden="true" className="size-4 shrink-0 fill-current transition-transform group-data-open:rotate-180">
<path d="M8 11 3 6l.7-.7L8 9.6l4.3-4.3.7.7z" />
</svg>
</Dropdown.Trigger>
<Dropdown.Popup className="bg-layer-1 text-sm text-ink shadow-lg">
{types.map((t) => (
<Dropdown.Option key={t.value} value={t.value} className="cursor-pointer px-4 py-3 text-ink-muted hover:bg-layer-hover-1 data-highlighted:bg-layer-hover-1 data-selected:bg-layer-selected-1 data-selected:text-ink">
{t.label}
</Dropdown.Option>
))}
</Dropdown.Popup>
</Dropdown.Root>
<p className="text-xs text-ink-muted">ค่า: {type || '(ไม่มี)'}</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={() => setType('')} 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>
);
}
ถ้ามี name จะมี hidden input ส่งค่าไปกับฟอร์ม การ reset ฟอร์มคืนค่าแบบ uncontrolled กลับเป็น defaultValue ส่วน value แบบ controlled ให้ตั้งกลับเอง
เปิดขึ้นด้านบน
รายการเปิดลงด้านล่าง trigger และกว้างเท่ากับ trigger side="top" ที่ Popup ให้เปิดขึ้นด้านบนก่อน ทั้งสองแบบจะพลิกเองเมื่อที่ไม่พอ และ data-side ของ popup บอกว่าไปอยู่ด้านไหน ไม่มีขนาดให้เลือก ความสูงของ trigger และแถวเป็น CSS ของคุณ
import { Dropdown } from '@purpurabog/ui/headless';
const types = [
{ value: 'sale', label: 'ซื้อขาย' },
{ value: 'lease', label: 'เช่า' },
{ value: 'mortgage', label: 'จำนอง' },
];
export default function Example() {
return (
<div className="grid max-w-xs gap-2">
<Dropdown.Root items={types} name="type">
<Dropdown.Label className="text-xs text-ink-muted">ประเภทสัญญา</Dropdown.Label>
<Dropdown.Trigger className="group flex h-10 w-full items-center justify-between gap-2 border-b border-border-strong-1 bg-field-1 px-4 text-start text-sm text-ink hover:bg-field-hover-1 focus-visible:outline-2 focus-visible:-outline-offset-2 focus-visible:outline-focus data-open:outline-2 data-open:-outline-offset-2 data-open:outline-focus">
<Dropdown.Value placeholder="เลือกประเภท" className="data-placeholder:text-ink-placeholder" />
<svg viewBox="0 0 16 16" aria-hidden="true" className="size-4 shrink-0 fill-current transition-transform group-data-open:rotate-180">
<path d="M8 11 3 6l.7-.7L8 9.6l4.3-4.3.7.7z" />
</svg>
</Dropdown.Trigger>
{/* data-side บอกว่ารายการไปเปิดที่ไหน: เงาจึงหันออกจากช่อง */}
<Dropdown.Popup side="top" className="bg-layer-1 text-sm text-ink shadow-lg data-[side=top]:shadow-[0_-2px_6px_rgb(0_0_0/0.3)]">
{types.map((t) => (
<Dropdown.Option key={t.value} value={t.value} className="cursor-pointer px-4 py-3 text-ink-muted hover:bg-layer-hover-1 data-highlighted:bg-layer-hover-1 data-selected:bg-layer-selected-1 data-selected:text-ink">
{t.label}
</Dropdown.Option>
))}
</Dropdown.Popup>
</Dropdown.Root>
</div>
);
}
โหลดรายการตอนเปิดรายการ
onOpenChange ยิงหลังรายการเปิดหรือปิด ระหว่างที่ loading เป็น true ปุ่มคีย์บอร์ดเลือกอะไรไม่ได้ และ popup มี aria-busy เมื่อ loading จบตอนรายการเปิดอยู่ ไฮไลต์จะไปที่รายการที่เลือกไว้ ถ้าไม่มีก็รายการแรก
Root ไม่วาดแถว "กำลังโหลด…" และไม่ประกาศอะไร ให้ render แถวนั้นและข้อความ status เอง
import { useState } from 'react';
import { Dropdown, type ListOption } from '@purpurabog/ui/headless';
// แทน fetch('/api/branches'): ตอบกลับหลังผ่านไปหนึ่งวินาที
const fetchBranches = () =>
new Promise<ListOption[]>((resolve) => {
setTimeout(() => resolve([
{ value: 'silom', label: 'สีลม' },
{ value: 'asok', label: 'อโศก' },
{ value: 'chiangmai', label: 'เชียงใหม่' },
]), 1000);
});
export default function Example() {
const [branches, setBranches] = useState<ListOption[]>();
const load = async (open: boolean) => {
if (open && !branches) setBranches(await fetchBranches());
};
return (
<div className="grid max-w-xs gap-2">
<Dropdown.Root items={branches ?? []} name="branch" loading={!branches} onOpenChange={load}>
<Dropdown.Label className="text-xs text-ink-muted">สาขา</Dropdown.Label>
<Dropdown.Trigger className="group flex h-10 w-full items-center justify-between gap-2 border-b border-border-strong-1 bg-field-1 px-4 text-start text-sm text-ink hover:bg-field-hover-1 focus-visible:outline-2 focus-visible:-outline-offset-2 focus-visible:outline-focus data-open:outline-2 data-open:-outline-offset-2 data-open:outline-focus">
<Dropdown.Value placeholder="เลือกสาขา" className="data-placeholder:text-ink-placeholder" />
<svg viewBox="0 0 16 16" aria-hidden="true" className="size-4 shrink-0 fill-current transition-transform group-data-open:rotate-180">
<path d="M8 11 3 6l.7-.7L8 9.6l4.3-4.3.7.7z" />
</svg>
</Dropdown.Trigger>
<Dropdown.Popup className="bg-layer-1 text-sm text-ink shadow-lg">
{branches?.map((b) => (
<Dropdown.Option key={b.value} value={b.value} className="cursor-pointer px-4 py-3 text-ink-muted hover:bg-layer-hover-1 data-highlighted:bg-layer-hover-1 data-selected:bg-layer-selected-1 data-selected:text-ink">
{b.label}
</Dropdown.Option>
))}
{branches ? null : <li role="option" aria-disabled="true" aria-selected="false" className="px-4 py-3 text-ink-placeholder">กำลังโหลด…</li>}
</Dropdown.Popup>
<span role="status" className="sr-only">{branches ? '' : 'กำลังโหลด…'}</span>
</Dropdown.Root>
</div>
);
}
อ่านอย่างเดียวและปิดใช้งาน
readOnly แสดงค่า แต่เปิดรายการไม่ได้และปุ่มคีย์บอร์ดไม่ทำอะไร trigger ได้ aria-readonly
disabled ปิดใช้งาน trigger
- ช่องที่ปิดใช้งานไม่ render hidden input จึงไม่ถูกส่งไปกับฟอร์ม (เหมือน control ปกติที่ปิดใช้งาน)
import { Dropdown } from '@purpurabog/ui/headless';
const types = [
{ value: 'sale', label: 'ซื้อขาย' },
{ value: 'lease', label: 'เช่า' },
];
function TypeField({ label, ...props }: { label: string; defaultValue?: string; readOnly?: boolean; disabled?: boolean }) {
return (
<Dropdown.Root items={types} {...props}>
<Dropdown.Label className="text-xs text-ink-muted data-disabled:text-ink-disabled">{label}</Dropdown.Label>
<Dropdown.Trigger className="group flex h-10 w-full items-center justify-between gap-2 border-b border-border-strong-1 bg-field-1 px-4 text-start text-sm text-ink hover:bg-field-hover-1 focus-visible:outline-2 focus-visible:-outline-offset-2 focus-visible:outline-focus data-readonly:cursor-default data-readonly:border-hairline data-readonly:bg-transparent data-disabled:cursor-not-allowed data-disabled:border-transparent data-disabled:text-ink-disabled data-disabled:hover:bg-field-1">
<Dropdown.Value placeholder="เลือกประเภท" className="data-placeholder:text-ink-placeholder" />
<svg viewBox="0 0 16 16" aria-hidden="true" className="size-4 shrink-0 fill-current group-data-readonly:opacity-25">
<path d="M8 11 3 6l.7-.7L8 9.6l4.3-4.3.7.7z" />
</svg>
</Dropdown.Trigger>
<Dropdown.Popup className="bg-layer-1 text-sm text-ink shadow-lg">
{types.map((t) => (
<Dropdown.Option key={t.value} value={t.value} className="px-4 py-3">{t.label}</Dropdown.Option>
))}
</Dropdown.Popup>
</Dropdown.Root>
);
}
export default function Example() {
return (
<div className="grid max-w-xs gap-2">
<TypeField label="ประเภทสัญญา (อ่านอย่างเดียว)" defaultValue="lease" readOnly />
<TypeField label="ประเภทสัญญา (ปิดใช้งาน)" disabled />
</div>
);
}
required และ invalid ตั้งแค่ aria-required และ aria-invalid (กับ data-invalid) ไม่มีข้อความในตัว ให้ตรวจค่าและแสดง error เอง (ฟอร์มและการตรวจค่า)
ใส่ style
import { useState } from 'react';
import { Dropdown } from '@purpurabog/ui/headless';
const types = [
{ value: 'sale', label: 'ซื้อขาย' },
{ value: 'lease', label: 'เช่า' },
{ value: 'mortgage', label: 'จำนอง (งดชั่วคราว)', disabled: true },
];
export default function Example() {
const [type, setType] = useState('');
const invalid = type === '';
return (
<div className="grid max-w-xs gap-2">
<Dropdown.Root items={types} name="type" value={type} onValueChange={setType} invalid={invalid} required>
<Dropdown.Label className="text-xs text-ink-muted">ประเภทสัญญา</Dropdown.Label>
<Dropdown.Trigger
aria-describedby={invalid ? 'dropdown-style-error' : undefined}
className="group flex h-10 w-full items-center justify-between gap-2 border-b border-border-strong-1 bg-field-1 px-4 text-start text-sm text-ink hover:bg-field-hover-1 focus-visible:outline-2 focus-visible:-outline-offset-2 focus-visible:outline-focus data-open:outline-2 data-open:-outline-offset-2 data-open:outline-focus data-invalid:outline-2 data-invalid:-outline-offset-2 data-invalid:outline-error data-disabled:text-ink-disabled"
>
<Dropdown.Value placeholder="เลือกประเภท" className="data-placeholder:text-ink-placeholder" />
<svg viewBox="0 0 16 16" aria-hidden="true" className="size-4 shrink-0 fill-current transition-transform group-data-open:rotate-180">
<path d="M8 11 3 6l.7-.7L8 9.6l4.3-4.3.7.7z" />
</svg>
</Dropdown.Trigger>
<Dropdown.Popup className="bg-layer-1 text-sm text-ink shadow-lg">
{types.map((t) => (
<Dropdown.Option key={t.value} value={t.value} className="cursor-pointer px-4 py-3 text-ink-muted hover:bg-layer-hover-1 data-highlighted:bg-layer-hover-1 data-selected:bg-layer-selected-1 data-selected:font-semibold data-selected:text-ink data-disabled:cursor-not-allowed data-disabled:bg-transparent data-disabled:text-ink-disabled">
{t.label}
</Dropdown.Option>
))}
</Dropdown.Popup>
</Dropdown.Root>
{invalid ? <p id="dropdown-style-error" className="text-xs text-error-text">เลือกประเภทสัญญา</p> : null}
</div>
);
}
/* className="trigger" ที่ Trigger, "value" ที่ Value, "option" ที่แต่ละ Option */
.trigger[data-open] { outline: 2px solid; }
.trigger[data-invalid] { outline: 2px solid #da1e28; }
.value[data-placeholder] { color: #6f6f6f; }
.option[data-highlighted] { background: #e8e8e8; }
.option[data-selected] { font-weight: 600; }
.option[data-disabled] { color: #a8a8a8; }
Parts
Dropdown.Root
ไม่ render element ของตัวเอง render แค่ children และ hidden input เมื่อมี name และไม่ได้ disabled
| Prop |
Type |
ค่าเริ่มต้น |
|
items |
ListItem[] |
|
รายการและกลุ่ม { label, options } ที่ปุ่มคีย์บอร์ดเลื่อนไปและการพิมพ์ค้นหา |
value |
string |
|
ค่าที่เลือก (controlled) |
defaultValue |
string |
'' |
ค่าเริ่มต้น (uncontrolled) |
onValueChange |
(value: string) => void |
|
หลังผู้ใช้เลือกรายการอื่น |
onOpenChange |
(open: boolean) => void |
|
หลังรายการเปิดหรือปิด |
disabled |
boolean |
false |
ปิดใช้งาน trigger |
readOnly |
boolean |
false |
เปิดรายการไม่ได้ |
required |
boolean |
false |
aria-required ที่ trigger |
invalid |
boolean |
false |
aria-invalid และ data-invalid ที่ trigger |
loading |
boolean |
false |
ปุ่มคีย์บอร์ดเลือกอะไรไม่ได้ popup เป็น aria-busy |
name |
string |
|
ส่งค่าไปกับฟอร์ม (hidden input) ยกเว้นตอนปิดใช้งาน |
id |
string |
สร้างให้ |
ฐานของ id: <id>-label, <id>-value, <id>-list, <id>-opt-<n> |
Dropdown.useDropdownContext() คืนสถานะของ Root (open, value, selected, active, loading …) ให้ component ที่อยู่ข้างใน เช่น ใช้หมุนลูกศร
Dropdown.Label
<span> ที่มี id <id>-label ใช้เป็นชื่อของ trigger (คู่กับ Value) และของ popup
| Attribute |
เมื่อ |
data-disabled |
Root ปิดใช้งาน |
Dropdown.Trigger
<button type="button" role="combobox"> ที่มี aria-labelledby เป็น <id>-label <id>-value ถ้าคุณไม่ได้ใส่เอง รับ prop ของ button ได้ทุกตัว ยกเว้น type, role และ disabled
| Attribute |
เมื่อ |
data-open |
รายการเปิดอยู่ |
data-disabled |
ปิดใช้งาน |
data-readonly |
อ่านอย่างเดียว |
data-invalid |
ตั้ง invalid |
data-placeholder |
ยังไม่ได้เลือกอะไร |
Dropdown.Value
<span> ที่มี id <id>-value แสดงชื่อของรายการที่เลือก ถ้าไม่มีก็แสดง placeholder ส่วน children ใช้แทนทั้งสองอย่าง
| Prop |
Type |
ค่าเริ่มต้น |
|
placeholder |
ReactNode |
|
แสดงระหว่างที่ยังไม่ได้เลือก |
| Attribute |
เมื่อ |
data-placeholder |
ยังไม่ได้เลือกอะไร |
<ul role="listbox" popover="manual"> ใน top layer กว้างเท่ากับ trigger children จะ render เฉพาะตอนรายการเปิด การกดข้างในทำให้โฟกัสยังอยู่ที่ trigger การกดข้างนอกจะปิดรายการ aria-labelledby เป็น <id>-label ถ้าคุณไม่ได้ใส่เอง
| Prop |
Type |
ค่าเริ่มต้น |
|
side |
'bottom' | 'top' |
'bottom' |
ด้านที่รายการเปิด จะพลิกเองเมื่อที่ไม่พอ |
| Attribute |
เมื่อ |
data-open |
รายการเปิดอยู่ |
data-side |
bottom หรือ top คือด้านที่รายการไปอยู่ |
Dropdown.Option
<li role="option"> ที่มี id <id>-opt-<n> คลิกรายการที่ใช้งานได้จะเลือกรายการนั้นแล้วปิดรายการ
| Prop |
Type |
ค่าเริ่มต้น |
|
value |
string |
|
ค่าของรายการหนึ่งใน items (ต้องใส่) |
| Attribute |
เมื่อ |
data-highlighted |
ไฮไลต์ของคีย์บอร์ดอยู่ที่รายการนี้ |
data-selected |
เป็นรายการที่เลือกไว้ |
data-disabled |
รายการนี้ (หรือกลุ่มของมัน) ปิดใช้งาน |
Dropdown.Group, GroupLabel, GroupOptions
Group คือ <div role="group"> อยู่ใน <li> ที่เป็น presentation ใช้ GroupLabel (<div>) เป็นชื่อ ส่วน GroupOptions คือ <ul> ที่เป็น presentation ใช้ใส่ Option ของกลุ่ม ทั้งสามไม่มี prop และ data attribute เพิ่ม
คีย์บอร์ดและ accessibility
| ปุ่ม |
ตอนรายการปิด |
ตอนรายการเปิด |
| ↓, Enter, Space |
เปิดที่รายการที่เลือกไว้ (ถ้าไม่มีก็รายการแรก) |
↓ เลื่อนลง ส่วน Enter และ Space เลือกรายการที่ไฮไลต์แล้วปิด |
| ↑ |
ไม่ทำอะไร |
เลื่อนขึ้น (ไม่วนกลับ) |
| Home / End |
เปิดที่รายการแรก / สุดท้าย |
ไปรายการแรก / สุดท้าย |
| Page Up / Page Down |
|
เลื่อนทีละ 10 |
| ตัวอักษร |
เปิดที่รายการถัดไปที่ขึ้นต้นด้วยตัวอักษรนั้น |
กระโดดไปรายการถัดไปที่ขึ้นต้นด้วยตัวอักษรนั้น |
| Alt+↑ |
|
เลือกรายการที่ไฮไลต์แล้วปิด |
| Esc |
|
ปิดโดยไม่เปลี่ยนค่า |
| Tab |
ไปช่องถัดไป |
ปิดโดยไม่เปลี่ยนค่าแล้วไปช่องถัดไป |
part ต่าง ๆ ตั้ง role="combobox", aria-haspopup, aria-expanded, aria-controls, aria-activedescendant, aria-readonly, aria-required และ aria-invalid ที่ trigger และตั้ง role="listbox", role="option", aria-selected และ aria-disabled ที่รายการ โฟกัสอยู่ที่ trigger ตลอด
ส่วนที่คุณเพิ่มเอง
- ข้อความ label ใน
Label หรือใส่ aria-labelledby ของคุณเองที่ trigger และ popup
- ข้อความช่วยเหลือและ error ผูกด้วย
aria-describedby ที่ trigger
- ข้อความ
role="status" สำหรับ "กำลังโหลด…" ตามตัวอย่างด้านบน