ช่องกรอกพร้อมป้ายกำกับ
FormField
A form control with its label above, and its hint and error below, with the ids wired for screen readers. The library's inputs use it when given a label, hint or error, so screens rarely need it directly. Use it for a native control the library doesn't wrap, such as a date input, passing the control as a function of its props.
ช่องกรอกของไลบรารีรับ label hint error และ required อยู่แล้ว หน้าจอจึงแทบไม่ต้องใช้ FormField เอง ใช้เฉพาะกับตัวควบคุมของเบราว์เซอร์ที่ไลบรารียังไม่มี เช่น วันและเวลา หรือกลุ่มปุ่มตัวเลือก FormField วางป้ายกำกับ คำแนะนำ และข้อผิดพลาด และส่ง id กับแอตทริบิวต์ให้ตัวควบคุมผ่านฟังก์ชัน
import { FormField } from "@bsru/ui";ตัวอย่าง
ส่งตัวควบคุมเป็นฟังก์ชัน
children เป็นฟังก์ชันที่รับ id และแอตทริบิวต์ aria แล้วคืนตัวควบคุม กระจายแอตทริบิวต์ทั้งหมดลงบนตัวควบคุม
ในเวลาราชการ 08:30 ถึง 16:30
<FormField label="วันและเวลานัดตรวจรับ" hint="ในเวลาราชการ 08:30 ถึง 16:30" required>
{(control) => (
<input type="datetime-local" className="input" {...control} />
)}
</FormField>| Prop | ค่าในตัวอย่างนี้ | ชนิดข้อมูล | คำอธิบาย |
|---|---|---|---|
| label | "วันและเวลานัดตรวจรับ" | string | The visible label, already in the screen language. Every field needs one, unless the screen labels it another way. |
| hint | "ในเวลาราชการ 08:30 ถึง 16:30" | string | Help under the field, such as the format or a limit. |
| required | true | boolean | Marks the label with a red star, and the control as required. |
| children* จำเป็น | ไม่ได้ส่งในตัวอย่างนี้ | (control: FieldControl) => ReactNode | Renders the control, given the attributes to put on it. |
กลุ่มตัวควบคุม
ส่ง group เมื่อมีหลายตัวควบคุม ป้ายกำกับจะเป็นชื่อของกลุ่มผ่าน aria-labelledby แทนที่จะชี้ไปที่ช่องเดียว และ required กลายเป็น aria-required กระจายแอตทริบิวต์ลงบนกลุ่ม ลองเลือก
ประเภทคำขอ
เลือกตามลักษณะของงาน
const [kind, setKind] = useState("buy");
<FormField label="ประเภทคำขอ" hint="เลือกตามลักษณะของงาน" required group>
{(control) => (
<div role="radiogroup" {...control} className="grid gap-2 md:grid-cols-3">
{kinds.map((k) => (
<label key={k.value} className="radio-card">
<input type="radio" name="kind" className="check mt-0.5" value={k.value} checked={kind === k.value} onChange={() => setKind(k.value)} />
<span>
<span className="block font-medium">{k.label}</span>
<span className="block text-sm text-ink-600">{k.note}</span>
</span>
</label>
))}
</div>
)}
</FormField>| Prop | ค่าในตัวอย่างนี้ | ชนิดข้อมูล | คำอธิบาย |
|---|---|---|---|
| label | "ประเภทคำขอ" | string | The visible label, already in the screen language. Every field needs one, unless the screen labels it another way. |
| hint | "เลือกตามลักษณะของงาน" | string | Help under the field, such as the format or a limit. |
| required | true | boolean | Marks the label with a red star, and the control as required. |
| group | true | boolean | The control is a group of inputs, such as code boxes. The label then names the group instead of one input. |
| options | รายการ 3 รายการ[
{ value: "buy", label: "ซื้อ", note: "ครุภัณฑ์หรือวัสดุ" },
{ value: "hire", label: "จ้าง", note: "งานบริการหรือซ่อมแซม" },
{ value: "rent", label: "เช่า", note: "อาคาร ยานพาหนะ หรือซอฟต์แวร์" },
] | array | — |
| children* จำเป็น | ไม่ได้ส่งในตัวอย่างนี้ | (control: FieldControl) => ReactNode | Renders the control, given the attributes to put on it. |
คำแนะนำและข้อผิดพลาด
เมื่อส่ง hint หรือ error ตัวควบคุมได้ aria-describedby ที่ชี้ไปที่ข้อความนั้น error ยังให้ aria-invalid ด้วย โปรแกรมอ่านหน้าจอจึงอ่านข้อความพร้อมตัวควบคุม
ในเวลาราชการ 08:30 ถึง 16:30
เลือกเวลาหลังวันส่งของ 10 ธ.ค. 2569
<FormField
label="วันและเวลานัดตรวจรับ"
hint="ในเวลาราชการ 08:30 ถึง 16:30"
error="เลือกเวลาหลังวันส่งของ 10 ธ.ค. 2569"
required
>
{(control) => (
<input type="datetime-local" className="input" {...control} />
)}
</FormField>| Prop | ค่าในตัวอย่างนี้ | ชนิดข้อมูล | คำอธิบาย |
|---|---|---|---|
| label | "วันและเวลานัดตรวจรับ" | string | The visible label, already in the screen language. Every field needs one, unless the screen labels it another way. |
| hint | "ในเวลาราชการ 08:30 ถึง 16:30" | string | Help under the field, such as the format or a limit. |
| error | "เลือกเวลาหลังวันส่งของ 10 ธ.ค. 2569" | string | What's wrong and how to fix it, shown under the field. It marks the field invalid. |
| required | true | boolean | Marks the label with a red star, and the control as required. |
| children* จำเป็น | ไม่ได้ส่งในตัวอย่างนี้ | (control: FieldControl) => ReactNode | Renders the control, given the attributes to put on it. |
props ทั้งหมด
ตัวอย่างด้านบนใช้ props เพียงบางตัว ตารางนี้แสดงทุกตัวที่คอมโพเนนต์รับ พร้อมค่าเริ่มต้นและค่าที่ใช้ได้
| ชื่อ | ชนิดข้อมูล | ค่าเริ่มต้น | คำอธิบาย |
|---|---|---|---|
| label | string | — | The visible label, already in the screen language. Every field needs one, unless the screen labels it another way. |
| hint | string | — | Help under the field, such as the format or a limit. |
| error | string | — | What's wrong and how to fix it, shown under the field. It marks the field invalid. |
| required | boolean | — | Marks the label with a red star, and the control as required. |
| id | string | — | The control's id. One is made when it's left out. |
| group | boolean | false | The control is a group of inputs, such as code boxes. The label then names the group instead of one input. |
| className | string | — | Classes for the wrapper, such as a width. |
| children* จำเป็น | (control: FieldControl) => ReactNode | — | Renders the control, given the attributes to put on it. |
ชนิดข้อมูล
FieldControl
The attributes FormField gives the control inside it, so the label, hint and error are announced with it. They are all real HTML attributes, so spread them onto the control: <input {...control} />.
| ชื่อ | ชนิดข้อมูล | ค่าเริ่มต้น | คำอธิบาย |
|---|---|---|---|
| id* จำเป็น | string | — | The control's id, which the label points to. |
| aria-labelledby | string | — | For a group, the label's id, which names the group. |
| aria-describedby | string | — | — |
| aria-invalid | true | — | — |
| required | true | — | For a single control. |
| aria-required | true | — | For a group, where required doesn't apply. |
ข้อควรปฏิบัติ
- ก่อนใช้ FormField ดูก่อนว่ามีช่องของไลบรารีหรือยัง เช่น TextInput รับ type date และ time แล้ว
- กระจายแอตทริบิวต์ที่ได้ลงบนตัวควบคุมเสมอ ไม่ตั้ง id หรือ aria-describedby เอง ถ้าต้องการ id ที่รู้ค่า ส่ง id ให้ FormField
- ส่ง label hint และ error ในภาษาของหน้าจออยู่แล้ว error บอกว่าผิดอะไรและแก้อย่างไร
ซอร์สโค้ด: ui/src/field.tsx