Headless Select
Select คือพฤติกรรมของ Select แบบมี style ในรูป React part เดียวที่ไม่มี style: เป็น <select> จริงที่มี <option> และ <optgroup> ของคุณเป็น children เบราว์เซอร์จึงจัดการรายการ คีย์บอร์ด และฟอร์มให้ part นี้เพิ่ม option สำหรับ placeholder, โหมดอ่านอย่างเดียว, aria-invalid และ attribute data-* ไว้ให้แต่ง style
markup เป็นของคุณเอง ทั้ง label, helper text, ข้อความ error, ไอคอนลูกศร และ style ทั้งหมด ไม่มีขนาดและไม่มีแบบ inline ความสูงและการจัดวางเป็น CSS ของคุณเอง
ติดตั้ง
ส่วน headless มากับแพ็กเกจเลย (ไม่มี item ของ shadcn; item ของ React + Tailwind สร้างบนมัน):
npm install @purpurabog/ui
import { Select } from '@purpurabog/ui/headless';
วิธีใช้
import { Select } from '@purpurabog/ui/headless';
export default function Example() {
return (
<div className="grid max-w-xs gap-2">
<label htmlFor="department" className="text-xs text-ink-muted">แผนก</label>
<Select.Root
id="department"
name="department"
placeholder="เลือกแผนก"
className="h-10 border-b border-border-strong-1 bg-field-1 px-4 text-ink data-placeholder:text-ink-placeholder"
>
<option value="sales">ฝ่ายขาย</option>
<option value="legal">ฝ่ายกฎหมาย</option>
</Select.Root>
</div>
);
}
โครงสร้าง
<Select.Root>
<option />
<optgroup>
<option />
</optgroup>
</Select.Root>
label และข้อความช่วย
label และบรรทัดข้อความใต้ช่องเป็น element ของคุณเอง ผูกกับ select ด้วย htmlFor และ aria-describedby
import { Select } from '@purpurabog/ui/headless';
export default function Example() {
return (
<div className="grid max-w-xs gap-2">
<label htmlFor="filed-department" className="text-xs text-ink-muted">แผนก</label>
<Select.Root
id="filed-department"
name="department"
placeholder="เลือกแผนก"
aria-describedby="filed-department-help"
className="h-10 border-b border-border-strong-1 bg-field-1 px-4 text-sm text-ink hover:bg-field-hover-1 focus-visible:outline-2 focus-visible:-outline-offset-2 focus-visible:outline-focus data-placeholder:text-ink-placeholder"
>
<option value="sales">ฝ่ายขาย</option>
<option value="legal">ฝ่ายกฎหมาย</option>
<option value="finance">ฝ่ายการเงิน</option>
</Select.Root>
<p id="filed-department-help" className="text-xs text-ink-helper">แผนกที่เก็บสัญญานี้</p>
</div>
);
}
Placeholder และค่าเริ่มต้น
placeholder เพิ่ม option ว่างเป็นตัวแรก (value="") ซึ่งเป็นค่าที่เลือกไว้แบบ uncontrolled จนกว่าผู้ใช้จะเลือกตัวอื่น และระหว่างที่ option นี้แสดงอยู่ Root จะมี data-placeholder ถ้าใส่ required ด้วย ผู้ใช้ต้องเลือกตัวอื่นก่อนจึงส่งฟอร์มได้
ใช้ value คู่กับ onChange ถ้าต้องการคุมค่าเอง หรือ defaultValue แบบ uncontrolled onChange เป็นของ React เอง
import { useState } from 'react';
import { Select } from '@purpurabog/ui/headless';
export default function Example() {
const [currency, setCurrency] = useState('thb');
return (
<div className="grid max-w-xs gap-2">
<label htmlFor="currency" className="text-xs text-ink-muted">สกุลเงิน</label>
<Select.Root
id="currency"
name="currency"
value={currency}
onChange={(e) => setCurrency(e.target.value)}
className="h-10 border-b border-border-strong-1 bg-field-1 px-4 text-sm text-ink hover:bg-field-hover-1 focus-visible:outline-2 focus-visible:-outline-offset-2 focus-visible:outline-focus"
>
<option value="usd">ดอลลาร์สหรัฐ</option>
<option value="thb">บาท</option>
</Select.Root>
<p className="text-xs text-ink-muted">ค่า: {currency}</p>
</div>
);
}
กลุ่มและรายการที่ปิดใช้งาน
children เป็น <option> และ <optgroup> ธรรมดา disabled ที่ตัวไหนก็ทำงานเหมือน HTML
import { Select } from '@purpurabog/ui/headless';
export default function Example() {
return (
<div className="grid max-w-xs gap-2">
<label htmlFor="office" className="text-xs text-ink-muted">สาขา</label>
<Select.Root
id="office"
name="office"
placeholder="เลือกสาขา"
className="h-10 border-b border-border-strong-1 bg-field-1 px-4 text-sm text-ink hover:bg-field-hover-1 focus-visible:outline-2 focus-visible:-outline-offset-2 focus-visible:outline-focus data-placeholder:text-ink-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.Root>
</div>
);
}
อ่านอย่างเดียวและปิดใช้งาน
readOnly คงค่าไว้และตั้ง aria-readonly: กดแล้วรายการไม่เปิด และไม่มีปุ่มไหนเปลี่ยนค่าหรือเปิดรายการได้ รวมถึงการพิมพ์ตัวอักษร ส่วน Tab และ shortcut ที่กดกับ ⌘ / Ctrl ยังใช้ได้ แต่ยังรับโฟกัสได้และส่งค่าไปกับฟอร์ม disabled เป็น attribute ปกติของ <select> ค่าจะไม่ถูกส่งไปกับฟอร์ม
import { Select } from '@purpurabog/ui/headless';
export default function Example() {
return (
<div className="grid max-w-xs gap-4">
<Select.Root
aria-label="แผนก"
defaultValue="legal"
readOnly
className="h-10 border-b border-border-strong-1 bg-field-1 px-4 text-sm text-ink focus-visible:outline-2 focus-visible:-outline-offset-2 focus-visible:outline-focus data-readonly:border-border-subtle-1 data-readonly:bg-transparent"
>
<option value="sales">ฝ่ายขาย</option>
<option value="legal">ฝ่ายกฎหมาย</option>
</Select.Root>
<Select.Root
aria-label="แผนก"
disabled
className="h-10 border-b border-border-strong-1 bg-field-1 px-4 text-sm text-ink data-disabled:cursor-not-allowed data-disabled:border-transparent data-disabled:text-ink-disabled"
>
<option value="sales">ฝ่ายขาย</option>
<option value="legal">ฝ่ายกฎหมาย</option>
</Select.Root>
</div>
);
}
แต่ง style
import { useState } from 'react';
import { Select } from '@purpurabog/ui/headless';
const states = ['invalid', 'readOnly', 'disabled'] as const;
export default function Example() {
const [on, setOn] = useState<Record<string, boolean>>({});
return (
<div className="grid max-w-xs gap-2">
<label htmlFor="dept" className="text-xs text-ink-muted">แผนก</label>
<div className="relative">
<Select.Root
id="dept"
placeholder="เลือก"
invalid={on.invalid}
readOnly={on.readOnly}
disabled={on.disabled}
className="h-10 w-full appearance-none border-b border-border-strong-1 bg-field-1 pl-4 pr-10 text-sm text-ink focus-visible:outline-2 focus-visible:-outline-offset-2 focus-visible:outline-focus data-placeholder:text-ink-placeholder data-invalid:outline-2 data-invalid:-outline-offset-2 data-invalid:outline-error data-readonly:bg-transparent data-disabled:cursor-not-allowed data-disabled:text-ink-disabled"
>
<option value="hr">ฝ่ายบุคคล</option>
<option value="it">ฝ่ายไอที</option>
</Select.Root>
<svg className="pointer-events-none absolute right-4 top-3 size-4 fill-ink" viewBox="0 0 16 16" aria-hidden="true">
<path d="M8 11 3 6l.7-.7L8 9.6l4.3-4.3.7.7z" />
</svg>
</div>
<div className="flex gap-4 text-sm text-ink">
{states.map((state) => (
<label key={state} className="flex items-center gap-2">
<input type="checkbox" checked={!!on[state]} onChange={(e) => setOn({ ...on, [state]: e.target.checked })} />
{state}
</label>
))}
</div>
</div>
);
}
select { appearance: none; }
select[data-placeholder] { color: #6f6f6f; }
select[data-invalid] { outline: 2px solid #da1e28; }
select[data-readonly] { background: transparent; }
select[data-disabled] { color: #a8a8a8; }
Parts
Select.Root
render <select> โดยมี <option> ว่างเป็นตัวแรกเมื่อตั้ง placeholder ตามด้วย children ของคุณ รับ prop ของ <select> ได้ทุกตัว
| Prop |
Type |
ค่าเริ่มต้น |
|
placeholder |
string |
— |
ข้อความของ option ว่างตัวแรก (value="") ที่ถูกเลือกไว้จนกว่าผู้ใช้จะเลือกตัวอื่น |
readOnly |
boolean |
false |
คงค่าไว้ กดหรือกดปุ่มที่เปลี่ยนค่าแล้วไม่ทำอะไร (aria-readonly) |
invalid |
boolean |
— |
ตั้ง aria-invalid ถ้าไม่ใส่ก็ไม่มี aria-invalid |
| Attribute |
เมื่อ |
data-placeholder |
option ว่างของ placeholder ถูกเลือกอยู่ |
data-readonly |
ตั้ง readOnly |
data-disabled |
ตั้ง disabled |
data-invalid |
ตั้ง invalid |
คีย์บอร์ดและ accessibility
- ตัวควบคุมเป็น
<select> จริง ปุ่มของเบราว์เซอร์เปิดและเปลี่ยนค่าได้ และบนมือถือจะแสดงตัวเลือกของระบบ
- สิ่งที่คุณต้องใส่เอง:
<label> (หรือ aria-label), aria-describedby ที่ชี้ไปยัง helper text หรือข้อความ error และตัวข้อความ error
- ใส่
aria-hidden="true" และ pointer-events: none ที่ไอคอนลูกศร คลิกที่ไอคอนจึงยังไปถึง select
- option ใส่ markup หรือบรรทัดที่สองไม่ได้ ถ้าต้องการให้ใช้ Dropdown