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

ช่องตัวเลขแบบเพิ่มลด

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

เพิ่มลดทีละหลายหน่วย

step กำหนดว่าปุ่มเพิ่มหรือลดครั้งละเท่าไร hint บอกช่วงที่รับได้ defaultValue ให้ช่องเก็บค่าเอง

ครั้งละ 10 ที่นั่ง ตั้งแต่ 10 ถึง 200

<NumberStepper
  label="จำนวนที่นั่ง"
  hint="ครั้งละ 10 ที่นั่ง ตั้งแต่ 10 ถึง 200"
  min={10}
  max={200}
  step={10}
  defaultValue={40}
  onValueChange={handleValueChange}
/>

เกินจำนวนในคลัง

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

props ทั้งหมด

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

NumberStepper 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.
idstring—The input's id. One is made when it's left out.
min* จำเป็นnumber—The lowest value allowed.
max* จำเป็นnumber—The highest value allowed.
valuenumber—The number, when the screen keeps it.
defaultValuenumber—The starting number, when the input keeps it. min by default.
stepnumber1How 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