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

ช่องเบอร์โทรศัพท์

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}
/>

ช่องว่าง ช่องบังคับ

placeholder บอกรูปแบบ ลองพิมพ์ตัวเลขติดกัน ช่องจะใส่ขีดให้เอง ไม่ส่ง value ช่องจะเก็บค่าเอง อ่านจากฟอร์มตอนส่งด้วย name

ตัวเลข 9 หรือ 10 หลัก

<PhoneInput
  label="เบอร์ติดต่อผู้ประสานงาน"
  hint="ตัวเลข 9 หรือ 10 หลัก"
  required
  name="contactPhone"
  placeholder="0XX-XXX-XXXX"
/>

ข้อผิดพลาด

ตรวจจำนวนหลักตอนบันทึก แล้วส่ง error ที่บอกว่าผิดอะไรและแก้อย่างไร

เบอร์ยังไม่ครบ ใส่ตัวเลข 10 หลัก

<PhoneInput
  label="โทรศัพท์มือถือ"
  required
  name="mobile"
  defaultValue="081-234"
  error="เบอร์ยังไม่ครบ ใส่ตัวเลข 10 หลัก"
/>

props ทั้งหมด

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

PhoneInput 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 Phone.
valuestring—The number, grouped, when the screen keeps it.
defaultValuestring""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