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

ช่องข้อความนับตัวอักษร

CountedTextarea

A textarea with a count of characters used against the limit, below it on the right.

ใช้กับข้อความยาวหลายบรรทัดที่มีจำนวนตัวอักษรจำกัด เช่น เหตุผลในการจัดซื้อหรือความเห็นของผู้อนุมัติ ตัวนับใต้ช่องบอกว่าใช้ไปเท่าไรแล้ว สำหรับข้อความสั้นบรรทัดเดียว ใช้ TextInput

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

ตัวอย่าง

หน้าจอเก็บข้อความ

ส่ง maxLength value และ onValueChange onValueChange ได้ข้อความทุกครั้งที่เปลี่ยน hint แสดงใต้ช่องคู่กับตัวนับ

27 / 200

บอกว่าใช้ทำอะไร และทำไมต้องซื้อตอนนี้

const [reason, setReason] = useState("ใช้ทดแทนเครื่องเดิมที่ชำรุด");

<CountedTextarea
  label="เหตุผลความจำเป็น"
  hint="บอกว่าใช้ทำอะไร และทำไมต้องซื้อตอนนี้"
  required
  maxLength={200}
  value={reason}
  onValueChange={setReason}
/>

ช่องว่าง สูงขึ้น

rows กำหนดความสูงเป็นจำนวนบรรทัด ไม่ส่ง value ช่องจะเก็บข้อความเอง

0 / 500

<CountedTextarea
  label="ความเห็นของผู้อนุมัติ"
  rows={5}
  maxLength={500}
  placeholder="ไม่บังคับ"
  onValueChange={handleValueChange}
/>

ข้อผิดพลาด

error บอกว่าผิดอะไรและแก้อย่างไร แสดงใต้ช่อง

0 / 300

ใส่เหตุผล เพื่อให้ผู้ขอแก้ไขคำขอได้

<CountedTextarea
  label="เหตุผลที่ไม่อนุมัติ"
  required
  maxLength={300}
  error="ใส่เหตุผล เพื่อให้ผู้ขอแก้ไขคำขอได้"
  onValueChange={handleValueChange}
/>

props ทั้งหมด

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

CountedTextarea 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 textarea's id. One is made when it's left out.
rowsnumber3The visible height in lines.
maxLength* จำเป็นnumber—The most characters allowed. Typing stops there.
placeholderstring——
valuestring—The text, when the screen keeps it.
defaultValuestring""The starting text, when the textarea keeps it.
onValueChange(next: string) => void—Hears the text on each change.

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

  • พิมพ์ได้ไม่เกิน maxLength ตัวนับนับตามตัวอักษรของ JavaScript สระและวรรณยุกต์ไทยจึงนับแยกด้วย
  • ตัวนับแจ้งโปรแกรมอ่านหน้าจอเมื่อเปลี่ยน

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