Headless Combobox
part ของ Combobox ให้พฤติกรรมเดียวกับ Combobox แบบมี style แต่ไม่มี style เลย ได้แก่ ช่องพิมพ์ที่มี role="combobox" listbox ใน top layer ที่การพิมพ์จะกรองหรือแค่ไฮไลต์ คีย์บอร์ด กติกาว่าออกจากช่องแล้วเก็บค่าอะไร ปุ่มล้างค่าและปุ่มลูกศรที่ไม่อยู่ในลำดับ Tab และ hidden input ที่ส่งค่าไปกับฟอร์ม
ส่วนที่คุณทำเองคือ markup, <label> ข้อความช่วยเหลือหรือ error ข้อความ "กำลังค้นหา…" และ "ไม่พบรายการที่ตรงกัน" และ style ทั้งหมด part ต่าง ๆ บอกสถานะผ่าน attribute data-* ให้คุณใช้ใส่ style
ติดตั้ง
ส่วน headless มากับแพ็กเกจเลย (ไม่มี item ของ shadcn; item ของ React + Tailwind สร้างบนมัน):
npm install @purpurabog/ui
import { Combobox } from '@purpurabog/ui/headless';
วิธีใช้
import { Combobox } from '@purpurabog/ui/headless';
const provinces = [
{ value: 'bkk', label: 'กรุงเทพมหานคร' },
{ value: 'cnx', label: 'เชียงใหม่' },
{ value: 'cei', label: 'เชียงราย' },
{ value: 'kkc', label: 'ขอนแก่น' },
{ value: 'hkt', label: 'ภูเก็ต' },
];
export default function Example() {
return (
<div className="grid max-w-xs gap-2">
<Combobox.Root items={provinces} name="province" filter="contains">
<label htmlFor="province" className="text-xs text-ink-muted">จังหวัด</label>
<Combobox.Anchor className="flex h-10 items-center border-b border-border-strong-1 bg-field-1 hover:bg-field-hover-1 focus-within:outline-2 focus-within:-outline-offset-2 focus-within:outline-focus">
<Combobox.Input id="province" placeholder="พิมพ์ชื่อจังหวัด" className="h-full min-w-0 flex-1 bg-transparent ps-4 text-sm text-ink outline-none placeholder:text-ink-placeholder" />
<Combobox.Toggle aria-label="เปิด" className="group grid h-full w-10 shrink-0 place-items-center text-ink">
<svg viewBox="0 0 16 16" aria-hidden="true" className="size-4 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>
</Combobox.Toggle>
</Combobox.Anchor>
<Combobox.Popup aria-label="จังหวัด" className="max-h-72 overflow-y-auto bg-layer-1 text-sm text-ink shadow-lg">
{(items) => items.map((p) => ('options' in p ? null : (
<Combobox.Option key={p.value} value={p.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">
{p.label}
</Combobox.Option>
)))}
</Combobox.Popup>
</Combobox.Root>
</div>
);
}
items ของ Root คือทุกรายการ Popup เรียกฟังก์ชัน children ด้วยรายการที่ข้อความที่พิมพ์ยังเก็บไว้ (กลุ่มยังอยู่) คุณจึง render เฉพาะรายการเหล่านั้น
โครงสร้าง
<Combobox.Root items={items}>
<label />
<Combobox.Anchor>
<Combobox.Input />
<Combobox.Clear />
<Combobox.Toggle />
</Combobox.Anchor>
<Combobox.Popup>
{(items) => (
<>
<Combobox.Option value="…" />
<Combobox.Group>
<Combobox.GroupLabel />
<Combobox.GroupOptions>
<Combobox.Option value="…" />
</Combobox.GroupOptions>
</Combobox.Group>
</>
)}
</Combobox.Popup>
</Combobox.Root>
การพิมพ์กรองรายการอย่างไร
การพิมพ์จะเปิดรายการและไฮไลต์รายการแรกที่ใช้งานได้และชื่อมีข้อความนั้นอยู่ filter กำหนดว่ารายการอื่นจะยังอยู่หรือไม่
filter |
รายการระหว่างพิมพ์ |
none (ค่าเริ่มต้น) |
ทุกรายการยังอยู่ มีแค่ไฮไลต์ที่ย้าย |
contains |
เหลือเฉพาะรายการที่ชื่อมีข้อความนั้นอยู่ |
starts-with |
เหลือเฉพาะรายการที่ชื่อขึ้นต้นด้วยข้อความนั้น |
ระหว่างที่ข้อความยังตรงกับชื่อของรายการที่เลือกไว้ รายการจะแสดงครบทุกรายการ
import { useState } from 'react';
import { Combobox } from '@purpurabog/ui/headless';
type Filter = 'none' | 'contains' | 'starts-with';
const provinces = [
{ value: 'cnx', label: 'เชียงใหม่' },
{ value: 'cei', label: 'เชียงราย' },
{ value: 'msn', label: 'แม่ฮ่องสอน' },
{ value: 'lpn', label: 'ลำพูน' },
{ value: 'nan', label: 'น่าน' },
];
export default function Example() {
const [filter, setFilter] = useState<Filter>('starts-with');
return (
<div className="grid max-w-xs gap-4">
<label className="grid gap-2 text-xs text-ink-muted">
filter
<select
value={filter}
onChange={(e) => setFilter(e.target.value as Filter)}
className="h-8 border-b border-border-strong-1 bg-field-1 px-2 text-sm text-ink focus-visible:outline-2 focus-visible:-outline-offset-2 focus-visible:outline-focus"
>
<option value="none">none</option>
<option value="contains">contains</option>
<option value="starts-with">starts-with</option>
</select>
</label>
<div className="grid gap-2">
<Combobox.Root items={provinces} filter={filter}>
<label htmlFor="province-filter" className="text-xs text-ink-muted">จังหวัด</label>
<Combobox.Anchor className="flex h-10 items-center border-b border-border-strong-1 bg-field-1 hover:bg-field-hover-1 focus-within:outline-2 focus-within:-outline-offset-2 focus-within:outline-focus">
<Combobox.Input id="province-filter" placeholder="ลองพิมพ์ “ราย” หรือ “น”" className="h-full min-w-0 flex-1 bg-transparent ps-4 text-sm text-ink outline-none placeholder:text-ink-placeholder" />
<Combobox.Toggle aria-label="เปิด" className="group grid h-full w-10 shrink-0 place-items-center text-ink">
<svg viewBox="0 0 16 16" aria-hidden="true" className="size-4 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>
</Combobox.Toggle>
</Combobox.Anchor>
<Combobox.Popup aria-label="จังหวัด" className="max-h-72 overflow-y-auto bg-layer-1 text-sm text-ink shadow-lg">
{(items) => items.map((p) => ('options' in p ? null : (
<Combobox.Option key={p.value} value={p.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">
{p.label}
</Combobox.Option>
)))}
</Combobox.Popup>
</Combobox.Root>
</div>
</div>
);
}
พิมพ์เองหรือเลือกจากรายการ
เมื่อผู้ใช้ออกจากช่อง ข้อความที่ตรงกับชื่อรายการที่ใช้งานได้ทุกตัวอักษรจะเลือกรายการนั้น ข้อความอื่นจะเด้งกลับเป็นรายการที่เลือกไว้ก่อนหน้า ถ้าตั้ง allowCustomValue ข้อความที่พิมพ์จะกลายเป็นค่าแทน
import { useState } from 'react';
import { Combobox } from '@purpurabog/ui/headless';
const titles = [
{ value: 'ทนายความ', label: 'ทนายความ' },
{ value: 'ผู้ช่วยทนาย', label: 'ผู้ช่วยทนาย' },
];
export default function Example() {
const [title, setTitle] = useState('');
return (
<div className="grid max-w-xs gap-2">
<Combobox.Root items={titles} name="title" allowCustomValue onValueChange={setTitle}>
<label htmlFor="job-title" className="text-xs text-ink-muted">ตำแหน่งงาน</label>
<Combobox.Anchor className="flex h-10 items-center border-b border-border-strong-1 bg-field-1 hover:bg-field-hover-1 focus-within:outline-2 focus-within:-outline-offset-2 focus-within:outline-focus">
<Combobox.Input id="job-title" placeholder="เลือกหรือพิมพ์เอง" className="h-full min-w-0 flex-1 bg-transparent ps-4 text-sm text-ink outline-none placeholder:text-ink-placeholder" />
<Combobox.Toggle aria-label="เปิด" className="group grid h-full w-10 shrink-0 place-items-center text-ink">
<svg viewBox="0 0 16 16" aria-hidden="true" className="size-4 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>
</Combobox.Toggle>
</Combobox.Anchor>
<Combobox.Popup aria-label="ตำแหน่งงาน" className="bg-layer-1 text-sm text-ink shadow-lg">
{(items) => items.map((t) => ('options' in t ? null : (
<Combobox.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}
</Combobox.Option>
)))}
</Combobox.Popup>
</Combobox.Root>
<p className="text-xs text-ink-muted">ค่า: {title || '(ไม่มี)'}</p>
</div>
);
}
ค่า
เก็บค่าเองด้วย value คู่กับ onValueChange หรือให้ค่าเริ่มต้นด้วย defaultValue onValueChange ยิงเมื่อค่าเปลี่ยน ไม่ว่าจากการเลือก ข้อความที่ยืนยันแล้ว หรือการล้างค่า เมื่อ value เปลี่ยนจากข้างนอก ช่องพิมพ์จะแสดงชื่อของรายการใหม่
import { useState } from 'react';
import { Combobox } from '@purpurabog/ui/headless';
const provinces = [
{ value: 'bkk', label: 'กรุงเทพมหานคร' },
{ value: 'cnx', label: 'เชียงใหม่' },
{ value: 'hkt', label: 'ภูเก็ต' },
];
export default function Example() {
const [province, setProvince] = useState('bkk');
return (
<div className="grid max-w-xs gap-2">
<Combobox.Root items={provinces} name="province" value={province} onValueChange={setProvince}>
<label htmlFor="province-value" className="text-xs text-ink-muted">จังหวัด</label>
<Combobox.Anchor className="flex h-10 items-center border-b border-border-strong-1 bg-field-1 hover:bg-field-hover-1 focus-within:outline-2 focus-within:-outline-offset-2 focus-within:outline-focus">
<Combobox.Input id="province-value" className="h-full min-w-0 flex-1 bg-transparent ps-4 text-sm text-ink outline-none placeholder:text-ink-placeholder" />
<Combobox.Toggle aria-label="เปิด" className="group grid h-full w-10 shrink-0 place-items-center text-ink">
<svg viewBox="0 0 16 16" aria-hidden="true" className="size-4 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>
</Combobox.Toggle>
</Combobox.Anchor>
<Combobox.Popup aria-label="จังหวัด" className="bg-layer-1 text-sm text-ink shadow-lg">
{(items) => items.map((p) => ('options' in p ? null : (
<Combobox.Option key={p.value} value={p.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">
{p.label}
</Combobox.Option>
)))}
</Combobox.Popup>
</Combobox.Root>
<p className="text-xs text-ink-muted">ค่า: {province || '(ไม่มี)'}</p>
{/* เปลี่ยนจากภายนอก: ช่องข้อความแสดงชื่อของตัวเลือกใหม่ */}
<button type="button" onClick={() => setProvince('hkt')} className="h-8 justify-self-start 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>
);
}
ค้นหาบน server
onInputValueChange ได้ข้อความทุกครั้งที่ผู้ใช้พิมพ์ ให้ดึงผลที่ตรงกัน แทนที่ items และตั้ง loading ระหว่างรอ request ซึ่งทำให้ popup มี aria-busy ใช้ filter="none" (ค่าเริ่มต้น) เพราะ server กรองมาให้แล้ว
part ต่าง ๆ ไม่วาดข้อความ "กำลังค้นหา…" หรือ "ไม่พบรายการที่ตรงกัน" Combobox.useComboboxContext() ให้ open, loading และ listed (รายการที่ข้อความยังเก็บไว้) เพื่อ render ข้อความเหล่านั้นเอง
import { useRef, useState } from 'react';
import { Combobox, type ListOption } from '@purpurabog/ui/headless';
const all = ['กรุงเทพมหานคร', 'เชียงใหม่', 'เชียงราย', 'ขอนแก่น', 'นครราชสีมา', 'ภูเก็ต', 'สงขลา', 'อุดรธานี'];
// แทน server: เมืองที่มีข้อความนั้น ตอบกลับหลัง 600 ms
const search = (text: string) =>
new Promise<ListOption[]>((resolve) => {
const found = all.filter((city) => city.toLowerCase().includes(text.trim().toLowerCase()));
setTimeout(() => resolve(found.map((city) => ({ value: city, label: city }))), 600);
});
function statusText(loading: boolean, count: number) {
if (loading) return 'กำลังค้นหา…';
return count === 0 ? 'ไม่พบรายการที่ตรงกัน' : '';
}
function ListStatus() {
const { open, loading, listed } = Combobox.useComboboxContext();
return <p role="status" className="min-h-4 text-xs text-ink-muted">{open ? statusText(loading, listed.length) : ''}</p>;
}
export default function Example() {
const [cities, setCities] = useState<ListOption[]>([]);
const [loading, setLoading] = useState(false);
const latest = useRef('');
const onText = async (text: string) => {
latest.current = text;
setLoading(true);
const found = await search(text);
if (latest.current !== text) return; // มีการค้นหาที่ใหม่กว่ากำลังมา
setCities(found);
setLoading(false);
};
return (
<div className="grid max-w-xs gap-2">
<Combobox.Root items={cities} name="city" loading={loading} onInputValueChange={onText}>
<label htmlFor="city" className="text-xs text-ink-muted">เมือง</label>
<Combobox.Anchor className="flex h-10 items-center border-b border-border-strong-1 bg-field-1 hover:bg-field-hover-1 focus-within:outline-2 focus-within:-outline-offset-2 focus-within:outline-focus">
<Combobox.Input id="city" placeholder="พิมพ์เพื่อค้นหา" className="h-full min-w-0 flex-1 bg-transparent px-4 text-sm text-ink outline-none placeholder:text-ink-placeholder" />
</Combobox.Anchor>
<Combobox.Popup aria-label="เมือง" className="max-h-72 overflow-y-auto bg-layer-1 text-sm text-ink shadow-lg">
{(items) => items.map((c) => ('options' in c ? null : (
<Combobox.Option key={c.value} value={c.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">
{c.label}
</Combobox.Option>
)))}
</Combobox.Popup>
<ListStatus />
</Combobox.Root>
</div>
);
}
ในตัวอย่างนี้ search() ทำหน้าที่แทน server และตอบกลับหลังรอสักครู่ ในแอปจริงจะดึงผลที่ตรงกัน เหมือนในหน้า Combobox
กลุ่มและคำอธิบาย
กลุ่มคือ { label, options } ใน items และรายการรับ description และ disabled ได้ การพิมพ์ค้นเฉพาะชื่อรายการ ใช้ isGroup แยกกลุ่มออกจากรายการในฟังก์ชันของ Popup
import { Combobox, 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">
<Combobox.Root items={branches} name="branch" filter="contains">
<label htmlFor="branch" className="text-xs text-ink-muted">สาขา</label>
<Combobox.Anchor className="flex h-10 items-center border-b border-border-strong-1 bg-field-1 hover:bg-field-hover-1 focus-within:outline-2 focus-within:-outline-offset-2 focus-within:outline-focus">
<Combobox.Input id="branch" placeholder="พิมพ์ชื่อสาขา" className="h-full min-w-0 flex-1 bg-transparent ps-4 text-sm text-ink outline-none placeholder:text-ink-placeholder" />
<Combobox.Toggle aria-label="เปิด" className="group grid h-full w-10 shrink-0 place-items-center text-ink">
<svg viewBox="0 0 16 16" aria-hidden="true" className="size-4 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>
</Combobox.Toggle>
</Combobox.Anchor>
<Combobox.Popup aria-label="สาขา" className="max-h-72 overflow-y-auto bg-layer-1 text-sm text-ink shadow-lg">
{(items) => items.filter(isGroup).map((group) => (
<Combobox.Group key={group.label}>
<Combobox.GroupLabel className="px-4 pt-3 pb-1 text-xs font-semibold text-ink-muted">{group.label}</Combobox.GroupLabel>
<Combobox.GroupOptions>
{group.options.map((o) => (
<Combobox.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>
</Combobox.Option>
))}
</Combobox.GroupOptions>
</Combobox.Group>
))}
</Combobox.Popup>
</Combobox.Root>
</div>
);
}
ปุ่มล้างค่า
Combobox.Clear ล้างข้อความและค่า แล้วให้โฟกัสอยู่ในช่องพิมพ์ ถ้าไม่ต้องการก็ไม่ต้องใส่ ปุ่มนี้ถูกปิดใช้งานเมื่อ Root ปิดใช้งานหรืออ่านอย่างเดียว ส่วน Combobox แบบมี style ทำมากกว่านั้น คือแสดงปุ่มนี้เฉพาะตอนที่ช่องเปลี่ยนค่าได้และมีข้อความอยู่
import { Combobox } from '@purpurabog/ui/headless';
const provinces = [
{ value: 'bkk', label: 'กรุงเทพมหานคร' },
{ value: 'cnx', label: 'เชียงใหม่' },
{ value: 'hkt', label: 'ภูเก็ต' },
];
function ClearButton() {
const { interactive, text } = Combobox.useComboboxContext();
return interactive && text !== '' ? (
<Combobox.Clear aria-label="ล้างค่า" className="grid h-full w-8 shrink-0 place-items-center text-ink hover:bg-field-hover-1">
<svg viewBox="0 0 16 16" aria-hidden="true" className="size-4 fill-current">
<path d="M12 4.7 11.3 4 8 7.3 4.7 4 4 4.7 7.3 8 4 11.3l.7.7L8 8.7l3.3 3.3.7-.7L8.7 8z" />
</svg>
</Combobox.Clear>
) : null;
}
export default function Example() {
return (
<div className="grid max-w-xs gap-2">
<Combobox.Root items={provinces} name="province" defaultValue="bkk">
<label htmlFor="province-clear" className="text-xs text-ink-muted">จังหวัด</label>
<Combobox.Anchor className="flex h-10 items-center border-b border-border-strong-1 bg-field-1 hover:bg-field-hover-1 focus-within:outline-2 focus-within:-outline-offset-2 focus-within:outline-focus">
<Combobox.Input id="province-clear" placeholder="พิมพ์ชื่อจังหวัด" className="h-full min-w-0 flex-1 bg-transparent ps-4 text-sm text-ink outline-none placeholder:text-ink-placeholder" />
<ClearButton />
<Combobox.Toggle aria-label="เปิด" className="group grid h-full w-10 shrink-0 place-items-center text-ink">
<svg viewBox="0 0 16 16" aria-hidden="true" className="size-4 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>
</Combobox.Toggle>
</Combobox.Anchor>
<Combobox.Popup aria-label="จังหวัด" className="bg-layer-1 text-sm text-ink shadow-lg">
{(items) => items.map((p) => ('options' in p ? null : (
<Combobox.Option key={p.value} value={p.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">
{p.label}
</Combobox.Option>
)))}
</Combobox.Popup>
</Combobox.Root>
</div>
);
}
Esc ตอนรายการปิดก็ล้างค่าได้เหมือนกัน
เปิดขึ้นด้านบน อ่านอย่างเดียว และปิดใช้งาน
side="top" ที่ Popup ให้เปิดขึ้นด้านบนก่อน และจะพลิกเองเมื่อที่ไม่พอ รายการจัดแนวตาม Anchor และกว้างเท่ากับมัน (ถ้าไม่มี Anchor ก็ใช้ Input)
readOnly ทำให้ช่องพิมพ์อ่านอย่างเดียว ปุ่มคีย์บอร์ดและการคลิกไม่เปิดรายการ และ Toggle กับ Clear ถูกปิดใช้งาน
disabled ปิดใช้งานช่องพิมพ์ Toggle และ Clear
- ช่องที่ปิดใช้งานไม่ render hidden input จึงไม่ถูกส่งไปกับฟอร์ม (เหมือน control ปกติที่ปิดใช้งาน) การ reset ฟอร์มคืนค่าแบบ uncontrolled และข้อความของมันกลับ
import { Combobox } from '@purpurabog/ui/headless';
const provinces = [
{ value: 'bkk', label: 'กรุงเทพมหานคร' },
{ value: 'cnx', label: 'เชียงใหม่' },
{ value: 'hkt', label: 'ภูเก็ต' },
];
interface FieldProps {
id: string;
label: string;
side?: 'bottom' | 'top';
defaultValue?: string;
readOnly?: boolean;
disabled?: boolean;
}
function ProvinceField({ id, label, side, ...props }: FieldProps) {
return (
<Combobox.Root items={provinces} {...props}>
<label htmlFor={id} className="text-xs text-ink-muted">{label}</label>
<Combobox.Anchor className="flex h-10 items-center border-b border-border-strong-1 bg-field-1 hover:bg-field-hover-1 focus-within:outline-2 focus-within:-outline-offset-2 focus-within:outline-focus has-data-readonly:border-hairline has-data-readonly:bg-transparent has-data-disabled:border-transparent has-data-disabled:hover:bg-field-1">
<Combobox.Input id={id} placeholder="พิมพ์ชื่อจังหวัด" className="h-full min-w-0 flex-1 bg-transparent ps-4 text-sm text-ink outline-none placeholder:text-ink-placeholder data-disabled:cursor-not-allowed data-disabled:text-ink-disabled" />
<Combobox.Toggle aria-label="เปิด" className="group grid h-full w-10 shrink-0 place-items-center text-ink disabled:text-ink-disabled">
<svg viewBox="0 0 16 16" aria-hidden="true" className="size-4 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>
</Combobox.Toggle>
</Combobox.Anchor>
<Combobox.Popup aria-label={label} side={side} className="bg-layer-1 text-sm text-ink shadow-lg">
{(items) => items.map((p) => ('options' in p ? null : (
<Combobox.Option key={p.value} value={p.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">
{p.label}
</Combobox.Option>
)))}
</Combobox.Popup>
</Combobox.Root>
);
}
export default function Example() {
return (
<div className="grid max-w-xs gap-2">
<ProvinceField id="province-top" label="จังหวัด (เปิดด้านบน)" side="top" />
<ProvinceField id="province-readonly" label="จังหวัด (อ่านอย่างเดียว)" defaultValue="bkk" readOnly />
<ProvinceField id="province-disabled" label="จังหวัด (ปิดใช้งาน)" disabled />
</div>
);
}
required และ invalid ตั้งแค่ aria-required และ aria-invalid (กับ data-invalid) ให้ตรวจค่าและแสดง error เอง (ฟอร์มและการตรวจค่า) ไม่มีขนาดให้เลือก ความสูงเป็น CSS ของคุณ
ใส่ style
import { useState } from 'react';
import { Combobox } from '@purpurabog/ui/headless';
const provinces = [
{ value: 'bkk', label: 'กรุงเทพมหานคร' },
{ value: 'cnx', label: 'เชียงใหม่' },
{ value: 'hkt', label: 'ภูเก็ต (ไม่มีสาขา)', disabled: true },
];
export default function Example() {
const [province, setProvince] = useState('');
const invalid = province === '';
return (
<div className="grid max-w-xs gap-2">
<Combobox.Root items={provinces} name="province" value={province} onValueChange={setProvince} invalid={invalid} required>
<label htmlFor="province-style" className="text-xs text-ink-muted">จังหวัด</label>
<Combobox.Anchor className="flex h-10 items-center border-b border-border-strong-1 bg-field-1 hover:bg-field-hover-1 focus-within:outline-2 focus-within:-outline-offset-2 focus-within:outline-focus has-data-invalid:outline-2 has-data-invalid:-outline-offset-2 has-data-invalid:outline-error">
<Combobox.Input
id="province-style"
placeholder="พิมพ์ชื่อจังหวัด"
aria-describedby={invalid ? 'province-style-error' : undefined}
className="h-full min-w-0 flex-1 bg-transparent ps-4 text-sm text-ink outline-none placeholder:text-ink-placeholder"
/>
<Combobox.Toggle aria-label="เปิด" className="grid h-full w-10 shrink-0 place-items-center text-ink data-open:rotate-180">
<svg viewBox="0 0 16 16" aria-hidden="true" className="size-4 fill-current">
<path d="M8 11 3 6l.7-.7L8 9.6l4.3-4.3.7.7z" />
</svg>
</Combobox.Toggle>
</Combobox.Anchor>
<Combobox.Popup aria-label="จังหวัด" className="bg-layer-1 text-sm text-ink shadow-lg">
{(items) => items.map((p) => ('options' in p ? null : (
<Combobox.Option key={p.value} value={p.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">
{p.label}
</Combobox.Option>
)))}
</Combobox.Popup>
</Combobox.Root>
{invalid ? <p id="province-style-error" className="text-xs text-error-text">เลือกจังหวัดจากรายการ</p> : null}
</div>
);
}
/* className="input" ที่ Input, "toggle" ที่ Toggle, "option" ที่แต่ละ Option */
.input[data-open] { outline: 2px solid; }
.input[data-invalid] { outline: 2px solid #da1e28; }
.toggle[data-open] { transform: rotate(180deg); }
.option[data-highlighted] { background: #e8e8e8; }
.option[data-selected] { font-weight: 600; }
Parts
Combobox.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 |
|
หลังค่าเปลี่ยน: การเลือก ข้อความที่ยืนยันแล้ว การล้างค่า |
onInputValueChange |
(text: string) => void |
|
ทุกครั้งที่ผู้ใช้พิมพ์ |
filter |
'none' | 'contains' | 'starts-with' |
'none' |
ข้อความที่พิมพ์กรองรายการอย่างไร |
allowCustomValue |
boolean |
false |
เก็บข้อความที่ไม่ตรงกับรายการไหนเป็นค่า |
loading |
boolean |
false |
popup เป็น aria-busy |
onOpenChange |
(open: boolean) => void |
|
หลังรายการเปิดหรือปิด |
disabled |
boolean |
false |
ปิดใช้งานช่องพิมพ์ Toggle และ Clear |
readOnly |
boolean |
false |
ข้อความและค่าเปลี่ยนไม่ได้ |
required |
boolean |
false |
aria-required ที่ช่องพิมพ์ |
invalid |
boolean |
false |
aria-invalid และ data-invalid ที่ช่องพิมพ์ |
name |
string |
|
ส่งค่าไปกับฟอร์ม (hidden input) ยกเว้นตอนปิดใช้งาน |
id |
string |
สร้างให้ |
id ของช่องพิมพ์ และฐานของ id อื่น: <id>-list, <id>-opt-<n> ส่วน aria-labelledby ของ popup ชี้ไปที่ <id>-label ซึ่งเป็น id ที่ <label> ของคุณต้องมีเอง (ดู Combobox.Popup) |
Combobox.useComboboxContext() คืนสถานะของ Root (text, value, open, listed, loading, interactive …) ให้ component ที่อยู่ข้างใน
<input type="text" role="combobox" autocomplete="off"> มี id เป็น id ของ Root ถ้าคุณไม่ได้ใส่เอง รับ prop ของ input ได้ทุกตัว ยกเว้น type, role, value, defaultValue, disabled และ readOnly ซึ่งมาจาก Root
| Attribute |
เมื่อ |
data-open |
รายการเปิดอยู่ |
data-disabled |
ปิดใช้งาน |
data-readonly |
อ่านอย่างเดียว |
data-invalid |
ตั้ง invalid |
Combobox.Anchor
<div> ที่ครอบช่องพิมพ์และปุ่มต่าง ๆ popup จัดแนวตามมันและกว้างเท่ากับมัน ไม่มี prop และ data attribute เพิ่ม
Combobox.Clear
<button type="button"> ที่มี tabIndex={-1} (ใส่เองเพื่อเปลี่ยนได้) คลิกแล้วล้างข้อความและค่า ปิดรายการ และให้โฟกัสอยู่ในช่องพิมพ์ ถูกปิดใช้งานเมื่อ Root ปิดใช้งานหรืออ่านอย่างเดียว ใส่ aria-label ให้ด้วย
Combobox.Toggle
<button type="button"> ที่มี tabIndex={-1}, aria-expanded และ aria-controls คลิกแล้วเปิดหรือปิดรายการ โฟกัสยังอยู่ในช่องพิมพ์ ปุ่มถูกปิดใช้งานเมื่อ Root ปิดใช้งานหรืออ่านอย่างเดียว ใส่ aria-label ให้ด้วย
| Attribute |
เมื่อ |
data-open |
รายการเปิดอยู่ |
<ul role="listbox" popover="manual"> ใน top layer แถวจะ render เฉพาะตอนรายการเปิด aria-labelledby เป็น <id>-label ถ้าคุณไม่ได้ใส่เอง แต่ไม่มี part สำหรับ label จึงต้องใส่ aria-label หรือให้ <label> ของคุณมี id นั้น
| Prop |
Type |
ค่าเริ่มต้น |
|
children |
ReactNode | ((items: ListItem[]) => ReactNode) |
|
แถว หรือฟังก์ชันที่รับรายการที่ข้อความยังเก็บไว้ |
side |
'bottom' | 'top' |
'bottom' |
ด้านที่รายการเปิด จะพลิกเองเมื่อที่ไม่พอ |
| Attribute |
เมื่อ |
data-open |
รายการเปิดอยู่ |
data-side |
bottom หรือ top คือด้านที่รายการไปอยู่ |
Combobox.Option, Group, GroupLabel, GroupOptions
part ชุดเดียวกับใน Headless Dropdown คือ Option เป็น <li role="option"> ที่ต้องใส่ value และมี data-highlighted, data-selected และ data-disabled คลิกรายการที่ใช้งานได้จะเลือกรายการนั้น ใส่ชื่อลงในช่องพิมพ์ แล้วปิดรายการ
คีย์บอร์ดและ accessibility
| ปุ่ม |
ทำอะไร |
| พิมพ์ |
เปิดรายการและไฮไลต์รายการแรกที่มีข้อความนั้น |
| ↓ / ↑ |
เปิดรายการที่รายการที่เลือกไว้ (ถ้าไม่มีก็รายการแรก / สุดท้าย) จากนั้นเลื่อนไฮไลต์ (ไม่วนกลับ) |
| Alt+↓ |
เปิดรายการโดยไม่มีไฮไลต์ |
| Alt+↑ |
เลือกรายการที่ไฮไลต์แล้วปิด |
| Enter |
เลือกรายการที่ไฮไลต์ ถ้าไม่มีไฮไลต์จะเลือกรายการแรกที่มีข้อความนั้น (หรือเก็บข้อความไว้เมื่อใช้ allowCustomValue) |
| Esc |
ปิดรายการ ถ้ารายการปิดอยู่จะล้างข้อความและค่า |
| Tab |
ไปช่องถัดไปโดยไม่เลือกรายการที่ไฮไลต์ ข้อความจะถูกยืนยันหรือเด้งกลับ |
part ต่าง ๆ ตั้ง role="combobox", aria-autocomplete="list", aria-expanded, aria-controls, aria-activedescendant, aria-required และ aria-invalid ที่ช่องพิมพ์ และตั้ง role ของ listbox และ option ที่รายการ โฟกัสอยู่ในช่องพิมพ์ตลอด
ส่วนที่คุณเพิ่มเอง
<label htmlFor> ของช่องพิมพ์ และชื่อของ popup (aria-label หรือ <label> ที่มี id <id>-label)
aria-label ที่ Clear และ Toggle
- ข้อความช่วยเหลือและ error ผูกด้วย
aria-describedby ที่ช่องพิมพ์
- ข้อความ
role="status" สำหรับ "กำลังค้นหา…" และ "ไม่พบรายการที่ตรงกัน" ตามตัวอย่างด้านบน