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

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

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 control the library doesn't wrap, such as a group of radio buttons, passing the control as a function of its props.

ช่องกรอกของไลบรารีรับ label hint error และ required อยู่แล้ว หน้าจอจึงแทบไม่ต้องใช้ FormField เอง ใช้เฉพาะกับตัวควบคุมที่ไลบรารียังไม่มี หรือหลายช่องที่ใช้ป้ายเดียวกัน เช่น ช่วงวันที่จาก DateInput สองช่อง หรือกลุ่มปุ่มตัวเลือก FormField วางป้ายกำกับ คำแนะนำ และข้อผิดพลาด และส่ง id กับแอตทริบิวต์ให้ตัวควบคุมผ่านฟังก์ชัน

import { FormField } from "@bsru/ui";

ตัวอย่าง

ช่วงวันที่

ส่ง group เมื่อหลายช่องใช้ป้ายเดียวกัน FormField ตั้งชื่อกลุ่ม และส่ง aria-describedby ให้ส่งต่อไปยัง DateInput ทุกช่อง โปรแกรมอ่านหน้าจอจึงอ่านคำแนะนำและข้อผิดพลาดพร้อมแต่ละช่อง placeholder เป็นชื่อของแต่ละช่อง และ min ของวันสิ้นสุดเป็นวันเริ่ม ถ้าไม่ต้องแยกป้ายหรือข้อผิดพลาดของแต่ละช่อง ใช้ DateInput ที่ kind เป็น range ช่องเดียวแทน ลองเลือก

ช่วงวันที่ตรวจรับ

เลือกวันเริ่มและวันสิ้นสุด

const [from, setFrom] = useState("2026-12-15");
const [to, setTo] = useState("2026-12-19");
const error = from && to && to < from ? "วันที่สิ้นสุดต้องไม่ก่อนวันที่เริ่ม" : undefined;

<FormField label="ช่วงวันที่ตรวจรับ" hint="เลือกวันเริ่มและวันสิ้นสุด" error={error} required group>
  {(control) => (
    <div role="group" {...control} className="grid gap-2 sm:grid-cols-2">
      <DateInput
        placeholder="ตั้งแต่วันที่"
        value={from}
        onValueChange={setFrom}
        required
        aria-describedby={control["aria-describedby"]}
        aria-invalid={!!error}
      />
      <DateInput
        placeholder="ถึงวันที่"
        value={to}
        onValueChange={setTo}
        min={from || undefined}
        required
        aria-describedby={control["aria-describedby"]}
        aria-invalid={!!error}
      />
    </div>
  )}
</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-semibold">{k.label}</span>
            <span className="block text-sm text-ink-600">{k.note}</span>
          </span>
        </label>
      ))}
    </div>
  )}
</FormField>

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

หน้าจอตรวจค่าแล้วส่ง error เมื่อวันสิ้นสุดอยู่ก่อนวันเริ่ม ทั้งสองช่องได้ aria-invalid และอ่านข้อความผิดพลาดพร้อมช่อง แก้วันสิ้นสุดแล้วข้อความหายไป

ช่วงวันที่ตรวจรับ

เลือกวันเริ่มและวันสิ้นสุด

วันที่สิ้นสุดต้องไม่ก่อนวันที่เริ่ม

const [from, setFrom] = useState("2026-12-15");
const [to, setTo] = useState("2026-12-10");
const error = from && to && to < from ? "วันที่สิ้นสุดต้องไม่ก่อนวันที่เริ่ม" : undefined;

<FormField label="ช่วงวันที่ตรวจรับ" hint="เลือกวันเริ่มและวันสิ้นสุด" error={error} required group>
  {(control) => (
    <div role="group" {...control} className="grid gap-2 sm:grid-cols-2">
      <DateInput
        placeholder="ตั้งแต่วันที่"
        value={from}
        onValueChange={setFrom}
        required
        aria-describedby={control["aria-describedby"]}
        aria-invalid={!!error}
      />
      <DateInput
        placeholder="ถึงวันที่"
        value={to}
        onValueChange={setTo}
        min={from || undefined}
        required
        aria-describedby={control["aria-describedby"]}
        aria-invalid={!!error}
      />
    </div>
  )}
</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.
hideLabelbooleanfalseKeeps the label for screen readers only, when the control already shows what it is, such as in a table's filter bar.
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 ดูก่อนว่ามีช่องของไลบรารีหรือยัง เช่น วันที่ เดือน และสัปดาห์ใช้ DateInput และเวลาใช้ TextInput ที่ type เป็น time
  • กระจายแอตทริบิวต์ที่ได้ลงบนตัวควบคุมเสมอ ไม่ตั้ง id หรือ aria-describedby เอง ถ้าต้องการ id ที่รู้ค่า ส่ง id ให้ FormField
  • ส่ง label hint และ error ในภาษาของหน้าจออยู่แล้ว error บอกว่าผิดอะไรและแก้อย่างไร

ซอร์สโค้ด: ui/src/field.tsx