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

ช่องข้อความ

TextInput

A one-line text field, with an optional leading icon. Use the more specific inputs for money, phone and search.

ใช้กรอกข้อความบรรทัดเดียว เช่น ชื่อ อีเมล หรือวันที่ ส่ง label hint และ error เป็นข้อความ ช่องจะวางป้ายกำกับและข้อผิดพลาดให้เอง ถ้าเป็นจำนวนเงิน เบอร์โทรศัพท์ การค้นหา หรือรหัสผ่าน ใช้ช่องเฉพาะของแต่ละแบบ ถ้าข้อความยาวหลายบรรทัด ใช้ CountedTextarea

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

ตัวอย่าง

ป้ายกำกับและคำแนะนำ

label อยู่เหนือช่อง hint อยู่ใต้ช่องและโปรแกรมอ่านหน้าจออ่านพร้อมช่อง placeholder เป็นตัวอย่าง ไม่ใช่ป้ายกำกับ

ไม่เกิน 120 ตัวอักษร

<TextInput
  label="ชื่อโครงการ"
  hint="ไม่เกิน 120 ตัวอักษร"
  placeholder="เช่น จัดซื้อครุภัณฑ์ห้องปฏิบัติการ"
/>

จำเป็นต้องกรอก พร้อมไอคอน

required ใส่ดาวสีแดงที่ป้ายกำกับและบอกเบราว์เซอร์ว่าต้องกรอก icon วางไอคอนไว้หน้าข้อความ

<TextInput
  label="ชื่อผู้ขอ"
  required
  icon={User}
  defaultValue="นางสาวทดสอบ ตัวอย่าง"
/>

ข้อผิดพลาด

ส่ง error เป็นข้อความที่บอกว่าผิดอะไรและแก้อย่างไร ช่องจะถูกทำเครื่องหมายว่าไม่ถูกต้อง และโปรแกรมอ่านหน้าจออ่านข้อความนั้น type email เปิดแป้นพิมพ์อีเมลบนมือถือ

กรอกอีเมลให้ครบ เช่น somchai@example.com

<TextInput
  label="อีเมลผู้ติดต่อ"
  type="email"
  icon={Mail}
  required
  defaultValue="somchai@example"
  error="กรอกอีเมลให้ครบ เช่น somchai@example.com"
/>

วันที่

type date ใช้ตัวเลือกวันที่ของเบราว์เซอร์ ค่าเป็นวันที่แบบ ISO เช่น 2026-12-15 จึงส่งให้ core API ได้ตรง ๆ

ต้องไม่ก่อนวันที่ยื่นคำขอ

<TextInput
  label="วันที่ต้องการใช้"
  type="date"
  icon={CalendarDays}
  defaultValue="2026-12-15"
  hint="ต้องไม่ก่อนวันที่ยื่นคำขอ"
/>

หน้าจอเก็บค่าเอง

ส่ง value และ onValueChange เมื่อหน้าจอต้องใช้ค่าระหว่างพิมพ์ เช่น ตรวจหรือแสดงผลข้าง ๆ ถ้าแค่อ่านค่าตอนส่งฟอร์ม ใช้ defaultValue และ name ลองพิมพ์

24 ตัวอักษร

const [value, setValue] = useState("สำนักงานตัวอย่าง อาคาร 9");

<TextInput
  label="สถานที่ส่งของ"
  value={value}
  onValueChange={setValue}
  hint={`${value.length} ตัวอักษร`}
/>

วันและเวลา เดือน และสัปดาห์

type เลือกตัวเลือกของเบราว์เซอร์ ค่าเป็นข้อความ ISO: datetime-local เป็น 2026-12-08T09:30 month เป็น 2026-12 และ week เป็น 2026-W50

<TextInput label="วันและเวลา" type="datetime-local" defaultValue="2026-12-08T09:30" />
<TextInput label="เดือน" type="month" defaultValue="2026-12" />
<TextInput label="สัปดาห์" type="week" defaultValue="2026-W50" />

props ทั้งหมด

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

TextInput 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.
iconLucideIcon—A leading icon, such as Mail.
type"text" | "email" | "url" | "date" | "time" | "datetime-local" | "month" | "week""text"The kind of text. The date and time kinds use the browser's picker, and their values are ISO text: date 2026-11-02, time 09:30, datetime-local 2026-11-02T09:30, month 2026-11 and week 2026-W45.
valuestring—The text, when the screen keeps it.
defaultValuestring""The starting text, when the input keeps it.
onValueChange(next: string) => void—Hears the text on each change.

รับ attribute ของ element HTML ต้นแบบได้ทั้งหมดด้วย เช่น id, name, required และ disabled

ข้อควรปฏิบัติ

  • ทุกช่องต้องมี label ที่มองเห็นได้ ส่งมาในภาษาของหน้าจออยู่แล้ว
  • แสดง error หลังผู้ใช้ออกจากช่องหรือกดส่ง ไม่ใช่ระหว่างพิมพ์ และย้ายโฟกัสไปที่ช่องแรกที่ผิด
  • รับแอตทริบิวต์ของ input ได้ เช่น name placeholder autoComplete และ disabled

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