ช่องข้อความนับตัวอักษร
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}
/>| 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. |
| required | true | boolean | Marks the label with a red star, and the control as required. |
| maxLength* จำเป็น | 200 | number | The most characters allowed. Typing stops there. |
| value | reason | string | The text, when the screen keeps it. |
| onValueChange | setReason | (next: string) => void | Hears the text on each change. |
ช่องว่าง สูงขึ้น
rows กำหนดความสูงเป็นจำนวนบรรทัด ไม่ส่ง value ช่องจะเก็บข้อความเอง
0 / 500
<CountedTextarea
label="ความเห็นของผู้อนุมัติ"
rows={5}
maxLength={500}
placeholder="ไม่บังคับ"
onValueChange={handleValueChange}
/>| Prop | ค่าในตัวอย่างนี้ | ชนิดข้อมูล | คำอธิบาย |
|---|---|---|---|
| label | "ความเห็นของผู้อนุมัติ" | string | The visible label, already in the screen language. Every field needs one, unless the screen labels it another way. |
| rows | 5 | number | The visible height in lines. |
| maxLength* จำเป็น | 500 | number | The most characters allowed. Typing stops there. |
| placeholder | "ไม่บังคับ" | string | — |
| onValueChange | handleValueChange | (next: string) => void | Hears the text on each change. |
ข้อผิดพลาด
error บอกว่าผิดอะไรและแก้อย่างไร แสดงใต้ช่อง
0 / 300
ใส่เหตุผล เพื่อให้ผู้ขอแก้ไขคำขอได้
<CountedTextarea
label="เหตุผลที่ไม่อนุมัติ"
required
maxLength={300}
error="ใส่เหตุผล เพื่อให้ผู้ขอแก้ไขคำขอได้"
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. |
| maxLength* จำเป็น | 300 | number | The most characters allowed. Typing stops there. |
| error | "ใส่เหตุผล เพื่อให้ผู้ขอแก้ไขคำขอได้" | string | What's wrong and how to fix it, shown under the field. It marks the field invalid. |
| onValueChange | handleValueChange | (next: string) => void | Hears the text on each change. |
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 textarea's id. One is made when it's left out. |
| rows | number | 3 | The visible height in lines. |
| maxLength* จำเป็น | number | — | The most characters allowed. Typing stops there. |
| placeholder | string | — | — |
| value | string | — | The text, when the screen keeps it. |
| defaultValue | string | "" | 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