ข้ามไปยังเนื้อหา

ช่องกรอกพร้อมป้ายกำกับ

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>

กลุ่มตัวควบคุม

ส่ง 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>

คำแนะนำและข้อผิดพลาด

เมื่อส่ง 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>

props ทั้งหมด

ตัวอย่างด้านบนใช้ props เพียงบางตัว ตารางนี้แสดงทุกตัวที่คอมโพเนนต์รับ พร้อมค่าเริ่มต้นและค่าที่ใช้ได้

FormField props
ชื่อชนิดข้อมูลค่าเริ่มต้นคำอธิบาย
labelstring—The visible label, already in the screen language. Every field needs one, unless the screen labels it another way.
hintstring—Help under the field, such as the format or a limit.
errorstring—What's wrong and how to fix it, shown under the field. It marks the field invalid.
requiredboolean—Marks the label with a red star, and the control as required.
idstring—The control's id. One is made when it's left out.
groupbooleanfalseThe control is a group of inputs, such as code boxes. The label then names the group instead of one input.
classNamestring—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} />.

FieldControl fields
ชื่อชนิดข้อมูลค่าเริ่มต้นคำอธิบาย
id* จำเป็นstring—The control's id, which the label points to.
aria-labelledbystring—For a group, the label's id, which names the group.
aria-describedbystring——
aria-invalidtrue——
requiredtrue—For a single control.
aria-requiredtrue—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