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

ช่องจำนวนเงิน

MoneyInput

An amount field. Separators go while you type and come back, with two decimals, when you leave it.

ใช้กับจำนวนเงินบาททุกช่อง ตัวเลขชิดขวา คั่นหลักและแสดงทศนิยมสองตำแหน่งเมื่อออกจากช่อง value และ onValueChange เป็นตัวเลข ไม่ใช่ข้อความ สำหรับจำนวนนับ เช่น จำนวนชิ้น ใช้ NumberStepper

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

ตัวอย่าง

หน้าจอเก็บจำนวนเงิน

value เป็นตัวเลข เช่น 150000 ช่องแสดงเป็น 150,000.00 onValueChange ได้ตัวเลขเมื่อออกจากช่อง หรือ null ถ้าว่างหรือไม่ใช่ตัวเลข

จัดรูปแบบให้เมื่อออกจากช่อง

const [amount, setAmount] = useState<number | null>(150000);

<MoneyInput
  label="จำนวนเงิน (บาท)"
  hint="จัดรูปแบบให้เมื่อออกจากช่อง"
  value={amount}
  onValueChange={setAmount}
/>

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

placeholder บอกรูปแบบ required ใส่ดาวสีแดงที่ป้าย ไม่ส่ง value ช่องจะเก็บค่าเอง และแจ้งผ่าน onValueChange

<MoneyInput
  label="วงเงินที่ขอ (บาท)"
  required
  placeholder="0.00"
  onValueChange={handleValueChange}
/>

เกินงบคงเหลือ

หน้าจอเทียบจำนวนเงินกับงบคงเหลือ แล้วส่ง error ที่บอกว่าผิดอะไรและแก้อย่างไร ลองแก้เป็น 400000 ข้อความจะหายไป

เกินงบคงเหลือ 500,000.00 บาท ลดวงเงินหรือเลือกรหัสงบอื่น

const [amount, setAmount] = useState<number | null>(600000);

<MoneyInput
  label="จำนวนเงิน (บาท)"
  required
  value={amount}
  onValueChange={setAmount}
  error={(amount ?? 0) > 500000 ? "เกินงบคงเหลือ 500,000.00 บาท ลดวงเงินหรือเลือกรหัสงบอื่น" : undefined}
/>

props ทั้งหมด

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

MoneyInput 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.
valuenumber | null—The amount, when the screen keeps it. null when empty.
defaultValuenumber | null—The starting amount, when the input keeps it.
onValueChange(next: number | null) => void—Hears the amount as a number when people leave the field, or null when it is empty or not a number.

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

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

  • ใช้ตัวเลขจาก onValueChange บันทึกได้ทันที ไม่ต้องตัดจุลภาคเอง
  • ขณะพิมพ์จะไม่มีตัวคั่นหลัก บนมือถือจะเปิดแป้นตัวเลขที่มีจุดทศนิยม
  • บอกหน่วยเงินในป้ายกำกับ เช่น (บาท)

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