Multi-select
Multi-select ใช้เลือกหลายรายการจากรายการที่แต่ละแถวเป็น checkbox รายการจะเปิดค้างไว้ระหว่างเลือก และมี tag ในช่องบอกว่าเลือกไปกี่รายการแล้ว
เจ้าของสัญญา
ทีมกฎหมาย
ทีมการเงิน
ผู้ลงนาม
HTML React React + Tailwind <ui-multi-select label ="แจ้งเตือนถึง" name ="notify" placeholder ="เลือกผู้รับ" select-all >
<option value ="owner" selected >เจ้าของสัญญา</option >
<option value ="legal" >ทีมกฎหมาย</option >
<option value ="finance" >ทีมการเงิน</option >
<option value ="signer" data-description ="หลังจากทั้งสองฝ่ายลงนามแล้วเท่านั้น" >ผู้ลงนาม</option >
</ui-multi-select >
import type { UiMultiSelect as MultiSelectElement } from '@purpurabog/ui' ;
const [notify, setNotify] = useState (['owner' ]);
<UiMultiSelect label = "แจ้งเตือนถึง" name = "notify" placeholder = "เลือกผู้รับ" selectAll value = {notify} onChange = {(e) => setNotify ((e.target as MultiSelectElement ).value)}>
<option value = "owner" >เจ้าของสัญญา</option >
<option value = "legal" >ทีมกฎหมาย</option >
<option value = "finance" >ทีมการเงิน</option >
<option value = "signer" data-description = "หลังจากทั้งสองฝ่ายลงนามแล้วเท่านั้น" >ผู้ลงนาม</option >
</UiMultiSelect >
const people = [
{ value: 'owner' , label: 'เจ้าของสัญญา' },
{ value: 'legal' , label: 'ทีมกฎหมาย' },
{ value: 'finance' , label: 'ทีมการเงิน' },
{ value: 'signer' , label: 'ผู้ลงนาม' , description: 'หลังจากทั้งสองฝ่ายลงนามแล้วเท่านั้น' },
];
<MultiSelect label = "แจ้งเตือนถึง" name = "notify" placeholder = "เลือกผู้รับ" items = {people} defaultValue = {['owner' ]} selectAll />
ติดตั้ง
npm shadcn · Web component shadcn · React + Tailwind คัดลอกเอง npm install @purpurabog/ui
npx shadcn@4 add @kanok-ui/multi-select
npx shadcn@4 add @kanok-ui/tw-multi-select
# Web component
# 1. ติดตั้ง dependency
npm install lit@^3.3.0
# 2. คัดลอก "content" ของแต่ละไฟล์จาก
# https://cdn.jsdelivr.net/npm/@purpurabog/ui@2.3.0/registry/multi-select.json
# ไปไว้ที่ target ของมัน:
# components/ui/primitives/multi-select/multi-select.styles.ts
# components/ui/primitives/multi-select/multi-select.ts
# 3. ทำแบบเดียวกันกับ item ที่มันใช้ (registry/<name>.json):
# core-field, core-listbox, core-multi-select, core-popup, core-typeahead, shared-element, shared-events, shared-field, shared-form-control, shared-listbox, shared-localize, shared-position, shared-quiet-focus, shared-slots, tokens
# React + Tailwind
# 1. ติดตั้ง dependency
npm install @purpurabog/ui@^2.3.0
# 2. คัดลอก "content" ของแต่ละไฟล์จาก
# https://cdn.jsdelivr.net/npm/@purpurabog/ui@2.3.0/registry/tw-multi-select.json
# ไปไว้ที่ target ของมัน:
# components/ui/react-tw/multi-select.tsx
# 3. ทำแบบเดียวกันกับ item ที่มันใช้ (registry/<name>.json):
# core-field, core-listbox, core-multi-select, tw-field, tw-i18n, tw-list, tw-theme, tw-utils
npm ให้ทุก component จากแพ็กเกจ ส่วน shadcn คัดลอกซอร์สเข้าแอปของคุณแทน เป็นสำเนาที่ล็อกเวอร์ชันนี้และแก้เองได้ คำสั่ง shadcn ต้องตั้ง registry @kanok-ui ใน components.json ครั้งเดียวก่อน ดู ติดตั้ง
วิธีใช้
HTML React React + Tailwind <script type ="module" >import '@purpurabog/ui/multi-select' ;</script >
<ui-multi-select label ="แจ้งเตือนถึง" name ="notify" placeholder ="เลือกผู้รับ" >
<option value ="owner" >เจ้าของสัญญา</option >
<option value ="legal" >ทีมกฎหมาย</option >
</ui-multi-select >
import { UiMultiSelect } from '@purpurabog/ui/react' ;
<UiMultiSelect label = "แจ้งเตือนถึง" name = "notify" placeholder = "เลือกผู้รับ" >
<option value = "owner" >เจ้าของสัญญา</option >
<option value = "legal" >ทีมกฎหมาย</option >
</UiMultiSelect >
import { MultiSelect } from '@/components/ui/react-tw/multi-select' ;
const people = [
{ value: 'owner' , label: 'เจ้าของสัญญา' },
{ value: 'legal' , label: 'ทีมกฎหมาย' },
];
<MultiSelect label = "แจ้งเตือนถึง" name = "notify" placeholder = "เลือกผู้รับ" items = {people} />
ต้องการเลือกค่าเดียวจากหลายรายการ? ดู Select, Dropdown, Combobox หรือ MultiSelect?
อ่านและส่งค่า
value คือ array ของค่าที่เลือกไว้ กำหนดรายการที่เลือกตอนเริ่มด้วย <option selected> (หรือ defaultValue ใน React + Tailwind) ฟอร์มจะได้หนึ่ง entry ต่อหนึ่งค่าที่เลือกภายใต้ name จึงอ่านด้วย FormData.getAll() event change ยิงทุกครั้งที่ชุดค่าที่เลือกเปลี่ยน ถ้าใส่ required ฟอร์มจะส่งไม่ได้จนกว่าจะเลือกอย่างน้อยหนึ่งรายการ
HTML React React + Tailwind <form id ="multi-select-form" style ="display: grid; gap: 16px" >
<ui-multi-select id ="multi-select-docs" label ="ประเภทเอกสารแนบ" name ="docs" placeholder ="เลือกได้หลายรายการ" required invalid-text ="เลือกอย่างน้อย 1 รายการ" >
<option value ="id" selected >สำเนาบัตรประชาชน</option >
<option value ="poa" >หนังสือมอบอำนาจ</option >
<option value ="cert" >หนังสือรับรองบริษัท</option >
<option value ="map" >แผนที่</option >
</ui-multi-select >
<div ><ui-button type ="submit" >ส่ง</ui-button ></div >
</form >
<p >value: <output id ="multi-select-docs-out" ></output ></p >
<p >ค่าที่ส่ง: <output id ="multi-select-form-out" ></output ></p >
<script type ="module" >
const form = document.getElementById ('multi-select-form' );
const docs = document.getElementById ('multi-select-docs' );
const valueOut = document.getElementById ('multi-select-docs-out' );
const show = () => (valueOut.value = JSON.stringify (docs.value));
show ();
docs.addEventListener ('change' , show);
form.addEventListener ('submit' , (event) => {
event.preventDefault ();
document.getElementById ('multi-select-form-out' ).value = JSON.stringify (new FormData (form).getAll ('docs' ));
});
</script >
import type { UiMultiSelect as MultiSelectElement } from '@purpurabog/ui' ;
const [docs, setDocs] = useState (['id' ]);
const [sent, setSent] = useState <string []>([]);
const submit = (event: FormEvent <HTMLFormElement >) => {
event.preventDefault ();
setSent (new FormData (event.currentTarget).getAll ('docs' ) as string []);
};
<form onSubmit = {submit}>
<UiMultiSelect label = "ประเภทเอกสารแนบ" name = "docs" placeholder = "เลือกได้หลายรายการ" required invalidText = "เลือกอย่างน้อย 1 รายการ" value = {docs} onChange = {(e) => setDocs ((e.target as MultiSelectElement ).value)}>
<option value = "id" >สำเนาบัตรประชาชน</option >
<option value = "poa" >หนังสือมอบอำนาจ</option >
<option value = "cert" >หนังสือรับรองบริษัท</option >
<option value = "map" >แผนที่</option >
</UiMultiSelect >
<UiButton type = "submit" >ส่ง</UiButton >
</form >
<p >value: <output >{JSON.stringify (docs)}</output ></p >
<p >ค่าที่ส่ง: <output >{JSON.stringify (sent)}</output ></p >
// การตรวจค่าเป็นของแอป: `tried` เป็น true หลังกดส่งครั้งแรก
<form onSubmit = {submit}>
<MultiSelect label = "ประเภทเอกสารแนบ" name = "docs" placeholder = "เลือกได้หลายรายการ" items = {documents} value = {docs} onChange = {setDocs} required invalid = {tried && docs.length === 0 } invalidText = "เลือกอย่างน้อย 1 รายการ" />
<Button type = "submit" >ส่ง</Button >
</form >
<p >value: <output >{JSON.stringify (docs)}</output ></p >
<p >ค่าที่ส่ง: <output >{JSON.stringify (sent)}</output ></p >
value เป็น property ไม่ใช่ attribute ใน HTML ให้ตั้งค่าจาก JavaScript หรือใช้ <option selected> ส่วน placeholder จะแสดงอยู่ในช่องตลอดแม้เลือกแล้ว (คือ label ของ Carbon) อยู่ข้าง tag จำนวน
เลือกทั้งหมด
select-all เพิ่มแถวแรกที่ติ๊กหรือเอาติ๊กออกจากทุกรายการที่ใช้งานได้ ระหว่างที่เลือกไว้แค่บางรายการ แถวนี้จะแสดงสถานะ mixed
ฝ่ายกฎหมาย
ฝ่ายการเงิน
ฝ่ายจัดซื้อ
ฝ่ายบุคคล (ไม่เกี่ยวข้อง)
HTML React React + Tailwind <ui-multi-select label ="แผนกที่ต้องตรวจ" name ="review" placeholder ="เลือกแผนก" select-all >
<option value ="legal" >ฝ่ายกฎหมาย</option >
<option value ="finance" >ฝ่ายการเงิน</option >
<option value ="procurement" >ฝ่ายจัดซื้อ</option >
<option value ="hr" disabled >ฝ่ายบุคคล (ไม่เกี่ยวข้อง)</option >
</ui-multi-select >
<UiMultiSelect label = "แผนกที่ต้องตรวจ" name = "review" placeholder = "เลือกแผนก" selectAll >
<option value = "legal" >ฝ่ายกฎหมาย</option >
<option value = "finance" >ฝ่ายการเงิน</option >
<option value = "procurement" >ฝ่ายจัดซื้อ</option >
<option value = "hr" disabled >ฝ่ายบุคคล (ไม่เกี่ยวข้อง)</option >
</UiMultiSelect >
const departments = [
{ value: 'legal' , label: 'ฝ่ายกฎหมาย' },
{ value: 'finance' , label: 'ฝ่ายการเงิน' },
{ value: 'procurement' , label: 'ฝ่ายจัดซื้อ' },
{ value: 'hr' , label: 'ฝ่ายบุคคล (ไม่เกี่ยวข้อง)' , disabled: true },
];
<MultiSelect label = "แผนกที่ต้องตรวจ" name = "review" placeholder = "เลือกแผนก" items = {departments} selectAll />
รายการที่เลือกไปอยู่ตรงไหน
selection-feedback กำหนดว่าแถวที่เลือกจะย้ายขึ้นไปบนสุดเมื่อไร
selection-feedback
แถวที่เลือก
top-after-reopen (ค่าเริ่มต้น)
ย้ายขึ้นบนสุดตอนเปิดรายการครั้งถัดไป ระหว่างเลือกจึงไม่มีอะไรกระโดด
top
ย้ายขึ้นบนสุดทันที
fixed
อยู่ที่เดิม
นอกจากนั้นลำดับเป็นไปตามที่คุณเขียน (Carbon เรียงตามตัวอักษรด้วย แต่ไลบรารีนี้คงลำดับ <option> ของคุณไว้) ถ้ามี <optgroup> กลุ่มจะอยู่ตามที่เขียนและไม่มีอะไรย้าย ลองเลือกแถวท้าย ๆ ในทั้งสองช่องด้านล่าง แล้วปิดและเปิดรายการใหม่
ตรวจร่าง
ฝ่ายกฎหมายตรวจ
ฝ่ายการเงินตรวจ
ลงนาม
ตรวจร่าง
ฝ่ายกฎหมายตรวจ
ฝ่ายการเงินตรวจ
ลงนาม
HTML React React + Tailwind <ui-multi-select label ="ขั้นตอนอนุมัติ (ค่าเริ่มต้น)" placeholder ="เลือกขั้นตอน" >
<option value ="draft" >ตรวจร่าง</option >
<option value ="legal" >ฝ่ายกฎหมายตรวจ</option >
<option value ="finance" >ฝ่ายการเงินตรวจ</option >
<option value ="sign" >ลงนาม</option >
</ui-multi-select >
<ui-multi-select label ="ขั้นตอนอนุมัติ (fixed)" placeholder ="เลือกขั้นตอน" selection-feedback ="fixed" >
<option value ="draft" >ตรวจร่าง</option >
<option value ="legal" >ฝ่ายกฎหมายตรวจ</option >
<option value ="finance" >ฝ่ายการเงินตรวจ</option >
<option value ="sign" >ลงนาม</option >
</ui-multi-select >
<UiMultiSelect label = "ขั้นตอนอนุมัติ (ค่าเริ่มต้น)" placeholder = "เลือกขั้นตอน" >…</UiMultiSelect >
<UiMultiSelect label = "ขั้นตอนอนุมัติ (fixed)" placeholder = "เลือกขั้นตอน" selectionFeedback = "fixed" >…</UiMultiSelect >
<MultiSelect label = "ขั้นตอนอนุมัติ (ค่าเริ่มต้น)" placeholder = "เลือกขั้นตอน" items = {steps} />
<MultiSelect label = "ขั้นตอนอนุมัติ (fixed)" placeholder = "เลือกขั้นตอน" items = {steps} selectionFeedback = "fixed" />
พิมพ์กรองได้
filterable เปลี่ยนช่องเป็นช่องพิมพ์ที่กรองรายการระหว่างพิมพ์ (คือ FilterableMultiSelect ของ Carbon) filter เป็น contains (ค่าเริ่มต้น เหมือน Carbon) หรือ starts-with การเลือกจะคงข้อความและรายการที่เปิดไว้ เมื่อออกจากช่อง ข้อความจะถูกล้าง และ "เลือกทั้งหมด" จะมีผลเฉพาะแถวที่แสดงอยู่
กรุงเทพมหานคร
เชียงใหม่
เชียงราย
ภูเก็ต
ขอนแก่น
HTML React React + Tailwind <ui-multi-select label ="จังหวัด" name ="provinces" placeholder ="พิมพ์เพื่อกรอง" filterable >
<option value ="bkk" >กรุงเทพมหานคร</option >
<option value ="cnx" >เชียงใหม่</option >
<option value ="cri" >เชียงราย</option >
<option value ="hkt" >ภูเก็ต</option >
<option value ="kkn" >ขอนแก่น</option >
</ui-multi-select >
<UiMultiSelect label = "จังหวัด" name = "provinces" placeholder = "พิมพ์เพื่อกรอง" filterable >
<option value = "bkk" >กรุงเทพมหานคร</option >
<option value = "cnx" >เชียงใหม่</option >
<option value = "cri" >เชียงราย</option >
<option value = "hkt" >ภูเก็ต</option >
<option value = "kkn" >ขอนแก่น</option >
</UiMultiSelect >
<MultiSelect label = "จังหวัด" name = "provinces" placeholder = "พิมพ์เพื่อกรอง" items = {provinces} filterable />
ค้นหาบน server
ถ้าค้นหาบน server ให้ตั้ง filter="none" ฟังข้อความที่พิมพ์ แทนที่รายการ และตั้ง loading (รายการแสดง "กำลังค้นหา…") เก็บรายการที่เลือกไว้แล้วไว้ในรายการด้วย แถวของมันจะได้ไม่หายไป ในตัวอย่างใช้ timer สั้น ๆ แทน server
HTML React React + Tailwind <ui-multi-select id ="multi-select-branches" label ="สาขา" name ="branches" placeholder ="ค้นหาสาขา" helper-text ="ค้นหาระหว่างที่พิมพ์" filterable filter ="none" ></ui-multi-select >
<script type ="module" >
// แทน fetch(`/api/branches?q=${encodeURIComponent(text)}`).then((response) => response.json())
const all = ['สาขาสีลม' , 'สาขาสยาม' , 'สาขาสาทร' , 'สาขาบางนา' , 'สาขาบางรัก' , 'สาขาลาดพร้าว' , 'สาขาเชียงใหม่' , 'สาขาขอนแก่น' , 'สาขาภูเก็ต' , 'สาขาหาดใหญ่' ];
const search = (text) =>
new Promise((resolve) => setTimeout (() => resolve (all.filter ((b) => b.toLowerCase ().includes (text.toLowerCase ()))), 600 ));
const branches = document.getElementById ('multi-select-branches' );
const show = (found) => {
const chosen = [... branches.querySelectorAll ('option' )].filter ((o) => branches.value.includes (o.value));
const rest = found.filter ((b) => ! branches.value.includes (b)).map ((b) => new Option (b, b));
branches.replaceChildren (... chosen, ... rest);
};
show (all);
branches.addEventListener ('input' , async () => {
const text = branches.inputValue;
branches.loading = true ;
const found = await search (text);
if (branches.inputValue !== text) return ; // มีการค้นหาที่ใหม่กว่ากำลังทำงาน
show (found);
branches.loading = false ;
});
</script >
<UiMultiSelect label = "สาขา" name = "branches" placeholder = "ค้นหาสาขา" filterable filter = "none" loading = {loading} onInput = {(e) => onText ((e.target as MultiSelectElement ).inputValue)}>
{rows.map ((b) => <option key = {b} value = {b}>{b}</option >)}
</UiMultiSelect >
<MultiSelect label = "สาขา" name = "branches" placeholder = "ค้นหาสาขา" items = {rows.map ((b) => ({ value: b, label: b }))} filterable filter = "none" loading = {loading} onInputChange = {onText} />
search() ดึงผลที่ตรงกัน เหมือนในหน้า Combobox ส่วนในแบบ React onText ตั้ง loading ค้นหา แล้วใส่ผลลัพธ์ใน rows รวมกับรายการที่เลือกไว้
โหลดรายการตอนเปิดรายการ
ถ้าไม่ได้ใช้ filterable ตัว loading จะแสดงแถว "กำลังโหลด…" แถวเดียว และปุ่มต่าง ๆ เลือกอะไรไม่ได้ ใช้คู่กับ event ตอนเปิดรายการเพื่อดึงรายการครั้งแรกที่เปิด แบบเดียวกับใน Dropdown คือ ui-toggle ใน web component, onUiToggle ใน React และ onMenuChange ใน React + Tailwind
HTML React React + Tailwind <ui-multi-select id ="multi-select-witnesses" label ="พยาน" name ="witnesses" placeholder ="เลือกพยาน" helper-text ="รายชื่อจะโหลดครั้งแรกที่เปิดรายการ" loading ></ui-multi-select >
<script type ="module" >
// แทน fetch('/api/staff').then((response) => response.json())
const loadStaff = () =>
new Promise((resolve) =>
setTimeout (() => resolve ([
{ value: 'anong' , label: 'อนงค์ พ.' },
{ value: 'boonmee' , label: 'บุญมี ส.' },
{ value: 'chaiya' , label: 'ชัยยา ร.' },
]), 1000 ),
);
const witnesses = document.getElementById ('multi-select-witnesses' );
witnesses.addEventListener ('ui-toggle' , async (event) => {
if (! event.detail.open || ! witnesses.loading) return ;
const staff = await loadStaff ();
witnesses.replaceChildren (... staff.map ((s) => new Option (s.label, s.value)));
witnesses.loading = false ;
});
</script >
const [staff, setStaff] = useState <{ value: string ; label: string }[]>();
const load = async (open: boolean ) => {
if (open && ! staff) setStaff (await loadStaff ());
};
<UiMultiSelect label = "พยาน" name = "witnesses" placeholder = "เลือกพยาน" loading = {! staff} onUiToggle = {(e) => load (e.detail.open)}>
{staff?.map ((s) => <option key = {s.value} value = {s.value}>{s.label}</option >)}
</UiMultiSelect >
<MultiSelect label = "พยาน" name = "witnesses" placeholder = "เลือกพยาน" items = {staff ?? []} loading = {! staff} onMenuChange = {load} />
กลุ่มและคำอธิบาย
เหมือน Dropdown คือ <optgroup label> จัดกลุ่มรายการ data-description เพิ่มบรรทัดที่สอง (การพิมพ์และการกรองค้นเฉพาะชื่อ) และรายการที่ disabled เลือกไม่ได้ React + Tailwind รับกลุ่ม { label, options } และ description ใน items
ทีมกฎหมาย
ทีมการเงิน
คู่สัญญา
ผู้รับรองเอกสาร (ไม่ต้องใช้กับสัญญานี้)
HTML React React + Tailwind <ui-multi-select label ="ส่งสำเนาถึง" name ="cc" placeholder ="เลือกผู้รับ" >
<optgroup label ="ภายในบริษัท" >
<option value ="legal" data-description ="legal@example.co.th" >ทีมกฎหมาย</option >
<option value ="finance" data-description ="finance@example.co.th" >ทีมการเงิน</option >
</optgroup >
<optgroup label ="ภายนอก" >
<option value ="party" data-description ="ผู้ติดต่อของอีกฝ่าย" >คู่สัญญา</option >
<option value ="notary" disabled >ผู้รับรองเอกสาร (ไม่ต้องใช้กับสัญญานี้)</option >
</optgroup >
</ui-multi-select >
<UiMultiSelect label = "ส่งสำเนาถึง" name = "cc" placeholder = "เลือกผู้รับ" >
<optgroup label = "ภายในบริษัท" >
<option value = "legal" data-description = "legal@example.co.th" >ทีมกฎหมาย</option >
<option value = "finance" data-description = "finance@example.co.th" >ทีมการเงิน</option >
</optgroup >
<optgroup label = "ภายนอก" >
<option value = "party" data-description = "ผู้ติดต่อของอีกฝ่าย" >คู่สัญญา</option >
<option value = "notary" disabled >ผู้รับรองเอกสาร (ไม่ต้องใช้กับสัญญานี้)</option >
</optgroup >
</UiMultiSelect >
const recipients = [
{ label: 'ภายในบริษัท' , options: [
{ value: 'legal' , label: 'ทีมกฎหมาย' , description: 'legal@example.co.th' },
{ value: 'finance' , label: 'ทีมการเงิน' , description: 'finance@example.co.th' },
] },
{ label: 'ภายนอก' , options: [
{ value: 'party' , label: 'คู่สัญญา' , description: 'ผู้ติดต่อของอีกฝ่าย' },
{ value: 'notary' , label: 'ผู้รับรองเอกสาร (ไม่ต้องใช้กับสัญญานี้)' , disabled: true },
] },
];
<MultiSelect label = "ส่งสำเนาถึง" name = "cc" placeholder = "เลือกผู้รับ" items = {recipients} />
ขนาดและทิศทาง
size: sm 32px, md 40px (ค่าเริ่มต้น), lg 48px และ direction="top" เปิดรายการขึ้นด้านบน
ทีมกฎหมาย
ทีมการเงิน
ทีมกฎหมาย
ทีมการเงิน
ทีมกฎหมาย
ทีมการเงิน
HTML React React + Tailwind <ui-multi-select label ="เล็ก" size ="sm" placeholder ="เลือกผู้รับ" >
<option value ="legal" selected >ทีมกฎหมาย</option >
<option value ="finance" >ทีมการเงิน</option >
</ui-multi-select >
<ui-multi-select label ="กลาง" placeholder ="เลือกผู้รับ" >
<option value ="legal" selected >ทีมกฎหมาย</option >
<option value ="finance" >ทีมการเงิน</option >
</ui-multi-select >
<ui-multi-select label ="ใหญ่ เปิดขึ้นด้านบน" size ="lg" direction ="top" placeholder ="เลือกผู้รับ" >
<option value ="legal" selected >ทีมกฎหมาย</option >
<option value ="finance" >ทีมการเงิน</option >
</ui-multi-select >
<UiMultiSelect label = "เล็ก" size = "sm" placeholder = "เลือกผู้รับ" >…</UiMultiSelect >
<UiMultiSelect label = "กลาง" placeholder = "เลือกผู้รับ" >…</UiMultiSelect >
<UiMultiSelect label = "ใหญ่ เปิดขึ้นด้านบน" size = "lg" direction = "top" placeholder = "เลือกผู้รับ" >…</UiMultiSelect >
<MultiSelect label = "เล็ก" items = {people} size = "sm" placeholder = "เลือกผู้รับ" defaultValue = {['legal' ]} />
<MultiSelect label = "กลาง" items = {people} placeholder = "เลือกผู้รับ" defaultValue = {['legal' ]} />
<MultiSelect label = "ใหญ่ เปิดขึ้นด้านบน" items = {people} size = "lg" direction = "top" placeholder = "เลือกผู้รับ" defaultValue = {['legal' ]} />
แบบ fluid
fluid วาง label ไว้ในกล่องสูง 64px (FluidMultiSelect ของ Carbon) รายการที่เปิดออกมายังสูงแถวละเท่าเดิม ถ้าครอบด้วย fluid form ก็ได้ผลเหมือนกัน ดู fluid form
เจ้าของสัญญา
ทีมกฎหมาย
ทีมการเงิน
HTML React React + Tailwind <ui-multi-select fluid label ="แจ้งเตือนถึง" name ="notify" placeholder ="เลือกผู้รับ" >
<option value ="owner" selected >เจ้าของสัญญา</option >
<option value ="legal" >ทีมกฎหมาย</option >
<option value ="finance" >ทีมการเงิน</option >
</ui-multi-select >
<UiMultiSelect fluid label = "แจ้งเตือนถึง" name = "notify" placeholder = "เลือกผู้รับ" >…</UiMultiSelect >
<MultiSelect fluid label = "แจ้งเตือนถึง" name = "notify" placeholder = "เลือกผู้รับ" items = {people} defaultValue = {['owner' ]} />
แบบมีขอบ (ดีไซน์ Kanok)
ในดีไซน์ Kanok ช่องกรอกเป็นแบบ filled โดยปกติ คือพื้นเทาอ่อน ไม่มีขอบ ใส่ data-field-variant="outlined" ที่ element ที่ครอบช่อง (เช่น <form> หรือ <html> ถ้าต้องการทั้งแอป) จะเปลี่ยนเป็นขอบ 1px บนพื้นใส ส่วน focus ยังเป็นวงแสงสีฟ้าเหมือนเดิม attribute นี้ไม่มีผลในดีไซน์ Carbon ตัวอย่างด้านล่างจึงครอบด้วย data-design="kanok" ดูรายละเอียดทั้งหมดที่ ดีไซน์ Kanok
เจ้าของสัญญา
ทีมกฎหมาย
ทีมการเงิน
ผู้ลงนาม
HTML React React + Tailwind <div data-design ="kanok" data-theme ="paper" data-field-variant ="outlined" >
<ui-multi-select label ="แจ้งเตือนถึง" name ="notify" placeholder ="เลือกผู้รับ" select-all >
<option value ="owner" selected >เจ้าของสัญญา</option >
<option value ="legal" >ทีมกฎหมาย</option >
<option value ="finance" >ทีมการเงิน</option >
<option value ="signer" data-description ="หลังจากทั้งสองฝ่ายลงนามแล้วเท่านั้น" >ผู้ลงนาม</option >
</ui-multi-select >
</div >
import type { UiMultiSelect as MultiSelectElement } from '@purpurabog/ui' ;
const [notify, setNotify] = useState (['owner' ]);
<div data-field-variant = "outlined" >
<UiMultiSelect label = "แจ้งเตือนถึง" name = "notify" placeholder = "เลือกผู้รับ" selectAll value = {notify} onChange = {(e) => setNotify ((e.target as MultiSelectElement ).value)}>
<option value = "owner" >เจ้าของสัญญา</option >
<option value = "legal" >ทีมกฎหมาย</option >
<option value = "finance" >ทีมการเงิน</option >
<option value = "signer" data-description = "หลังจากทั้งสองฝ่ายลงนามแล้วเท่านั้น" >ผู้ลงนาม</option >
</UiMultiSelect >
</div >
const people = [
{ value: 'owner' , label: 'เจ้าของสัญญา' },
{ value: 'legal' , label: 'ทีมกฎหมาย' },
{ value: 'finance' , label: 'ทีมการเงิน' },
{ value: 'signer' , label: 'ผู้ลงนาม' , description: 'หลังจากทั้งสองฝ่ายลงนามแล้วเท่านั้น' },
];
<div data-field-variant = "outlined" >
<MultiSelect label = "แจ้งเตือนถึง" name = "notify" placeholder = "เลือกผู้รับ" items = {people} defaultValue = {['owner' ]} selectAll />
</div >
ข้อความช่วยเหลือ คำเตือน และ error
ใต้ช่องแสดงได้ทีละบรรทัด error มาก่อนคำเตือน และคำเตือนมาก่อนข้อความช่วยเหลือ warn ไม่บล็อกการส่งฟอร์ม invalid บังคับให้แสดง error
สมชาย ก.
มาลี ส.
สมชาย ก.
มาลี ส.
สมชาย ก.
มาลี ส.
HTML React React + Tailwind <ui-multi-select label ="ผู้อนุมัติ" name ="approvers" placeholder ="เลือกผู้อนุมัติ" helper-text ="ผู้อนุมัติลงนามตามลำดับ" >
<option value ="somchai" >สมชาย ก.</option >
<option value ="malee" >มาลี ส.</option >
</ui-multi-select >
<ui-multi-select label ="ผู้อนุมัติ" name ="approvers2" placeholder ="เลือกผู้อนุมัติ" warn warn-text ="มาลีลาพักถึงวันที่ 20 ตุลาคม" >
<option value ="somchai" >สมชาย ก.</option >
<option value ="malee" selected >มาลี ส.</option >
</ui-multi-select >
<ui-multi-select label ="ผู้อนุมัติ" name ="approvers3" placeholder ="เลือกผู้อนุมัติ" invalid invalid-text ="สัญญาเกิน 1 ล้านบาทต้องมีผู้อนุมัติสองคน" >
<option value ="somchai" selected >สมชาย ก.</option >
<option value ="malee" >มาลี ส.</option >
</ui-multi-select >
<UiMultiSelect label = "ผู้อนุมัติ" name = "approvers" placeholder = "เลือกผู้อนุมัติ" helperText = "ผู้อนุมัติลงนามตามลำดับ" >…</UiMultiSelect >
<UiMultiSelect label = "ผู้อนุมัติ" name = "approvers2" placeholder = "เลือกผู้อนุมัติ" value = {['malee' ]} warn warnText = "มาลีลาพักถึงวันที่ 20 ตุลาคม" >…</UiMultiSelect >
<UiMultiSelect label = "ผู้อนุมัติ" name = "approvers3" placeholder = "เลือกผู้อนุมัติ" value = {['somchai' ]} invalid invalidText = "สัญญาเกิน 1 ล้านบาทต้องมีผู้อนุมัติสองคน" >…</UiMultiSelect >
<MultiSelect label = "ผู้อนุมัติ" name = "approvers" placeholder = "เลือกผู้อนุมัติ" items = {approvers} helperText = "ผู้อนุมัติลงนามตามลำดับ" />
<MultiSelect label = "ผู้อนุมัติ" name = "approvers2" placeholder = "เลือกผู้อนุมัติ" items = {approvers} defaultValue = {['malee' ]} warn warnText = "มาลีลาพักถึงวันที่ 20 ตุลาคม" />
<MultiSelect label = "ผู้อนุมัติ" name = "approvers3" placeholder = "เลือกผู้อนุมัติ" items = {approvers} defaultValue = {['somchai' ]} invalid invalidText = "สัญญาเกิน 1 ล้านบาทต้องมีผู้อนุมัติสองคน" />
อ่านอย่างเดียวและปิดใช้งาน
readonly (readOnly ใน React + Tailwind) แสดงและส่งค่าที่เลือก แต่เปลี่ยนไม่ได้ disabled ทำให้ช่องเป็นสีเทาและไม่ถูกส่งไปกับฟอร์ม
เจ้าของสัญญา
ทีมกฎหมาย
ทีมการเงิน
เจ้าของสัญญา
ทีมกฎหมาย
HTML React React + Tailwind <ui-multi-select label ="แจ้งเตือนถึง" placeholder ="เลือกผู้รับ" readonly >
<option value ="owner" selected >เจ้าของสัญญา</option >
<option value ="legal" selected >ทีมกฎหมาย</option >
<option value ="finance" >ทีมการเงิน</option >
</ui-multi-select >
<ui-multi-select label ="แจ้งเตือนถึง" placeholder ="เลือกผู้รับ" disabled >
<option value ="owner" >เจ้าของสัญญา</option >
<option value ="legal" >ทีมกฎหมาย</option >
</ui-multi-select >
<UiMultiSelect label = "แจ้งเตือนถึง" placeholder = "เลือกผู้รับ" value = {['owner' , 'legal' ]} readonly >…</UiMultiSelect >
<UiMultiSelect label = "แจ้งเตือนถึง" placeholder = "เลือกผู้รับ" disabled >…</UiMultiSelect >
<MultiSelect label = "แจ้งเตือนถึง" placeholder = "เลือกผู้รับ" items = {people} defaultValue = {['owner' , 'legal' ]} readOnly />
<MultiSelect label = "แจ้งเตือนถึง" placeholder = "เลือกผู้รับ" items = {people} disabled />
ช่องบังคับกรอก error และคำเตือน ดูที่ ฟอร์มและการตรวจค่า
คีย์บอร์ด
ปุ่ม
ตอนรายการปิด
ตอนรายการเปิด
↓, 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 ตอนรายการปิดจะล้างทั้งค่าที่เลือกและข้อความ
Accessibility
รายการเป็น listbox ที่มี aria-multiselectable และไฮไลต์ถูกอ่านผ่าน aria-activedescendant โฟกัสจึงอยู่ที่ช่องตลอด
ปุ่ม × ไม่อยู่ในลำดับ Tab (เหมือน Carbon) ใช้ Delete, Backspace หรือ Esc ล้างค่าจากคีย์บอร์ด
"กำลังโหลด…", "กำลังค้นหา…" และ "ไม่พบรายการที่ตรงกัน" ถูกประกาศให้ screen reader ได้ยิน
ต้องการแค่พฤติกรรมโดยไม่มี style: Headless Multi-select
API
สร้างจากแพ็กเกจโดยตรง ใน React ส่ง property เป็น prop ชื่อเดียวกัน ส่วน event ก็เป็น prop เช่นกัน (ดูคอลัมน์ React)
<ui-multi-select> · React UiMultiSelect
Attribute
Attribute
Property
ชนิด
ค่าเริ่มต้น
คำอธิบาย
label
label
string
''
Visible label text; the label slot replaces it when markup is needed.
hide-label
hideLabel
boolean
false
Keep the label for screen readers but don't show it.
helper-text
helperText
string
''
Helper text under the field, shown while there is no error or warning.
invalid-text
invalidText
string
''
Error text shown while invalid or while the field fails validation.
invalid
invalid
boolean
false
Forces the error state, e.g. after server-side validation.
warn
warn
boolean
false
Non-blocking warning; shown when there is no error.
warn-text
warnText
string
''
Text of the warning shown while warn.
placeholder
placeholder
string
''
Text always shown in the field (Carbon's label), e.g. "Choose options".
name
name
string
''
Reflected: form-associated elements submit under the name attribute.
size
size
'sm' | 'md' | 'lg'
'md'
Field height: sm 32px, md 40px, lg 48px (tokens --ui-field-height-*).
direction
direction
'bottom' | 'top'
'bottom'
Preferred side for the list; it flips when there is no room.
selection-feedback
selectionFeedback
SelectionFeedback
'top-after-reopen'
select-all
selectAll
boolean
false
Adds a "Select all" row.
filterable
filterable
boolean
false
The field is a text box that narrows the list (Carbon FilterableMultiSelect / filterable).
filter
filter
MultiSelectFilter
'contains'
With filterable: contains (Carbon), starts-with, or none to leave the list to a server search.
loading
loading
boolean
false
The options are on their way: "Searching…" with filterable (a server search), else "Loading…"; the keys pick nothing.
disabled
disabled
boolean
false
Disables the control; a disabled <fieldset> ancestor does the same.
required
required
boolean
false
Blocks form submission while the field is empty.
readonly
readonly
boolean
false
Shows and submits the value but blocks changes.
fluid
fluid
boolean
false
Carbon FluidMultiSelect: the label inside a 64px box, an error / warning inside it. <ui-fluid-form> sets it for every field inside.
Property
Property
ชนิด
คำอธิบาย
value
string[]
Chosen option values. Initial choices can also come from <option selected>.
inputValue
With filterable, what the user typed; set it to search for something.
willValidate
(อ่านอย่างเดียว) Whether the field takes part in constraint validation (not while disabled), as natively.
labels
(อ่านอย่างเดียว) The <label>s that point at this field.
Method
Method
คำอธิบาย
setCustomValidity(message)
Like <input>.setCustomValidity(): a non-empty message makes the field invalid (and is shown as its error once touched) until cleared with ''.
Event
Event
React
คำอธิบาย
ui-toggle
onUiToggle
detail: { open } after the list opens or closes (Carbon React onMenuChange)
input
onInput
Whenever the set of chosen values changes, as ui-dropdown and ui-combobox; with filterable, also native and composed as the user types, where value is unchanged (kept for compatibility: listen to ui-input for the typing)
ui-input
onUiInput
detail: { value } the typed text (inputValue) with filterable, as the user types or when the text is emptied
change
onChange
Whenever the set of chosen values changes
Slot
Slot
คำอธิบาย
label
Label content with markup; the label attribute is the plain-text fallback
helper-text
Helper text with markup (links, bold); overrides helper-text
warn-text
Warning text shown while warn; overrides warn-text
invalid-text
Error text shown while invalid; overrides invalid-text
CSS part
Part
คำอธิบาย
option-description
Second line of an option (<option data-description>)
base
The field box
label
The label
input
The field button, or the text box with filterable (role=combobox)
toggle
The chevron button (filterable)
clear-filter-button
The × that clears the text (filterable)
empty
The "searching" / "no results" row (filterable)
tag
The selected-count pill
clear-button
The × inside the pill
chevron
The arrow icon
status-icon
The error / warning icon
helper-text
The helper line
warning-text
The warning line
warn-text
Same line as warning-text (named after the warn-text attribute / slot)
error-text
The error line
invalid-text
Same line as error-text (named after the invalid-text attribute / slot)
menu
The floating list
option
Each row
select-all
The "Select all" row
checkbox
The box drawn in each row
group-label
An <optgroup> heading
CSS custom property
Custom property
ค่าเริ่มต้น
คำอธิบาย
--ui-count-tag-background
The count pill's fill (default --ui-ink); also --ui-count-tag-ink (--ui-inverse-ink) and --ui-count-tag-hover (its ×, --ui-secondary-hover)
--ui-field-border
Border on every side of the box (default none: Carbon's underline only; Kanok: a 1px outline)
--ui-field-disabled-border-color
Border colour when disabled (default: the underline transparent)
--ui-field-readonly-border-color
Border colour when read-only (default: a hairline underline)
--ui-field-radius
Corner radius of the box (default 0; Kanok: 6px)
--ui-field-background
Fill of the box (default --ui-surface-1; Kanok: transparent)
--ui-label-font
Font of the label (default --ui-font-caption; Kanok: 14px semibold)
--ui-label-color
Colour of the label (default --ui-ink-muted; Kanok: --ui-ink)
--ui-menu-radius
Corner radius of the open list (default 0; Kanok: 8px)
--ui-menu-border
Border around the list (default none; Kanok: a hairline)
--ui-menu-shadow
box-shadow of the list (default Carbon's 0 2px 6px)
--ui-menu-padding
Padding of the open list (default 0; Kanok: 4px, rows inset)
--ui-menu-item-radius
Corner radius of a row (default 0; Kanok: 6px)
--ui-menu-item-ink
Text of a row at rest (default --ui-ink-muted; Kanok: --ui-ink)
--ui-menu-divider
Rule between rows (default --ui-row-divider; Kanok: none)
--ui-menu-item-selected
Fill of the chosen row (default --ui-surface-2; Kanok: --ui-highlight)
--ui-menu-item-selected-hover
The chosen row under the pointer (default --ui-layer-selected-hover; Kanok: the highlight a shade deeper)
--ui-field-readonly-background
The box's fill when read-only (default transparent; Kanok: a faint ink tint, so read-only reads as set and never weaker than disabled)
--ui-field-open-background
The box's fill while its list is open (default the field's hover fill; Kanok: transparent)
--ui-fluid-background
/ --ui-fluid-gap - A fluid box's fill and the space under it (default the layer's fill, 0: boxes that touch; Kanok: transparent, 8px)
--ui-menu-item-active-outline
/ --ui-menu-item-active-background - The keyboard-highlighted row: its ring and its fill (default focus, transparent)
--ui-option-box-align
/ --ui-option-box-offset - A two-line row's checkbox: align-self and top margin (default auto, 0: centred; Kanok: beside the label line)
--ui-icon-control-radius
Corners of its small icon buttons, their hover fill and ring (default 0; Kanok: round)
--ui-menu-item-pressed
Fill of a pressed option (default --ui-surface-2; Kanok: the 20% pressed tint)
--ui-focus-offset
outline-offset of its focus ring (default -2px: inside; Kanok: 2px outside)