ช่องตัวเลขแบบเพิ่มลด
NumberStepper
A number field with minus and plus buttons. The buttons stay within min and max, and a typed value is kept within them when people leave the field.
ใช้กับจำนวนนับที่มีช่วงชัดเจนและมักปรับทีละน้อย เช่น จำนวนชิ้นหรือจำนวนผู้เข้าร่วม ปุ่มลบและบวกไม่ให้เกิน min และ max และค่าที่พิมพ์จะถูกปรับให้อยู่ในช่วงเมื่อออกจากช่อง สำหรับจำนวนเงิน ใช้ MoneyInput
import { NumberStepper } from "@bsru/ui";ตัวอย่าง
หน้าจอเก็บจำนวน
ส่ง min max และ value onValueChange ได้ตัวเลขใหม่หลังกดปุ่ม ขณะพิมพ์ค่าที่อยู่ในช่วง และเมื่อออกจากช่อง ลองพิมพ์ 500 แล้วออกจากช่อง ค่าจะเป็น 99
const [quantity, setQuantity] = useState(5);
<NumberStepper
label="จำนวน (เครื่อง)"
min={1}
max={99}
value={quantity}
onValueChange={setQuantity}
/>| Prop | ค่าในตัวอย่างนี้ | ชนิดข้อมูล | คำอธิบาย |
|---|---|---|---|
| label | "จำนวน (เครื่อง)" | string | The visible label, already in the screen language. Every field needs one, unless the screen labels it another way. |
| min* จำเป็น | 1 | number | The lowest value allowed. |
| max* จำเป็น | 99 | number | The highest value allowed. |
| value | quantity | number | The number, when the screen keeps it. |
| onValueChange | setQuantity | (next: number) => void | Hears the new number after a button press, typing, or leaving the field. |
เพิ่มลดทีละหลายหน่วย
step กำหนดว่าปุ่มเพิ่มหรือลดครั้งละเท่าไร hint บอกช่วงที่รับได้ defaultValue ให้ช่องเก็บค่าเอง
ครั้งละ 10 ที่นั่ง ตั้งแต่ 10 ถึง 200
<NumberStepper
label="จำนวนที่นั่ง"
hint="ครั้งละ 10 ที่นั่ง ตั้งแต่ 10 ถึง 200"
min={10}
max={200}
step={10}
defaultValue={40}
onValueChange={handleValueChange}
/>| Prop | ค่าในตัวอย่างนี้ | ชนิดข้อมูล | คำอธิบาย |
|---|---|---|---|
| label | "จำนวนที่นั่ง" | string | The visible label, already in the screen language. Every field needs one, unless the screen labels it another way. |
| hint | "ครั้งละ 10 ที่นั่ง ตั้งแต่ 10 ถึง 200" | string | Help under the field, such as the format or a limit. |
| min* จำเป็น | 10 | number | The lowest value allowed. |
| max* จำเป็น | 200 | number | The highest value allowed. |
| step | 10 | number | How much each button adds or takes away. |
| defaultValue | 40 | number | The starting number, when the input keeps it. min by default. |
| onValueChange | handleValueChange | (next: number) => void | Hears the new number after a button press, typing, or leaving the field. |
เกินจำนวนในคลัง
min และ max เป็นขอบเขตของช่อง เงื่อนไขอื่น เช่น จำนวนในคลัง หน้าจอตรวจเองแล้วส่ง error ลองลดเหลือ 12 ข้อความจะหายไป
มีในคลัง 12 ชิ้น ลดจำนวน หรือแยกส่วนที่เหลือเป็นคำขอซื้อ
const [quantity, setQuantity] = useState(15);
<NumberStepper
label="จำนวนที่เบิก (ชิ้น)"
required
min={1}
max={99}
value={quantity}
onValueChange={setQuantity}
error={quantity > 12 ? "มีในคลัง 12 ชิ้น ลดจำนวน หรือแยกส่วนที่เหลือเป็นคำขอซื้อ" : 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. |
| min* จำเป็น | 1 | number | The lowest value allowed. |
| max* จำเป็น | 99 | number | The highest value allowed. |
| value | quantity | number | The number, when the screen keeps it. |
| onValueChange | setQuantity | (next: number) => void | Hears the new number after a button press, typing, or leaving the field. |
| error | quantity > 12 ? "มีในคลัง 12 ชิ้น ลดจำนวน หรือแยกส่วนที่เหลือเป็นคำขอซื้อ" : 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. |
| id | string | — | The input's id. One is made when it's left out. |
| min* จำเป็น | number | — | The lowest value allowed. |
| max* จำเป็น | number | — | The highest value allowed. |
| value | number | — | The number, when the screen keeps it. |
| defaultValue | number | — | The starting number, when the input keeps it. min by default. |
| step | number | 1 | How much each button adds or takes away. |
| onValueChange | (next: number) => void | — | Hears the new number after a button press, typing, or leaving the field. |
ข้อควรปฏิบัติ
- บอกหน่วยในป้ายกำกับ และบอกช่วงที่รับได้ใน hint เมื่อไม่ชัดเจน
- ค่าที่พิมพ์นอกช่วงจะถูกปรับเป็น min หรือ max เมื่อออกจากช่อง ช่องว่างจะกลับเป็นค่าเดิม onValueChange จึงได้ค่าในช่วงเสมอ
- แป้นพิมพ์: ลูกศรขึ้นลงในช่องเพิ่มหรือลดตาม step ปุ่มลบและบวกมีชื่อสำหรับโปรแกรมอ่านหน้าจอ
ซอร์สโค้ด: ui/src/amount-inputs.tsx