ช่องจำนวนเงิน
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}
/>| Prop | ค่าในตัวอย่างนี้ | ชนิดข้อมูล | คำอธิบาย |
|---|---|---|---|
| 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. |
| value | amount | number | null | The amount, when the screen keeps it. null when empty. |
| onValueChange | setAmount | (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. |
ช่องว่าง ช่องบังคับ
placeholder บอกรูปแบบ required ใส่ดาวสีแดงที่ป้าย ไม่ส่ง value ช่องจะเก็บค่าเอง และแจ้งผ่าน onValueChange
<MoneyInput
label="วงเงินที่ขอ (บาท)"
required
placeholder="0.00"
onValueChange={handleValueChange}
/>| 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. |
| placeholder | "0.00" | string | — |
| onValueChange | handleValueChange | (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. |
เกินงบคงเหลือ
หน้าจอเทียบจำนวนเงินกับงบคงเหลือ แล้วส่ง 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}
/>| 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. |
| value | amount | number | null | The amount, when the screen keeps it. null when empty. |
| onValueChange | setAmount | (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. |
| error | (amount ?? 0) > 500000 ? "เกินงบคงเหลือ 500,000.00 บาท ลดวงเงินหรือเลือกรหัสงบอื่น" : undefined | string | What's wrong and how to fix it, shown under the field. It marks the field invalid. |
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. |
| value | number | null | — | The amount, when the screen keeps it. null when empty. |
| defaultValue | number | 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