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 Multi-select

part ของ MultiSelect ให้พฤติกรรมเดียวกับ Multi-select แบบมี style แต่ไม่มี style เลย ได้แก่ ช่องที่เปิด listbox ใน top layer และเปิดค้างไว้ระหว่างที่ผู้ใช้ติ๊กรายการ รายการที่เลือกย้ายขึ้นบนสุด แถว "เลือกทั้งหมด" ที่ใส่หรือไม่ใส่ก็ได้ ช่องพิมพ์สำหรับกรอง คีย์บอร์ด และ hidden input หนึ่งตัวต่อหนึ่งค่าที่เลือก

ส่วนที่คุณทำเองคือ markup ข้อความ label ตัวเลขจำนวน ช่องติ๊กในแต่ละแถว ข้อความช่วยเหลือหรือ error ข้อความ "กำลังโหลด…" และ style ทั้งหมด part ต่าง ๆ บอกสถานะผ่าน attribute data-* ให้คุณใช้ใส่ style

ติดตั้ง

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

npm install @purpurabog/ui import { MultiSelect, type ListOption } from '@purpurabog/ui/headless';

วิธีใช้

import { MultiSelect, type ListOption } from '@purpurabog/ui/headless'; const people = [ { value: 'owner', label: 'เจ้าของสัญญา' }, { value: 'legal', label: 'ทีมกฎหมาย' }, { value: 'finance', label: 'ทีมการเงิน' }, ]; // แถวที่วาดช่องติ๊กตาม data-selected ของมัน function Row({ value, label }: ListOption) { return ( <MultiSelect.Option value={value} className="group flex cursor-pointer items-center gap-2 px-4 py-3 text-ink-muted hover:bg-layer-hover-1 data-highlighted:bg-layer-hover-1 data-selected:text-ink"> <span aria-hidden="true" className="grid size-4 shrink-0 place-items-center rounded-xs border border-ink text-transparent group-data-selected:bg-ink group-data-selected:text-icon-inverse"> <svg viewBox="0 0 16 16" className="size-3.5 fill-current"><path d="M6 10.6 2.7 7.3 2 8l4 4 8-8-.7-.7z" /></svg> </span> {label} </MultiSelect.Option> ); } export default function Example() { return ( <div className="grid max-w-xs gap-2"> <MultiSelect.Root items={people} name="notify"> <MultiSelect.Label className="text-xs text-ink-muted">แจ้งเตือน</MultiSelect.Label> <MultiSelect.Anchor className="flex h-10 items-center border-b border-border-strong-1 bg-field-1 hover:bg-field-hover-1 has-focus-visible:outline-2 has-focus-visible:-outline-offset-2 has-focus-visible:outline-focus has-data-open:outline-2 has-data-open:-outline-offset-2 has-data-open:outline-focus"> <MultiSelect.Trigger className="group flex h-full min-w-0 flex-1 items-center justify-between gap-2 px-4 text-start text-sm text-ink outline-none"> <MultiSelect.Value placeholder="เลือกผู้รับ" className="truncate 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> </MultiSelect.Trigger> </MultiSelect.Anchor> <MultiSelect.Popup className="bg-layer-1 text-sm text-ink shadow-lg"> {(items) => items.map((p) => ('options' in p ? null : <Row key={p.value} {...p} />))} </MultiSelect.Popup> </MultiSelect.Root> </div> ); }

items ของ Root คือทุกรายการ Popup เรียกฟังก์ชัน children ด้วยแถวที่จะแสดงตามลำดับ คือกรองด้วยข้อความที่พิมพ์ รายการที่เลือกอยู่ก่อน และกลุ่มยังอยู่

โครงสร้าง

<MultiSelect.Root items={items}> <MultiSelect.Label /> <MultiSelect.Anchor> <MultiSelect.Trigger> <MultiSelect.Value /> </MultiSelect.Trigger> {/* หรือเมื่อใช้ filterable: */} <MultiSelect.Input /> <MultiSelect.ClearText /> <MultiSelect.Toggle /> <MultiSelect.Clear /> </MultiSelect.Anchor> <MultiSelect.Popup> {(items) => ( <> <MultiSelect.SelectAll /> <MultiSelect.Option value="…" /> <MultiSelect.Group> <MultiSelect.GroupLabel /> <MultiSelect.GroupOptions> <MultiSelect.Option value="…" /> </MultiSelect.GroupOptions> </MultiSelect.Group> </> )} </MultiSelect.Popup> </MultiSelect.Root>

จำนวน ปุ่มล้างค่า และคำอธิบาย

Value แสดง children ถ้าไม่มีก็แสดง placeholder มันไม่นับจำนวนให้ อ่าน count จาก MultiSelect.useMultiSelectContext() เพื่อแสดงว่าเลือกไปกี่รายการ Clear ล้างทุกค่าที่เลือกและให้โฟกัสอยู่ที่ช่อง ปุ่มนี้ถูกปิดใช้งานเมื่อ Root ปิดใช้งานหรืออ่านอย่างเดียว ตัวอย่างด้านล่างยังไม่แสดงปุ่มในตอนนั้นด้วย เหมือน MultiSelect แบบมี style

import { MultiSelect, type ListOption } from '@purpurabog/ui/headless'; const people = [ { value: 'owner', label: 'เจ้าของสัญญา' }, { value: 'legal', label: 'ทีมกฎหมาย' }, { value: 'finance', label: 'ทีมการเงิน' }, { value: 'signer', label: 'ผู้ลงนาม', description: 'หลังจากทั้งสองฝ่ายลงนามแล้วเท่านั้น' }, ]; function CountAndClear() { const { count, interactive } = MultiSelect.useMultiSelectContext(); if (count === 0) return null; return ( <span className="ms-2 inline-flex h-6 shrink-0 items-center gap-1 rounded-full bg-inverse-canvas ps-2 text-xs text-inverse-ink"> {count} {interactive ? ( <MultiSelect.Clear aria-label="ล้างทั้งหมด" className="grid size-6 place-items-center rounded-full hover:bg-inverse-hover"> <svg viewBox="0 0 16 16" aria-hidden="true" className="size-3.5 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> </MultiSelect.Clear> ) : null} </span> ); } function Row({ value, label, description }: ListOption) { return ( <MultiSelect.Option value={value} className="group flex cursor-pointer items-start gap-2 px-4 py-2 text-ink-muted hover:bg-layer-hover-1 data-highlighted:bg-layer-hover-1 data-selected:text-ink"> <span aria-hidden="true" className="mt-0.5 grid size-4 shrink-0 place-items-center rounded-xs border border-ink text-transparent group-data-selected:bg-ink group-data-selected:text-icon-inverse"> <svg viewBox="0 0 16 16" className="size-3.5 fill-current"><path d="M6 10.6 2.7 7.3 2 8l4 4 8-8-.7-.7z" /></svg> </span> <span className="flex flex-col"> {label} <small className="text-xs text-ink-helper">{description}</small> </span> </MultiSelect.Option> ); } export default function Example() { return ( <div className="grid max-w-xs gap-2"> <MultiSelect.Root items={people} name="notify" defaultValue={['owner']}> <MultiSelect.Label className="text-xs text-ink-muted">แจ้งเตือน</MultiSelect.Label> <MultiSelect.Anchor className="flex h-10 items-center border-b border-border-strong-1 bg-field-1 hover:bg-field-hover-1 has-focus-visible:outline-2 has-focus-visible:-outline-offset-2 has-focus-visible:outline-focus has-data-open:outline-2 has-data-open:-outline-offset-2 has-data-open:outline-focus"> <CountAndClear /> <MultiSelect.Trigger className="group flex h-full min-w-0 flex-1 items-center justify-between gap-2 px-4 text-start text-sm text-ink outline-none"> <MultiSelect.Value placeholder="เลือกผู้รับ" className="truncate 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> </MultiSelect.Trigger> </MultiSelect.Anchor> <MultiSelect.Popup className="bg-layer-1 text-sm text-ink shadow-lg"> {(items) => items.map((p) => ('options' in p ? null : <Row key={p.value} {...p} />))} </MultiSelect.Popup> </MultiSelect.Root> </div> ); }

ค่า

value เป็น array เก็บค่าเองด้วย value คู่กับ onValueChange หรือให้ค่าเริ่มต้นด้วย defaultValue ถ้ามี name ตัว Root จะ render hidden input หนึ่งตัวต่อหนึ่งค่าที่เลือก จึงอ่านด้วย FormData.getAll()

import { useState } from 'react'; import { MultiSelect, type ListOption } from '@purpurabog/ui/headless'; const people = [ { value: 'owner', label: 'เจ้าของสัญญา' }, { value: 'legal', label: 'ทีมกฎหมาย' }, { value: 'finance', label: 'ทีมการเงิน' }, ]; function Row({ value, label }: ListOption) { return ( <MultiSelect.Option value={value} className="group flex cursor-pointer items-center gap-2 px-4 py-3 text-ink-muted hover:bg-layer-hover-1 data-highlighted:bg-layer-hover-1 data-selected:text-ink"> <span aria-hidden="true" className="grid size-4 shrink-0 place-items-center rounded-xs border border-ink text-transparent group-data-selected:bg-ink group-data-selected:text-icon-inverse"> <svg viewBox="0 0 16 16" className="size-3.5 fill-current"><path d="M6 10.6 2.7 7.3 2 8l4 4 8-8-.7-.7z" /></svg> </span> {label} </MultiSelect.Option> ); } export default function Example() { const [notify, setNotify] = useState(['legal']); const [sent, setSent] = useState<FormDataEntryValue[]>(); return ( <form className="grid max-w-xs gap-2" onSubmit={(e) => { e.preventDefault(); setSent(new FormData(e.currentTarget).getAll('notify')); }} > <MultiSelect.Root items={people} name="notify" value={notify} onValueChange={setNotify}> <MultiSelect.Label className="text-xs text-ink-muted">แจ้งเตือน</MultiSelect.Label> <MultiSelect.Anchor className="flex h-10 items-center border-b border-border-strong-1 bg-field-1 hover:bg-field-hover-1 has-focus-visible:outline-2 has-focus-visible:-outline-offset-2 has-focus-visible:outline-focus has-data-open:outline-2 has-data-open:-outline-offset-2 has-data-open:outline-focus"> <MultiSelect.Trigger className="group flex h-full min-w-0 flex-1 items-center justify-between gap-2 px-4 text-start text-sm text-ink outline-none"> <MultiSelect.Value placeholder="เลือกผู้รับ" className="truncate 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> </MultiSelect.Trigger> </MultiSelect.Anchor> <MultiSelect.Popup className="bg-layer-1 text-sm text-ink shadow-lg"> {(items) => items.map((p) => ('options' in p ? null : <Row key={p.value} {...p} />))} </MultiSelect.Popup> </MultiSelect.Root> <p className="text-xs text-ink-muted">ค่า: {JSON.stringify(notify)}</p> <button type="submit" className="h-10 justify-self-start 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> {sent ? <p className="text-xs text-ink-muted">ส่งแล้ว: notify = {JSON.stringify(sent)}</p> : null} </form> ); }

เลือกทั้งหมด

selectAll เพิ่มแถว "เลือกทั้งหมด" ให้วาง SelectAll ไว้แรกสุดใน popup มันจะ render เฉพาะตอนที่เปิดใช้และมีรายการให้เลือก คลิกแล้วเลือกทุกรายการที่ใช้งานได้ซึ่งแสดงอยู่ หรือเอาออกทั้งหมดเมื่อเลือกครบแล้ว ระหว่างที่เลือกไว้แค่บางรายการ แถวนี้มี data-mixed และ aria-checked="mixed"

import { MultiSelect, type ListOption } from '@purpurabog/ui/headless'; const people = [ { value: 'owner', label: 'เจ้าของสัญญา' }, { value: 'legal', label: 'ทีมกฎหมาย' }, { value: 'finance', label: 'ทีมการเงิน' }, ]; const box = 'grid size-4 shrink-0 place-items-center rounded-xs border border-ink text-transparent group-data-selected:bg-ink group-data-selected:text-icon-inverse'; const check = <svg viewBox="0 0 16 16" className="size-3.5 fill-current"><path d="M6 10.6 2.7 7.3 2 8l4 4 8-8-.7-.7z" /></svg>; function Row({ value, label }: ListOption) { return ( <MultiSelect.Option value={value} className="group flex cursor-pointer items-center gap-2 px-4 py-3 text-ink-muted hover:bg-layer-hover-1 data-highlighted:bg-layer-hover-1 data-selected:text-ink"> <span aria-hidden="true" className={box}>{check}</span> {label} </MultiSelect.Option> ); } export default function Example() { return ( <div className="grid max-w-xs gap-2"> <MultiSelect.Root items={people} name="notify" selectAll> <MultiSelect.Label className="text-xs text-ink-muted">แจ้งเตือน</MultiSelect.Label> <MultiSelect.Anchor className="flex h-10 items-center border-b border-border-strong-1 bg-field-1 hover:bg-field-hover-1 has-focus-visible:outline-2 has-focus-visible:-outline-offset-2 has-focus-visible:outline-focus has-data-open:outline-2 has-data-open:-outline-offset-2 has-data-open:outline-focus"> <MultiSelect.Trigger className="group flex h-full min-w-0 flex-1 items-center justify-between gap-2 px-4 text-start text-sm text-ink outline-none"> <MultiSelect.Value placeholder="เลือกผู้รับ" className="truncate 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> </MultiSelect.Trigger> </MultiSelect.Anchor> <MultiSelect.Popup className="bg-layer-1 text-sm text-ink shadow-lg"> {(items) => ( <> <MultiSelect.SelectAll className="group flex cursor-pointer items-center gap-2 px-4 py-3 text-ink-muted hover:bg-layer-hover-1 data-highlighted:bg-layer-hover-1 data-selected:text-ink"> {/* mixed: ช่องทึบที่มีขีดแทนเครื่องหมายถูก */} <span aria-hidden="true" className={`${box} relative after:absolute after:top-[6px] after:left-[3px] after:hidden after:h-0.5 after:w-2 after:bg-icon-inverse group-data-mixed:bg-ink group-data-mixed:after:block`}>{check}</span> เลือกทั้งหมด </MultiSelect.SelectAll> {items.map((p) => ('options' in p ? null : <Row key={p.value} {...p} />))} </> )} </MultiSelect.Popup> </MultiSelect.Root> </div> ); }

รายการที่เลือกไปอยู่ตรงไหน

selectionFeedback กำหนดว่าแถวที่เลือกจะย้ายขึ้นบนสุดของแถวที่ Popup ส่งให้เมื่อไร

selectionFeedback แถวที่เลือก
top-after-reopen (ค่าเริ่มต้น) ย้ายขึ้นบนสุดตอนเปิดรายการครั้งถัดไป
top ย้ายขึ้นบนสุดทันที
fixed อยู่ที่เดิม

ถ้ามีกลุ่มใน items จะไม่มีอะไรย้าย

import { useState } from 'react'; import { MultiSelect, type ListOption } from '@purpurabog/ui/headless'; type Feedback = 'top-after-reopen' | 'top' | 'fixed'; const people = [ { value: 'owner', label: 'เจ้าของสัญญา' }, { value: 'legal', label: 'ทีมกฎหมาย' }, { value: 'finance', label: 'ทีมการเงิน' }, { value: 'signer', label: 'ผู้ลงนาม' }, ]; function Row({ value, label }: ListOption) { return ( <MultiSelect.Option value={value} className="group flex cursor-pointer items-center gap-2 px-4 py-3 text-ink-muted hover:bg-layer-hover-1 data-highlighted:bg-layer-hover-1 data-selected:text-ink"> <span aria-hidden="true" className="grid size-4 shrink-0 place-items-center rounded-xs border border-ink text-transparent group-data-selected:bg-ink group-data-selected:text-icon-inverse"> <svg viewBox="0 0 16 16" className="size-3.5 fill-current"><path d="M6 10.6 2.7 7.3 2 8l4 4 8-8-.7-.7z" /></svg> </span> {label} </MultiSelect.Option> ); } export default function Example() { const [feedback, setFeedback] = useState<Feedback>('top-after-reopen'); return ( <div className="grid max-w-xs gap-4"> <label className="grid gap-2 text-xs text-ink-muted"> selectionFeedback <select value={feedback} onChange={(e) => setFeedback(e.target.value as Feedback)} 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="top-after-reopen">top-after-reopen</option> <option value="top">top</option> <option value="fixed">fixed</option> </select> </label> <div className="grid gap-2"> <MultiSelect.Root items={people} selectionFeedback={feedback}> <MultiSelect.Label className="text-xs text-ink-muted">แจ้งเตือน</MultiSelect.Label> <MultiSelect.Anchor className="flex h-10 items-center border-b border-border-strong-1 bg-field-1 hover:bg-field-hover-1 has-focus-visible:outline-2 has-focus-visible:-outline-offset-2 has-focus-visible:outline-focus has-data-open:outline-2 has-data-open:-outline-offset-2 has-data-open:outline-focus"> <MultiSelect.Trigger className="group flex h-full min-w-0 flex-1 items-center justify-between gap-2 px-4 text-start text-sm text-ink outline-none"> <MultiSelect.Value placeholder="เลือกผู้รับ" className="truncate 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> </MultiSelect.Trigger> </MultiSelect.Anchor> <MultiSelect.Popup className="bg-layer-1 text-sm text-ink shadow-lg"> {(items) => items.map((p) => ('options' in p ? null : <Row key={p.value} {...p} />))} </MultiSelect.Popup> </MultiSelect.Root> </div> </div> ); }

พิมพ์กรองได้

filterable เปลี่ยนช่องเป็นช่องพิมพ์ ให้ใช้ Input แทน Trigger และ Value แล้วเพิ่ม ClearText และ Toggle ถ้าต้องการ filter เป็น contains (ค่าเริ่มต้น) หรือ starts-with การเลือกจะคงข้อความและรายการไว้ เมื่อโฟกัสออกจาก Anchor หรือกดข้างนอก รายการจะปิดและข้อความถูกล้าง แถว "เลือกทั้งหมด" จะมีผลกับแถวที่แสดงอยู่เท่านั้น

import { MultiSelect, type ListOption } from '@purpurabog/ui/headless'; const provinces = [ { value: 'bkk', label: 'กรุงเทพมหานคร' }, { value: 'cnx', label: 'เชียงใหม่' }, { value: 'cei', label: 'เชียงราย' }, { value: 'kkc', label: 'ขอนแก่น' }, { value: 'hkt', label: 'ภูเก็ต' }, { value: 'skh', label: 'สงขลา' }, ]; function Row({ value, label }: ListOption) { return ( <MultiSelect.Option value={value} className="group flex cursor-pointer items-center gap-2 px-4 py-3 text-ink-muted hover:bg-layer-hover-1 data-highlighted:bg-layer-hover-1 data-selected:text-ink"> <span aria-hidden="true" className="grid size-4 shrink-0 place-items-center rounded-xs border border-ink text-transparent group-data-selected:bg-ink group-data-selected:text-icon-inverse"> <svg viewBox="0 0 16 16" className="size-3.5 fill-current"><path d="M6 10.6 2.7 7.3 2 8l4 4 8-8-.7-.7z" /></svg> </span> {label} </MultiSelect.Option> ); } export default function Example() { return ( <div className="grid max-w-xs gap-2"> <MultiSelect.Root items={provinces} name="provinces" filterable> <MultiSelect.Label className="text-xs text-ink-muted">จังหวัด</MultiSelect.Label> <MultiSelect.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"> <MultiSelect.Input placeholder="พิมพ์เพื่อกรอง" className="h-full min-w-0 flex-1 bg-transparent ps-4 text-sm text-ink outline-none placeholder:text-ink-placeholder" /> <MultiSelect.ClearText 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> </MultiSelect.ClearText> <MultiSelect.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> </MultiSelect.Toggle> </MultiSelect.Anchor> <MultiSelect.Popup className="max-h-72 overflow-y-auto bg-layer-1 text-sm text-ink shadow-lg"> {(items) => items.map((p) => ('options' in p ? null : <Row key={p.value} {...p} />))} </MultiSelect.Popup> </MultiSelect.Root> </div> ); }

ถ้าค้นหาบน server ให้ตั้ง filter="none" รับข้อความที่พิมพ์จาก onInputValueChange แทนที่ items และตั้ง loading เก็บรายการที่เลือกไว้แล้วไว้ใน items ด้วย แถวของมันจะได้ไม่หาย

import { useRef, useState } from 'react'; import { MultiSelect, type ListOption } from '@purpurabog/ui/headless'; const everyone: ListOption[] = [ { value: 'somchai', label: 'สมชาย ใจดี' }, { value: 'suda', label: 'สุดา รักไทย' }, { value: 'anan', label: 'อนันต์ ศรีสุข' }, { value: 'malee', label: 'มาลี วงศ์ษา' }, { value: 'prasert', label: 'ประเสริฐ ชัยโย' }, { value: 'ratana', label: 'รัตนา บุญมี' }, ]; // แทน server: คนที่ชื่อมีข้อความนั้น ตอบกลับหลัง 500 ms const search = (text: string) => new Promise<ListOption[]>((resolve) => { const found = everyone.filter((p) => p.label.toLowerCase().includes(text.trim().toLowerCase())); setTimeout(() => resolve(found), 500); }); function Row({ value, label }: ListOption) { return ( <MultiSelect.Option value={value} className="group flex cursor-pointer items-center gap-2 px-4 py-3 text-ink-muted hover:bg-layer-hover-1 data-highlighted:bg-layer-hover-1 data-selected:text-ink"> <span aria-hidden="true" className="grid size-4 shrink-0 place-items-center rounded-xs border border-ink text-transparent group-data-selected:bg-ink group-data-selected:text-icon-inverse"> <svg viewBox="0 0 16 16" className="size-3.5 fill-current"><path d="M6 10.6 2.7 7.3 2 8l4 4 8-8-.7-.7z" /></svg> </span> {label} </MultiSelect.Option> ); } export default function Example() { const [value, setValue] = useState<string[]>([]); const [found, setFound] = useState(everyone); const [loading, setLoading] = useState(false); const latest = useRef(''); const onText = async (text: string) => { latest.current = text; setLoading(true); const result = await search(text); if (latest.current !== text) return; // มีการค้นหาที่ใหม่กว่ากำลังมา setFound(result); setLoading(false); }; // เก็บตัวที่เลือกไว้ใน items เพื่อให้แถวของมันยังอยู่ const rows = [...everyone.filter((p) => value.includes(p.value) && !found.includes(p)), ...found]; return ( <div className="grid max-w-xs gap-2"> <MultiSelect.Root items={rows} name="people" value={value} onValueChange={setValue} filterable filter="none" loading={loading} onInputValueChange={onText}> <MultiSelect.Label className="text-xs text-ink-muted">บุคคล</MultiSelect.Label> <MultiSelect.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"> <MultiSelect.Input placeholder="พิมพ์ชื่อ" className="h-full min-w-0 flex-1 bg-transparent ps-4 text-sm text-ink outline-none placeholder:text-ink-placeholder" /> <MultiSelect.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> </MultiSelect.Toggle> </MultiSelect.Anchor> <MultiSelect.Popup className="max-h-72 overflow-y-auto bg-layer-1 text-sm text-ink shadow-lg"> {(items) => ( <> {/* ระหว่างโหลด items จะว่าง */} {loading ? <li role="option" aria-disabled="true" aria-selected="false" className="px-4 py-3 text-ink-placeholder">กำลังค้นหา…</li> : null} {items.map((p) => ('options' in p ? null : <Row key={p.value} {...p} />))} </> )} </MultiSelect.Popup> <span role="status" className="sr-only">{loading ? 'กำลังค้นหา…' : ''}</span> </MultiSelect.Root> <p className="text-xs text-ink-muted">ค่า: {JSON.stringify(value)}</p> </div> ); }

onText ตั้ง loading ค้นหา (ในตัวอย่างนี้ใช้ตัวแทนที่ตอบกลับหลังรอสักครู่) แล้วใส่ผลลัพธ์ใน rows รวมกับรายการที่เลือกไว้ เหมือนในหน้า Multi-select

โหลดรายการตอนเปิดรายการ

onOpenChange ยิงหลังรายการเปิดหรือปิด ระหว่างที่ loading เป็น true ตัว Popup จะไม่ส่งแถวให้ แถว "เลือกทั้งหมด" ถูกซ่อน ปุ่มคีย์บอร์ดเลือกอะไรไม่ได้ และ popup มี aria-busy ให้ render แถว "กำลังโหลด…" และข้อความ role="status" เอง แบบเดียวกับใน Headless Dropdown

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

  • readOnly แสดงค่าที่เลือก แต่เปิดรายการไม่ได้และปุ่มคีย์บอร์ดไม่ทำอะไร ช่องได้ aria-readonly (หรือช่องพิมพ์เป็น readOnly เมื่อใช้ filterable) และ Toggle กับ Clear ถูกปิดใช้งาน
  • disabled ปิดใช้งานช่อง Toggle และ Clear
  • ช่องที่ปิดใช้งานไม่ render hidden input จึงไม่ถูกส่งไปกับฟอร์ม (เหมือน control ปกติที่ปิดใช้งาน) การ reset ฟอร์มคืนค่าที่เลือกแบบ uncontrolled กลับ
import { MultiSelect } from '@purpurabog/ui/headless'; const people = [ { value: 'owner', label: 'เจ้าของสัญญา' }, { value: 'legal', label: 'ทีมกฎหมาย' }, ]; // ชื่อของตัวที่เลือก อ่านจาก state ของ Root function ChosenValue() { const { value } = MultiSelect.useMultiSelectContext(); const labels = people.filter((p) => value.includes(p.value)).map((p) => p.label).join(', '); return <MultiSelect.Value placeholder="เลือกผู้รับ" className="truncate data-placeholder:text-ink-placeholder">{labels || undefined}</MultiSelect.Value>; } function NotifyField({ label, ...props }: { label: string; defaultValue?: string[]; readOnly?: boolean; disabled?: boolean }) { return ( <MultiSelect.Root items={people} {...props}> <MultiSelect.Label className="text-xs text-ink-muted data-disabled:text-ink-disabled">{label}</MultiSelect.Label> <MultiSelect.Anchor className="flex h-10 items-center border-b border-border-strong-1 bg-field-1 has-focus-visible:outline-2 has-focus-visible:-outline-offset-2 has-focus-visible:outline-focus has-data-readonly:border-hairline has-data-readonly:bg-transparent has-data-disabled:border-transparent"> <MultiSelect.Trigger className="group flex h-full min-w-0 flex-1 items-center justify-between gap-2 px-4 text-start text-sm text-ink outline-none data-readonly:cursor-default data-disabled:cursor-not-allowed data-disabled:text-ink-disabled"> <ChosenValue /> <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> </MultiSelect.Trigger> </MultiSelect.Anchor> <MultiSelect.Popup className="bg-layer-1 text-sm text-ink shadow-lg"> {(items) => items.map((p) => ('options' in p ? null : <MultiSelect.Option key={p.value} value={p.value} className="px-4 py-3">{p.label}</MultiSelect.Option>))} </MultiSelect.Popup> </MultiSelect.Root> ); } export default function Example() { return ( <div className="grid max-w-xs gap-2"> <NotifyField label="แจ้งเตือน (อ่านอย่างเดียว)" defaultValue={['owner', 'legal']} readOnly /> <NotifyField label="แจ้งเตือน (ปิดใช้งาน)" disabled /> </div> ); }

required และ invalid ตั้งแค่ aria-required และ aria-invalid (กับ data-invalid) ให้ตรวจค่าและแสดง error เอง (ฟอร์มและการตรวจค่า) ไม่มีขนาดให้เลือก ความสูงเป็น CSS ของคุณ

ใส่ style

import { useState } from 'react'; import { MultiSelect, type ListOption } from '@purpurabog/ui/headless'; const people = [ { value: 'owner', label: 'เจ้าของสัญญา' }, { value: 'legal', label: 'ทีมกฎหมาย' }, { value: 'board', label: 'คณะกรรมการ (ลาพัก)', disabled: true }, ]; function Row({ value, label }: ListOption) { return ( <MultiSelect.Option value={value} className="group flex cursor-pointer items-center gap-2 px-4 py-3 text-ink-muted hover:bg-layer-hover-1 data-highlighted:bg-layer-hover-1 data-selected:text-ink data-disabled:cursor-not-allowed data-disabled:bg-transparent data-disabled:text-ink-disabled"> <span aria-hidden="true" className="grid size-4 shrink-0 place-items-center rounded-xs border border-ink text-transparent group-data-selected:bg-ink group-data-selected:text-icon-inverse group-data-disabled:border-ink-disabled"> <svg viewBox="0 0 16 16" className="size-3.5 fill-current"><path d="M6 10.6 2.7 7.3 2 8l4 4 8-8-.7-.7z" /></svg> </span> {label} </MultiSelect.Option> ); } export default function Example() { const [notify, setNotify] = useState<string[]>([]); const invalid = notify.length === 0; return ( <div className="grid max-w-xs gap-2"> <MultiSelect.Root items={people} name="notify" value={notify} onValueChange={setNotify} invalid={invalid} required> <MultiSelect.Label className="text-xs text-ink-muted">แจ้งเตือน</MultiSelect.Label> <MultiSelect.Anchor className="flex h-10 items-center border-b border-border-strong-1 bg-field-1 hover:bg-field-hover-1 has-focus-visible:outline-2 has-focus-visible:-outline-offset-2 has-focus-visible:outline-focus has-data-open:outline-2 has-data-open:-outline-offset-2 has-data-open:outline-focus has-data-invalid:outline-2 has-data-invalid:-outline-offset-2 has-data-invalid:outline-error"> <MultiSelect.Trigger aria-describedby={invalid ? 'notify-style-error' : undefined} className="group flex h-full min-w-0 flex-1 items-center justify-between gap-2 px-4 text-start text-sm text-ink outline-none" > <MultiSelect.Value placeholder="เลือกผู้รับ" className="truncate 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> </MultiSelect.Trigger> </MultiSelect.Anchor> <MultiSelect.Popup className="bg-layer-1 text-sm text-ink shadow-lg"> {(items) => items.map((p) => ('options' in p ? null : <Row key={p.value} {...p} />))} </MultiSelect.Popup> </MultiSelect.Root> {invalid ? <p id="notify-style-error" className="text-xs text-error-text">เลือกอย่างน้อยหนึ่งคน</p> : null} </div> ); } /* className="trigger" ที่ Trigger, "option" ที่แต่ละ Option, "select-all" ที่ SelectAll, "box" ที่ช่องติ๊กที่คุณวาด */ .trigger[data-open] { outline: 2px solid; } .trigger[data-invalid] { outline: 2px solid #da1e28; } .option[data-highlighted] { background: #e8e8e8; } .option[data-selected] .box { background: #161616; } .select-all[data-mixed] .box { background: linear-gradient(#161616, #161616) center / 8px 2px no-repeat; }

Parts

MultiSelect.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 filterable: ทุกครั้งที่ข้อความที่พิมพ์เปลี่ยน
selectionFeedback 'top-after-reopen' | 'top' | 'fixed' 'top-after-reopen' รายการที่เลือกย้ายขึ้นบนสุดเมื่อไร
selectAll boolean false มีแถว SelectAll
filterable boolean false ช่องเป็นช่องพิมพ์ (Input) ที่กรองรายการ
filter 'contains' | 'starts-with' | 'none' 'contains' ข้อความที่พิมพ์กรองรายการอย่างไร (filterable)
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 ของช่องพิมพ์ (filterable) และฐานของ id: <id>-label, <id>-value, <id>-list, <id>-opt-<n>, <id>-opt-all

MultiSelect.useMultiSelectContext() คืนสถานะของ Root (value, count, open, text, rows, allState, interactive …) ให้ component ที่อยู่ข้างใน

MultiSelect.Label

<span> ที่มี id <id>-label ใช้เป็นชื่อของช่องและของ popup

Attribute เมื่อ
data-disabled Root ปิดใช้งาน

MultiSelect.Anchor

<div> ที่ครอบช่อง ตัวเลขจำนวน และปุ่มต่าง ๆ popup จัดแนวตามมันและกว้างเท่ากับมัน เมื่อใช้ filterable โฟกัสที่ออกจากมันจะปิดรายการและล้างข้อความ ไม่มี prop และ data attribute เพิ่ม

MultiSelect.Trigger

<button type="button" role="combobox"> สำหรับช่องที่ไม่ได้ใช้ filterable มี aria-labelledby เป็น <id>-label <id>-value ถ้าคุณไม่ได้ใส่เอง รับ prop ของ button ได้ทุกตัว ยกเว้น type, role และ disabled

Attribute เมื่อ
data-open รายการเปิดอยู่
data-disabled ปิดใช้งาน
data-readonly อ่านอย่างเดียว
data-invalid ตั้ง invalid
data-placeholder ยังไม่ได้เลือกอะไร

MultiSelect.Value

<span> ที่มี id <id>-value แสดง children ถ้าไม่มีก็แสดง placeholder

Prop Type ค่าเริ่มต้น
placeholder ReactNode แสดงเมื่อไม่มี children
Attribute เมื่อ
data-placeholder ยังไม่ได้เลือกอะไร

MultiSelect.Input

filterable: <input type="text" role="combobox" autocomplete="off"> ที่กรองรายการ มี id เป็น id ของ Root และ aria-labelledby เป็น <id>-label ถ้าคุณไม่ได้ใส่เอง รับ prop ของ input ได้ทุกตัว ยกเว้น type, role, value, defaultValue, disabled และ readOnly มี data attribute ชุดเดียวกับ Trigger

MultiSelect.Clear

<button type="button"> ที่มี tabIndex={-1} คลิกแล้วล้างทุกค่าที่เลือกและย้ายโฟกัสไปที่ช่อง ถูกปิดใช้งานเมื่อ Root ปิดใช้งานหรืออ่านอย่างเดียว ใส่ aria-label ให้ด้วย

MultiSelect.ClearText

filterable: <button type="button"> ที่มี tabIndex={-1} คลิกแล้วล้างข้อความที่พิมพ์และย้ายโฟกัสไปที่ช่องพิมพ์ ใส่ aria-label ให้ด้วย

MultiSelect.Toggle

filterable: <button type="button"> ที่มี tabIndex={-1}, aria-expanded และ aria-controls คลิกแล้วเปิดหรือปิดรายการ โฟกัสยังอยู่ในช่องพิมพ์ ปุ่มถูกปิดใช้งานเมื่อ Root ปิดใช้งานหรืออ่านอย่างเดียว ใส่ aria-label ให้ด้วย

Attribute เมื่อ
data-open รายการเปิดอยู่

MultiSelect.Popup

<ul role="listbox" aria-multiselectable="true" popover="manual"> ใน top layer แถวจะ render เฉพาะตอนรายการเปิด การกดข้างนอกจะปิดรายการและล้างข้อความ

Prop Type ค่าเริ่มต้น
children ReactNode | ((items: ListItem[]) => ReactNode) แถว หรือฟังก์ชันที่รับแถวที่จะแสดง
side 'bottom' | 'top' 'bottom' ด้านที่รายการเปิด จะพลิกเองเมื่อที่ไม่พอ
Attribute เมื่อ
data-open รายการเปิดอยู่
data-side bottom หรือ top คือด้านที่รายการไปอยู่

MultiSelect.SelectAll

<li role="option"> ที่มี id <id>-opt-all render เฉพาะตอนตั้ง selectAll ไม่ได้โหลดอยู่ และมีรายการที่ใช้งานได้แสดงอยู่ คลิกแล้วเลือกทุกรายการที่ใช้งานได้ซึ่งแสดงอยู่ หรือเอาออกทั้งหมดเมื่อเลือกครบแล้ว

Attribute เมื่อ
data-highlighted ไฮไลต์ของคีย์บอร์ดอยู่ที่แถวนี้
data-selected เลือกครบทุกรายการที่แสดงอยู่
data-mixed เลือกไว้บางรายการ

MultiSelect.Option, Group, GroupLabel, GroupOptions

part ชุดเดียวกับใน Headless Dropdown คือ Option เป็น <li role="option"> ที่ต้องใส่ value และมี data-highlighted, data-selected และ data-disabled คลิกรายการที่ใช้งานได้จะติ๊กหรือเอาติ๊กออก และรายการยังเปิดอยู่

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

ปุ่ม ตอนรายการปิด ตอนรายการเปิด
↓, Enter, Space เปิดรายการ Enter และ Space ติ๊กหรือเอาติ๊กออกจากแถวที่ไฮไลต์ รายการยังเปิดอยู่
↓ / ↑, Page Down / Page Up, Home / End ↑ ไม่ทำอะไร ส่วน Home / End เปิดที่แถวแรก / สุดท้าย เลื่อนไฮไลต์
ตัวอักษร เปิดที่รายการถัดไปที่ขึ้นต้นด้วยตัวอักษรนั้น กระโดดไปรายการถัดไปที่ขึ้นต้นด้วยตัวอักษรนั้น
Delete, Backspace, Esc ล้างทุกค่าที่เลือก Esc ปิดรายการ
Tab ไปช่องถัดไป ปิดรายการแล้วไปช่องถัดไป

เมื่อใช้ filterable ตัวอักษร Space, Home, End, Delete และ Backspace ใช้แก้ข้อความ ↓ หรือ Enter เปิดรายการ Enter ติ๊กแถวที่ไฮไลต์ (หรือปิดรายการเมื่อไม่มีไฮไลต์) และ Esc ตอนรายการปิดจะล้างค่าที่เลือกและข้อความ

part ต่าง ๆ ตั้ง role="combobox", aria-haspopup, aria-expanded, aria-controls, aria-activedescendant, aria-readonly, aria-required และ aria-invalid ที่ช่อง และตั้ง aria-multiselectable, aria-selected และ aria-checked="mixed" ที่รายการ โฟกัสอยู่ที่ช่องตลอด

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

  • ข้อความ label ใน Label
  • aria-label ที่ Clear, ClearText และ Toggle
  • ตัวเลขจำนวนถ้าจะแสดง และช่องติ๊กที่วาดในแต่ละแถว (ซ่อนจาก screen reader เพราะ aria-selected บอกอยู่แล้ว)
  • ข้อความช่วยเหลือและ error ผูกด้วย aria-describedby ที่ช่อง
  • ข้อความ role="status" สำหรับ "กำลังโหลด…", "กำลังค้นหา…" และ "ไม่พบรายการที่ตรงกัน"