ช่องเบอร์โทรศัพท์
PhoneInput
A Thai phone number field that groups the digits as 081-234-5678 while you type.
ใช้กับเบอร์โทรศัพท์ในประเทศไทย 9 หรือ 10 หลัก ช่องจัดกลุ่มตัวเลขเป็น 081-234-5678 ขณะพิมพ์ และรับเฉพาะตัวเลข สำหรับเบอร์ต่างประเทศที่มีรหัสประเทศ ใช้ TextInput
import { PhoneInput } from "@bsru/ui";ตัวอย่าง
หน้าจอเก็บเบอร์
value เป็นเบอร์ที่จัดกลุ่มแล้ว onValueChange ได้เบอร์ที่มีขีดทุกครั้งที่เปลี่ยน icon ใส่ไอคอนนำหน้า
const [phone, setPhone] = useState("081-234-5678");
<PhoneInput
label="โทรศัพท์"
icon={Phone}
autoComplete="tel-national"
value={phone}
onValueChange={setPhone}
/>| Prop | ค่าในตัวอย่างนี้ | ชนิดข้อมูล | คำอธิบาย |
|---|---|---|---|
| label | "โทรศัพท์" | string | The visible label, already in the screen language. Every field needs one, unless the screen labels it another way. |
| icon | Phone | LucideIcon | A leading icon, such as Phone. |
| autoComplete | "tel-national" | string | — |
| value | phone | string | The number, grouped, when the screen keeps it. |
| onValueChange | setPhone | (next: string) => void | Hears the grouped number on each change. Remove the dashes before sending it to the core API. |
ช่องว่าง ช่องบังคับ
placeholder บอกรูปแบบ ลองพิมพ์ตัวเลขติดกัน ช่องจะใส่ขีดให้เอง ไม่ส่ง value ช่องจะเก็บค่าเอง อ่านจากฟอร์มตอนส่งด้วย name
ตัวเลข 9 หรือ 10 หลัก
<PhoneInput
label="เบอร์ติดต่อผู้ประสานงาน"
hint="ตัวเลข 9 หรือ 10 หลัก"
required
name="contactPhone"
placeholder="0XX-XXX-XXXX"
/>| Prop | ค่าในตัวอย่างนี้ | ชนิดข้อมูล | คำอธิบาย |
|---|---|---|---|
| label | "เบอร์ติดต่อผู้ประสานงาน" | string | The visible label, already in the screen language. Every field needs one, unless the screen labels it another way. |
| hint | "ตัวเลข 9 หรือ 10 หลัก" | 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. |
| name | "contactPhone" | string | — |
| placeholder | "0XX-XXX-XXXX" | string | — |
| onValueChange | ไม่ได้ส่งในตัวอย่างนี้ | (next: string) => void | Hears the grouped number on each change. Remove the dashes before sending it to the core API. |
ข้อผิดพลาด
ตรวจจำนวนหลักตอนบันทึก แล้วส่ง error ที่บอกว่าผิดอะไรและแก้อย่างไร
เบอร์ยังไม่ครบ ใส่ตัวเลข 10 หลัก
<PhoneInput
label="โทรศัพท์มือถือ"
required
name="mobile"
defaultValue="081-234"
error="เบอร์ยังไม่ครบ ใส่ตัวเลข 10 หลัก"
/>| 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. |
| name | "mobile" | string | — |
| defaultValue | "081-234" | string | The starting number, already grouped, such as "081-234-5678". |
| error | "เบอร์ยังไม่ครบ ใส่ตัวเลข 10 หลัก" | string | What's wrong and how to fix it, shown under the field. It marks the field invalid. |
| onValueChange | ไม่ได้ส่งในตัวอย่างนี้ | (next: string) => void | Hears the grouped number on each change. Remove the dashes before sending it to the core API. |
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 Phone. |
| value | string | — | The number, grouped, when the screen keeps it. |
| defaultValue | string | "" | The starting number, already grouped, such as "081-234-5678". |
| onValueChange | (next: string) => void | — | Hears the grouped number on each change. Remove the dashes before sending it to the core API. |
รับ attribute ของ element HTML ต้นแบบได้ทั้งหมดด้วย เช่น id, name, required และ disabled
ข้อควรปฏิบัติ
- ค่าที่ได้มีขีด ให้ตัดขีดก่อนส่งไปยัง core API
- บนมือถือจะเปิดแป้นตัวเลข และรับได้ไม่เกิน 10 หลัก
- ห้ามใช้เบอร์ของบุคคลจริงในตัวอย่าง
ซอร์สโค้ด: ui/src/inputs.tsx