ช่องข้อความ
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="เช่น จัดซื้อครุภัณฑ์ห้องปฏิบัติการ"
/>| Prop | ค่าในตัวอย่างนี้ | ชนิดข้อมูล | คำอธิบาย |
|---|---|---|---|
| label | "ชื่อโครงการ" | string | The visible label, already in the screen language. Every field needs one, unless the screen labels it another way. |
| hint | "ไม่เกิน 120 ตัวอักษร" | string | Help under the field, such as the format or a limit. |
| placeholder | "เช่น จัดซื้อครุภัณฑ์ห้องปฏิบัติการ" | string | — |
| onValueChange | ไม่ได้ส่งในตัวอย่างนี้ | (next: string) => void | Hears the text on each change. |
จำเป็นต้องกรอก พร้อมไอคอน
required ใส่ดาวสีแดงที่ป้ายกำกับและบอกเบราว์เซอร์ว่าต้องกรอก icon วางไอคอนไว้หน้าข้อความ
<TextInput
label="ชื่อผู้ขอ"
required
icon={User}
defaultValue="นางสาวทดสอบ ตัวอย่าง"
/>| Prop | ค่าในตัวอย่างนี้ | ชนิดข้อมูล | คำอธิบาย |
|---|---|---|---|
| label | "ชื่อผู้ขอ" | string | The visible label, already in the screen language. Every field needs one, unless the screen labels it another way. |
| required | true | boolean | Marks the label with a red star, and the control as required. |
| icon | User | LucideIcon | A leading icon, such as Mail. |
| defaultValue | "นางสาวทดสอบ ตัวอย่าง" | string | The starting text, when the input keeps it. |
| onValueChange | ไม่ได้ส่งในตัวอย่างนี้ | (next: string) => void | Hears the text on each change. |
ข้อผิดพลาด
ส่ง error เป็นข้อความที่บอกว่าผิดอะไรและแก้อย่างไร ช่องจะถูกทำเครื่องหมายว่าไม่ถูกต้อง และโปรแกรมอ่านหน้าจออ่านข้อความนั้น type email เปิดแป้นพิมพ์อีเมลบนมือถือ
กรอกอีเมลให้ครบ เช่น somchai@example.com
<TextInput
label="อีเมลผู้ติดต่อ"
type="email"
icon={Mail}
required
defaultValue="somchai@example"
error="กรอกอีเมลให้ครบ เช่น somchai@example.com"
/>| Prop | ค่าในตัวอย่างนี้ | ชนิดข้อมูล | คำอธิบาย |
|---|---|---|---|
| label | "อีเมลผู้ติดต่อ" | string | The visible label, already in the screen language. Every field needs one, unless the screen labels it another way. |
| type | "email" | "text" | "email" | "url" | "date" | "time" | "datetime-local" | "month" | "week" ค่าที่ใช้ได้: "text""email""url""date""time""datetime-local""month""week" | 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. |
| icon | Mail | LucideIcon | A leading icon, such as Mail. |
| required | true | boolean | Marks the label with a red star, and the control as required. |
| defaultValue | "somchai@example" | string | The starting text, when the input keeps it. |
| error | "กรอกอีเมลให้ครบ เช่น somchai@example.com" | string | What's wrong and how to fix it, shown under the field. It marks the field invalid. |
| onValueChange | ไม่ได้ส่งในตัวอย่างนี้ | (next: string) => void | Hears the text on each change. |
วันที่
type date ใช้ตัวเลือกวันที่ของเบราว์เซอร์ ค่าเป็นวันที่แบบ ISO เช่น 2026-12-15 จึงส่งให้ core API ได้ตรง ๆ
ต้องไม่ก่อนวันที่ยื่นคำขอ
<TextInput
label="วันที่ต้องการใช้"
type="date"
icon={CalendarDays}
defaultValue="2026-12-15"
hint="ต้องไม่ก่อนวันที่ยื่นคำขอ"
/>| Prop | ค่าในตัวอย่างนี้ | ชนิดข้อมูล | คำอธิบาย |
|---|---|---|---|
| label | "วันที่ต้องการใช้" | string | The visible label, already in the screen language. Every field needs one, unless the screen labels it another way. |
| type | "date" | "text" | "email" | "url" | "date" | "time" | "datetime-local" | "month" | "week" ค่าที่ใช้ได้: "text""email""url""date""time""datetime-local""month""week" | 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. |
| icon | CalendarDays | LucideIcon | A leading icon, such as Mail. |
| defaultValue | "2026-12-15" | string | The starting text, when the input keeps it. |
| hint | "ต้องไม่ก่อนวันที่ยื่นคำขอ" | string | Help under the field, such as the format or a limit. |
| onValueChange | ไม่ได้ส่งในตัวอย่างนี้ | (next: string) => void | Hears the text on each change. |
หน้าจอเก็บค่าเอง
ส่ง value และ onValueChange เมื่อหน้าจอต้องใช้ค่าระหว่างพิมพ์ เช่น ตรวจหรือแสดงผลข้าง ๆ ถ้าแค่อ่านค่าตอนส่งฟอร์ม ใช้ defaultValue และ name ลองพิมพ์
24 ตัวอักษร
const [value, setValue] = useState("สำนักงานตัวอย่าง อาคาร 9");
<TextInput
label="สถานที่ส่งของ"
value={value}
onValueChange={setValue}
hint={`${value.length} ตัวอักษร`}
/>| Prop | ค่าในตัวอย่างนี้ | ชนิดข้อมูล | คำอธิบาย |
|---|---|---|---|
| label | "สถานที่ส่งของ" | string | The visible label, already in the screen language. Every field needs one, unless the screen labels it another way. |
| value | value | string | The text, when the screen keeps it. |
| onValueChange | setValue | (next: string) => void | Hears the text on each change. |
| hint | `${value.length} ตัวอักษร` | string | Help under the field, such as the format or a limit. |
วันและเวลา เดือน และสัปดาห์
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" />| Prop | ค่า 1 | ค่า 2 | ค่า 3 | ชนิดข้อมูล | คำอธิบาย |
|---|---|---|---|---|---|
| label | "วันและเวลา" | "เดือน" | "สัปดาห์" | string | The visible label, already in the screen language. Every field needs one, unless the screen labels it another way. |
| type | "datetime-local" | "month" | "week" | "text" | "email" | "url" | "date" | "time" | "datetime-local" | "month" | "week" ค่าที่ใช้ได้: "text""email""url""date""time""datetime-local""month""week" | 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. |
| defaultValue | "2026-12-08T09:30" | "2026-12" | "2026-W50" | string | The starting text, when the input keeps it. |
| onValueChange | ไม่ได้ส่งในตัวอย่างนี้ | (next: string) => void | Hears the text on each change. | ||
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. |
| icon | LucideIcon | — | 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. |
| value | string | — | The text, when the screen keeps it. |
| defaultValue | string | "" | 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