Select
Select ใช้เลือกหนึ่งรายการจากรายการทั้งหมด ข้างในเป็น <select> ของ browser เอง มือถือจึงเปิดตัวเลือกแบบ native และ screen reader คุ้นเคยดี
ฝ่ายขาย
ฝ่ายกฎหมาย
ฝ่ายการเงิน
HTML React React + Tailwind <ui-select label ="แผนก" name ="department" placeholder ="เลือกแผนก" helper-text ="แผนกที่เก็บสัญญานี้" >
<option value ="sales" >ฝ่ายขาย</option >
<option value ="legal" >ฝ่ายกฎหมาย</option >
<option value ="finance" >ฝ่ายการเงิน</option >
</ui-select >
<UiSelect label = "แผนก" name = "department" placeholder = "เลือกแผนก" helperText = "แผนกที่เก็บสัญญานี้" >
<option value = "sales" >ฝ่ายขาย</option >
<option value = "legal" >ฝ่ายกฎหมาย</option >
<option value = "finance" >ฝ่ายการเงิน</option >
</UiSelect >
<Select label = "แผนก" name = "department" placeholder = "เลือกแผนก" helperText = "แผนกที่เก็บสัญญานี้" >
<option value = "sales" >ฝ่ายขาย</option >
<option value = "legal" >ฝ่ายกฎหมาย</option >
<option value = "finance" >ฝ่ายการเงิน</option >
</Select >
ติดตั้ง
npm shadcn · Web component shadcn · React + Tailwind คัดลอกเอง npm install @purpurabog/ui
npx shadcn@4 add @kanok-ui/select
npx shadcn@4 add @kanok-ui/tw-select
# Web component
# 1. ติดตั้ง dependency
npm install lit@^3.3.0
# 2. คัดลอก "content" ของแต่ละไฟล์จาก
# https://cdn.jsdelivr.net/npm/@purpurabog/ui@2.3.0/registry/select.json
# ไปไว้ที่ target ของมัน:
# components/ui/primitives/select/select-skeleton.ts
# components/ui/primitives/select/select.styles.ts
# components/ui/primitives/select/select.ts
# 3. ทำแบบเดียวกันกับ item ที่มันใช้ (registry/<name>.json):
# shared-element, shared-field, shared-form-control, shared-localize, shared-skeleton, 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-select.json
# ไปไว้ที่ target ของมัน:
# components/ui/react-tw/select.tsx
# 3. ทำแบบเดียวกันกับ item ที่มันใช้ (registry/<name>.json):
# core-field, tw-field, tw-skeleton, tw-theme, tw-utils
npm ให้ทุก component จากแพ็กเกจ ส่วน shadcn คัดลอกซอร์สเข้าแอปของคุณแทน เป็นสำเนาที่ล็อกเวอร์ชันนี้และแก้เองได้ คำสั่ง shadcn ต้องตั้ง registry @kanok-ui ใน components.json ครั้งเดียวก่อน ดู ติดตั้ง
วิธีใช้
HTML React React + Tailwind <script type ="module" >import '@purpurabog/ui/select' ;</script >
<ui-select label ="แผนก" name ="department" placeholder ="เลือกแผนก" >
<option value ="sales" >ฝ่ายขาย</option >
<option value ="legal" >ฝ่ายกฎหมาย</option >
</ui-select >
import { UiSelect } from '@purpurabog/ui/react' ;
<UiSelect label = "แผนก" name = "department" placeholder = "เลือกแผนก" >
<option value = "sales" >ฝ่ายขาย</option >
<option value = "legal" >ฝ่ายกฎหมาย</option >
</UiSelect >
import { Select } from '@/components/ui/react-tw/select' ;
<Select label = "แผนก" name = "department" placeholder = "เลือกแผนก" >
<option value = "sales" >ฝ่ายขาย</option >
<option value = "legal" >ฝ่ายกฎหมาย</option >
</Select >
Select, Dropdown, Combobox หรือ MultiSelect?
Component
ใช้เมื่อ
Select
ช่องในฟอร์มธรรมดา browser เป็นคนวาดรายการ มือถือเปิดตัวเลือกแบบ native และ screen reader คุ้นเคยที่สุด แต่ละรายการเป็นข้อความบรรทัดเดียว
Dropdown
อยากให้รายการหน้าตาเหมือนกันทุก browser หรือรายการต้องมีบรรทัดที่สอง (data-description)
Combobox
รายการยาวจนพิมพ์หาเร็วกว่าเลื่อนหา รายการมาจากการค้นหาบน server หรือยอมให้พิมพ์ค่าเองได้
Multi-select
ผู้ใช้เลือกได้หลายค่า
ถ้ามีสองถึงห้าตัวเลือกที่ควรเห็นพร้อมกันทั้งหมด ให้ใช้ radio group (เลือกหนึ่ง) หรือ checkbox (เลือกหลายอัน)
Placeholder และค่าเริ่มต้น
placeholder เพิ่มตัวเลือกว่างเป็นอันแรกด้วยสีจาง ถ้าใส่ required ด้วย ผู้ใช้ต้องเลือกอย่างอื่นก่อนจึงจะส่งฟอร์มได้
ถ้าไม่มี placeholder ค่าเริ่มต้นคือ value ถ้าไม่มีก็เป็น <option selected> ถ้าไม่มีอีกก็เป็นรายการแรก เหมือน select แบบ native การ reset จะกลับไปที่ค่าเริ่มต้นนี้ event change ยิงเมื่อผู้ใช้เลือกรายการ
ดอลลาร์สหรัฐ
บาท
ยูโร
ค่า:
HTML React React + Tailwind <ui-select id ="select-currency" label ="สกุลเงิน" name ="currency" value ="thb" >
<option value ="usd" >ดอลลาร์สหรัฐ</option >
<option value ="thb" >บาท</option >
<option value ="eur" >ยูโร</option >
</ui-select >
<p >ค่า: <output id ="select-currency-out" ></output ></p >
<script type ="module" >
const currency = document.getElementById ('select-currency' );
const out = document.getElementById ('select-currency-out' );
out.value = currency.value;
currency.addEventListener ('change' , () => (out.value = currency.value));
</script >
import type { UiSelect as SelectElement } from '@purpurabog/ui' ;
const [currency, setCurrency] = useState ('thb' );
<UiSelect label = "สกุลเงิน" name = "currency" value = {currency} onChange = {(e) => setCurrency ((e.target as SelectElement ).value)}>
<option value = "usd" >ดอลลาร์สหรัฐ</option >
<option value = "thb" >บาท</option >
<option value = "eur" >ยูโร</option >
</UiSelect >
<p >ค่า: <output >{currency}</output ></p >
const [currency, setCurrency] = useState ('thb' );
<Select label = "สกุลเงิน" name = "currency" value = {currency} onChange = {(e) => setCurrency (e.target.value)}>
<option value = "usd" >ดอลลาร์สหรัฐ</option >
<option value = "thb" >บาท</option >
<option value = "eur" >ยูโร</option >
</Select >
<p >ค่า: <output >{currency}</output ></p >
React + Tailwind สร้าง <select> จริง onChange จึงเป็นของ React เอง และรับ defaultValue ได้ด้วย
กลุ่มและรายการที่ปิดใช้งาน
ใส่รายการไว้ใน <optgroup label> เพื่อจัดกลุ่ม disabled ที่ option หรือ <optgroup> จะยังแสดงอยู่แต่เลือกไม่ได้ web component คอยดู children ของตัวเอง จึงเพิ่มหรือลบ <option> ได้ทุกเมื่อ
สีลม
อโศก
เชียงใหม่
ลำปาง (ปิดแล้ว)
HTML React React + Tailwind <ui-select label ="สาขา" name ="office" placeholder ="เลือกสาขา" >
<optgroup label ="กรุงเทพฯ" >
<option value ="silom" >สีลม</option >
<option value ="asok" >อโศก</option >
</optgroup >
<optgroup label ="ภาคเหนือ" >
<option value ="chiangmai" >เชียงใหม่</option >
<option value ="lampang" disabled >ลำปาง (ปิดแล้ว)</option >
</optgroup >
</ui-select >
<UiSelect label = "สาขา" name = "office" placeholder = "เลือกสาขา" >
<optgroup label = "กรุงเทพฯ" >
<option value = "silom" >สีลม</option >
<option value = "asok" >อโศก</option >
</optgroup >
<optgroup label = "ภาคเหนือ" >
<option value = "chiangmai" >เชียงใหม่</option >
<option value = "lampang" disabled >ลำปาง (ปิดแล้ว)</option >
</optgroup >
</UiSelect >
<Select label = "สาขา" name = "office" placeholder = "เลือกสาขา" >
<optgroup label = "กรุงเทพฯ" >
<option value = "silom" >สีลม</option >
<option value = "asok" >อโศก</option >
</optgroup >
<optgroup label = "ภาคเหนือ" >
<option value = "chiangmai" >เชียงใหม่</option >
<option value = "lampang" disabled >ลำปาง (ปิดแล้ว)</option >
</optgroup >
</Select >
รายการที่ขึ้นกับช่องอื่น
ฟอร์มที่อยู่เติมรายการเขต/อำเภอตามจังหวัดที่เลือก ให้เปลี่ยน option เมื่อ select ตัวแรกเปลี่ยน web component จะรับ children ใหม่เอง และปิด select ตัวที่สองไว้จนกว่าจะมีรายการให้เลือก
กรุงเทพมหานคร
เชียงใหม่
HTML React React + Tailwind <ui-select id ="select-province" label ="จังหวัด" name ="province" placeholder ="เลือกจังหวัด" >
<option value ="bkk" >กรุงเทพมหานคร</option >
<option value ="cnx" >เชียงใหม่</option >
</ui-select >
<ui-select id ="select-district" label ="เขต/อำเภอ" name ="district" placeholder ="เลือกเขต/อำเภอ" disabled ></ui-select >
<script type ="module" >
const districts = {
bkk: [['bangrak' , 'บางรัก' ], ['pathumwan' , 'ปทุมวัน' ], ['chatuchak' , 'จตุจักร' ]],
cnx: [['mueang' , 'เมืองเชียงใหม่' ], ['hangdong' , 'หางดง' ], ['sansai' , 'สันทราย' ]],
};
const province = document.getElementById ('select-province' );
const district = document.getElementById ('select-district' );
province.addEventListener ('change' , () => {
district.replaceChildren (... districts[province.value].map (([value, label]) => new Option (label, value)));
district.value = '' ;
district.disabled = false ;
});
</script >
const districts: Record <string , [string , string ][]> = {
bkk: [['bangrak' , 'บางรัก' ], ['pathumwan' , 'ปทุมวัน' ], ['chatuchak' , 'จตุจักร' ]],
cnx: [['mueang' , 'เมืองเชียงใหม่' ], ['hangdong' , 'หางดง' ], ['sansai' , 'สันทราย' ]],
};
const [province, setProvince] = useState ('' );
const [district, setDistrict] = useState ('' );
<UiSelect label = "จังหวัด" name = "province" placeholder = "เลือกจังหวัด" value = {province} onChange = {(e) => { setProvince ((e.target as SelectElement ).value); setDistrict ('' ); }}>
<option value = "bkk" >กรุงเทพมหานคร</option >
<option value = "cnx" >เชียงใหม่</option >
</UiSelect >
<UiSelect label = "เขต/อำเภอ" name = "district" placeholder = "เลือกเขต/อำเภอ" disabled = {! province} value = {district} onChange = {(e) => setDistrict ((e.target as SelectElement ).value)}>
{districts[province]?.map (([value, label]) => <option key = {value} value = {value}>{label}</option >)}
</UiSelect >
<Select label = "จังหวัด" name = "province" placeholder = "เลือกจังหวัด" value = {province} onChange = {(e) => { setProvince (e.target.value); setDistrict ('' ); }}>
<option value = "bkk" >กรุงเทพมหานคร</option >
<option value = "cnx" >เชียงใหม่</option >
</Select >
<Select label = "เขต/อำเภอ" name = "district" placeholder = "เลือกเขต/อำเภอ" disabled = {! province} value = {district} onChange = {(e) => setDistrict (e.target.value)}>
{districts[province]?.map (([value, label]) => <option key = {value} value = {value}>{label}</option >)}
</Select >
districts, province และ district เหมือนกันใน React ทุกแบบ
ขนาด
size: sm 32px, md 40px (ค่าเริ่มต้น เหมือน Carbon), lg 48px
ฝ่ายกฎหมาย
ฝ่ายกฎหมาย
ฝ่ายกฎหมาย
HTML React React + Tailwind <ui-select label ="เล็ก" size ="sm" >
<option >ฝ่ายกฎหมาย</option >
</ui-select >
<ui-select label ="กลาง" >
<option >ฝ่ายกฎหมาย</option >
</ui-select >
<ui-select label ="ใหญ่" size ="lg" >
<option >ฝ่ายกฎหมาย</option >
</ui-select >
<UiSelect label = "เล็ก" size = "sm" ><option >ฝ่ายกฎหมาย</option ></UiSelect >
<UiSelect label = "กลาง" ><option >ฝ่ายกฎหมาย</option ></UiSelect >
<UiSelect label = "ใหญ่" size = "lg" ><option >ฝ่ายกฎหมาย</option ></UiSelect >
<Select label = "เล็ก" size = "sm" ><option >ฝ่ายกฎหมาย</option ></Select >
<Select label = "กลาง" ><option >ฝ่ายกฎหมาย</option ></Select >
<Select label = "ใหญ่" size = "lg" ><option >ฝ่ายกฎหมาย</option ></Select >
แบบ inline
inline วาง label ไว้ข้าง select ที่กะทัดรัดและไม่มีขอบ ใช้ในแถบเครื่องมือหรือเหนือตาราง ไม่ใช่ในฟอร์ม
10
25
50
HTML React React + Tailwind <ui-select inline label ="แถวต่อหน้า" value ="25" >
<option value ="10" >10</option >
<option value ="25" >25</option >
<option value ="50" >50</option >
</ui-select >
<UiSelect inline label = "แถวต่อหน้า" value = "25" >
<option value = "10" >10</option >
<option value = "25" >25</option >
<option value = "50" >50</option >
</UiSelect >
<Select inline label = "แถวต่อหน้า" defaultValue = "25" >
<option value = "10" >10</option >
<option value = "25" >25</option >
<option value = "50" >50</option >
</Select >
แบบ fluid
fluid วาง label ไว้ในกล่องสูง 64px (FluidSelect ของ Carbon) ถ้าครอบด้วย fluid form ทุกช่องข้างในจะเป็นแบบนี้ ดู fluid form ช่องแบบ fluid ไม่มีบรรทัดข้อความช่วยเหลือ
ฝ่ายขาย
ฝ่ายกฎหมาย
ฝ่ายการเงิน
HTML React React + Tailwind <ui-select fluid label ="แผนก" name ="department" placeholder ="เลือกแผนก" >
<option value ="sales" >ฝ่ายขาย</option >
<option value ="legal" >ฝ่ายกฎหมาย</option >
<option value ="finance" >ฝ่ายการเงิน</option >
</ui-select >
<UiSelect fluid label = "แผนก" name = "department" placeholder = "เลือกแผนก" >
<option value = "sales" >ฝ่ายขาย</option >
<option value = "legal" >ฝ่ายกฎหมาย</option >
<option value = "finance" >ฝ่ายการเงิน</option >
</UiSelect >
<Select fluid label = "แผนก" name = "department" placeholder = "เลือกแผนก" >
<option value = "sales" >ฝ่ายขาย</option >
<option value = "legal" >ฝ่ายกฎหมาย</option >
<option value = "finance" >ฝ่ายการเงิน</option >
</Select >
แบบมีขอบ (ดีไซน์ 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-select label ="แผนก" name ="department" placeholder ="เลือกแผนก" helper-text ="แผนกที่เก็บสัญญานี้" >
<option value ="sales" >ฝ่ายขาย</option >
<option value ="legal" >ฝ่ายกฎหมาย</option >
<option value ="finance" >ฝ่ายการเงิน</option >
</ui-select >
</div >
<div data-field-variant = "outlined" >
<UiSelect label = "แผนก" name = "department" placeholder = "เลือกแผนก" helperText = "แผนกที่เก็บสัญญานี้" >
<option value = "sales" >ฝ่ายขาย</option >
<option value = "legal" >ฝ่ายกฎหมาย</option >
<option value = "finance" >ฝ่ายการเงิน</option >
</UiSelect >
</div >
<div data-field-variant = "outlined" >
<Select label = "แผนก" name = "department" placeholder = "เลือกแผนก" helperText = "แผนกที่เก็บสัญญานี้" >
<option value = "sales" >ฝ่ายขาย</option >
<option value = "legal" >ฝ่ายกฎหมาย</option >
<option value = "finance" >ฝ่ายการเงิน</option >
</Select >
</div >
ข้อความช่วยเหลือ คำเตือน และ error
ใต้ช่องแสดงได้ทีละบรรทัด error มาก่อนคำเตือน และคำเตือนมาก่อนข้อความช่วยเหลือ warn เป็นคำเตือนที่ไม่บล็อกการส่ง invalid บังคับให้แสดง error เช่น หลัง server ปฏิเสธค่า
สมชาย ก.
มาลี ส.
สมชาย ก.
มาลี ส.
สมชาย ก.
มาลี ส.
HTML React React + Tailwind <ui-select label ="ผู้อนุมัติ" name ="approver" helper-text ="ผู้อนุมัติจะได้รับสัญญาเป็นลำดับถัดไป" >
<option value ="somchai" >สมชาย ก.</option >
<option value ="malee" >มาลี ส.</option >
</ui-select >
<ui-select label ="ผู้อนุมัติ" name ="approver2" value ="malee" warn warn-text ="มาลีลาพักถึงวันที่ 20 ตุลาคม" >
<option value ="somchai" >สมชาย ก.</option >
<option value ="malee" >มาลี ส.</option >
</ui-select >
<ui-select label ="ผู้อนุมัติ" name ="approver3" value ="somchai" invalid invalid-text ="สมชายเป็นผู้ร่างสัญญานี้ จึงอนุมัติเองไม่ได้" >
<option value ="somchai" >สมชาย ก.</option >
<option value ="malee" >มาลี ส.</option >
</ui-select >
<UiSelect label = "ผู้อนุมัติ" name = "approver" helperText = "ผู้อนุมัติจะได้รับสัญญาเป็นลำดับถัดไป" >…</UiSelect >
<UiSelect label = "ผู้อนุมัติ" name = "approver2" value = "malee" warn warnText = "มาลีลาพักถึงวันที่ 20 ตุลาคม" >…</UiSelect >
<UiSelect label = "ผู้อนุมัติ" name = "approver3" value = "somchai" invalid invalidText = "สมชายเป็นผู้ร่างสัญญานี้ จึงอนุมัติเองไม่ได้" >…</UiSelect >
<Select label = "ผู้อนุมัติ" name = "approver" helperText = "ผู้อนุมัติจะได้รับสัญญาเป็นลำดับถัดไป" >…</Select >
<Select label = "ผู้อนุมัติ" name = "approver2" defaultValue = "malee" warn warnText = "มาลีลาพักถึงวันที่ 20 ตุลาคม" >…</Select >
<Select label = "ผู้อนุมัติ" name = "approver3" defaultValue = "somchai" invalid invalidText = "สมชายเป็นผู้ร่างสัญญานี้ จึงอนุมัติเองไม่ได้" >…</Select >
ลิงก์ในข้อความช่วยเหลือ
attribute helper-text, warn-text และ invalid-text เป็นข้อความธรรมดา ถ้าต้องการลิงก์หรือตัวหนา ให้ใส่ markup ใน slot ชื่อเดียวกัน ใน React + Tailwind ส่ง markup ให้ helperText, warnText หรือ invalidText
HTML React React + Tailwind <ui-select label ="ระยะเวลาเก็บเอกสาร" name ="retention" value ="10" >
<option value ="5" >5 ปี</option >
<option value ="10" >10 ปี</option >
<option value ="forever" >เก็บตลอดไป</option >
<span slot ="helper-text" >ทำตาม<a href ="#accessibility" >นโยบายการเก็บรักษาเอกสาร</a ></span >
</ui-select >
<UiSelect label = "ระยะเวลาเก็บเอกสาร" name = "retention" value = "10" >
<option value = "5" >5 ปี</option >
<option value = "10" >10 ปี</option >
<option value = "forever" >เก็บตลอดไป</option >
<span slot = "helper-text" >ทำตาม<a href = "/policies/retention" >นโยบายการเก็บรักษาเอกสาร</a ></span >
</UiSelect >
<Select label = "ระยะเวลาเก็บเอกสาร" name = "retention" defaultValue = "10" helperText = {<>ทำตาม<a href = "/policies/retention" >นโยบายการเก็บรักษาเอกสาร</a ></>}>
<option value = "5" >5 ปี</option >
<option value = "10" >10 ปี</option >
<option value = "forever" >เก็บตลอดไป</option >
</Select >
อ่านอย่างเดียวและปิดใช้งาน
readonly (readOnly ใน React + Tailwind) แสดงค่าและส่งค่าไปกับฟอร์ม แต่เมาส์และคีย์บอร์ดเปลี่ยนค่าไม่ได้ กด Tab ผ่านได้ตามปกติ
disabled ทำให้ select เป็นสีเทาและไม่ถูกส่งไปกับฟอร์ม
ฝ่ายขาย
ฝ่ายกฎหมาย
ฝ่ายขาย
ฝ่ายกฎหมาย
HTML React React + Tailwind <ui-select label ="แผนก" value ="legal" readonly >
<option value ="sales" >ฝ่ายขาย</option >
<option value ="legal" >ฝ่ายกฎหมาย</option >
</ui-select >
<ui-select label ="แผนก" disabled >
<option value ="sales" >ฝ่ายขาย</option >
<option value ="legal" >ฝ่ายกฎหมาย</option >
</ui-select >
<UiSelect label = "แผนก" value = "legal" readonly >…</UiSelect >
<UiSelect label = "แผนก" disabled >…</UiSelect >
<Select label = "แผนก" defaultValue = "legal" readOnly >…</Select >
<Select label = "แผนก" disabled >…</Select >
บังคับเลือกในฟอร์ม
ถ้าใส่ required คู่กับ placeholder ฟอร์มจะส่งไม่ได้จนกว่าผู้ใช้จะเลือกรายการ ลองกดส่งโดยยังไม่เลือกเพื่อดู error ถ้าไม่ใส่ invalid-text จะเห็นข้อความในตัว "กรุณาเลือกรายการ" ตามภาษาของหน้า
HTML React React + Tailwind <form id ="select-form" style ="display: grid; gap: 16px" >
<ui-select label ="ประเภทสัญญา" name ="type" placeholder ="เลือกประเภท" required invalid-text ="เลือกประเภทสัญญา" >
<option value ="sale" >ซื้อขาย</option >
<option value ="lease" >เช่า</option >
<option value ="service" >จ้างบริการ</option >
</ui-select >
<div ><ui-button type ="submit" >ส่ง</ui-button ></div >
</form >
<p >ค่าที่ส่ง: <output id ="select-form-out" ></output ></p >
<script type ="module" >
const form = document.getElementById ('select-form' );
const out = document.getElementById ('select-form-out' );
form.addEventListener ('submit' , (event) => {
event.preventDefault ();
out.value = [...new FormData (form)].map (([key, value]) => `${ key }=${ value }` ).join (', ' );
});
</script >
const [sent, setSent] = useState ('' );
const submit = (event: FormEvent <HTMLFormElement >) => {
event.preventDefault ();
setSent ([...new FormData (event.currentTarget)].map (([key, value]) => `${ key }=${ value }` ).join (', ' ));
};
<form onSubmit = {submit}>
<UiSelect label = "ประเภทสัญญา" name = "type" placeholder = "เลือกประเภท" required invalidText = "เลือกประเภทสัญญา" >
<option value = "sale" >ซื้อขาย</option >
<option value = "lease" >เช่า</option >
<option value = "service" >จ้างบริการ</option >
</UiSelect >
<UiButton type = "submit" >ส่ง</UiButton >
</form >
<p >ค่าที่ส่ง: <output >{sent}</output ></p >
// การตรวจค่าเป็นของแอป: `type` คือค่าของช่อง `tried` เป็น true หลังกดส่งครั้งแรก
<form onSubmit = {submit}>
<Select label = "ประเภทสัญญา" name = "type" placeholder = "เลือกประเภท" value = {type} onChange = {(e) => setType (e.target.value)} invalid = {tried && ! type} invalidText = "เลือกประเภทสัญญา" >
<option value = "sale" >ซื้อขาย</option >
<option value = "lease" >เช่า</option >
<option value = "service" >จ้างบริการ</option >
</Select >
<Button type = "submit" >ส่ง</Button >
</form >
<p >ค่าที่ส่ง: <output >{sent}</output ></p >
select ของ React + Tailwind แสดงตามที่ส่งให้ invalid และ invalidText การตรวจค่าเป็นหน้าที่ของแอป เหมือน Carbon React ดูเพิ่มที่ ฟอร์มและการตรวจค่า
Skeleton
ระหว่างโหลดรายการ ให้แสดง ui-select-skeleton แทนช่อง เป็นแถบ label กับแถบช่องที่ size เดียวกัน skeleton ถูกซ่อนจาก screen reader จึงต้องบอกว่ากำลังโหลดที่ container เช่น aria-busy
HTML React React + Tailwind <ui-select-skeleton ></ui-select-skeleton >
<ui-select-skeleton size ="sm" hide-label ></ui-select-skeleton >
import { UiSelectSkeleton } from '@purpurabog/ui/react' ;
<UiSelectSkeleton />
<UiSelectSkeleton size = "sm" hideLabel />
import { SelectSkeleton } from '@/components/ui/react-tw/select' ;
<SelectSkeleton />
<SelectSkeleton size = "sm" hideLabel />
ใน web component ให้ import จาก @purpurabog/ui/select-skeleton
Accessibility
ตัวควบคุมเป็น <select> จริง ใช้ปุ่มของ browser เปิดและเปลี่ยนค่า และมือถือแสดงตัวเลือกของระบบ
label ผูกกับ select ไว้แล้ว ใช้ hide-label เพื่อให้เหลือไว้ให้ screen reader อ่านอย่างเดียว เฉพาะเมื่อบริบทรอบ ๆ บอกอยู่แล้วว่าช่องนี้คืออะไร
ข้อความช่วยเหลือ คำเตือน และ error เป็นคำอธิบายของ select จึงถูกอ่านเมื่อโฟกัสเข้ามา
รายการใส่ markup หรือบรรทัดที่สองไม่ได้ ถ้าต้องการ ให้ใช้ Dropdown
ต้องการแค่พฤติกรรมโดยไม่มี style: Headless Select
API
สร้างจากแพ็กเกจโดยตรง ใน React ส่ง property เป็น prop ชื่อเดียวกัน ส่วน event ก็เป็น prop เช่นกัน (ดูคอลัมน์ React)
<ui-select> · React UiSelect
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
''
Shown as an empty first choice; with required the user must pick something else.
value
value
string
''
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-*).
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 FluidSelect: the label inside a 64px box, an error / warning inside it. <ui-fluid-form> sets it for every field inside.
inline
inline
boolean
false
Label beside a compact, borderless select.
Property
Property
ชนิด
คำอธิบาย
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
คำอธิบาย
change
ไม่มี (ใช้ ref)
When the user picks an option
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
คำอธิบาย
base
The field box
label
The label
input
The native select
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)
CSS custom property
Custom property
ค่าเริ่มต้น
คำอธิบาย
--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-inline-border-color
Border colour of the inline variant (default: no underline)
--ui-field-radius
Corner radius of the box (default 0; Kanok: 6px)
--ui-select-appearance
base-select opens the list in the menu look of the dropdowns (--ui-menu-*) where the browser supports a customizable select (Chrome 135+); default none, the native list (Kanok: base-select)
--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-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-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)