ช่องค้นหา
SearchInput
A search field with a leading icon and a clear button that shows once something is typed.
ใช้ค้นหาหรือกรองรายการด้วยข้อความอิสระ เช่น ค้นในตาราง ปุ่มล้างจะแสดงเมื่อมีข้อความ ถ้าต้องเลือกรายการเดียวจากรายการยาว เช่น ผู้ค้า ใช้ Combobox
import { SearchInput } from "@bsru/ui";ตัวอย่าง
หน้าจอเก็บคำค้น
ส่ง value และ onValueChange เมื่อหน้าจอใช้คำค้นกรองรายการ หรือต้องล้างจากที่อื่น เช่น ปุ่มล้างตัวกรองทั้งหมด onValueChange ได้ "" เมื่อกดล้าง
const [query, setQuery] = useState("");
<SearchInput
label="ค้นหาคำขอซื้อ"
placeholder="พิมพ์เลขที่หรือชื่อรายการ"
value={query}
onValueChange={setQuery}
/>| Prop | ค่าในตัวอย่างนี้ | ชนิดข้อมูล | คำอธิบาย |
|---|---|---|---|
| label | "ค้นหาคำขอซื้อ" | string | The visible label, already in the screen language. Every field needs one, unless the screen labels it another way. |
| placeholder | "พิมพ์เลขที่หรือชื่อรายการ" | string | — |
| value | query | string | The search text, when the screen keeps it, such as to clear it from a Clear all button. |
| onValueChange | setQuery | (next: string) => void | Hears the text on each change, and "" when it is cleared. |
มีคำค้นอยู่แล้ว
defaultValue ใส่คำค้นเริ่มต้นแล้วให้ช่องเก็บข้อความเอง ปุ่มล้างแสดงทันที
<SearchInput
label="ค้นหา"
defaultValue="ครุภัณฑ์"
onValueChange={handleValueChange}
/>| Prop | ค่าในตัวอย่างนี้ | ชนิดข้อมูล | คำอธิบาย |
|---|---|---|---|
| label | "ค้นหา" | string | The visible label, already in the screen language. Every field needs one, unless the screen labels it another way. |
| defaultValue | "ครุภัณฑ์" | string | The starting search text, when the input keeps it. |
| onValueChange | handleValueChange | (next: string) => void | Hears the text on each change, and "" when it is cleared. |
เปลี่ยนไอคอน
icon เปลี่ยนไอคอนนำหน้า เช่น เมื่อช่องใช้กรองมากกว่าค้นหา
<SearchInput
label="กรองหน่วยงาน"
icon={Funnel}
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. |
| icon | Funnel | LucideIcon | The leading icon. |
| placeholder | "พิมพ์ชื่อหน่วยงาน" | string | — |
| onValueChange | handleValueChange | (next: string) => void | Hears the text on each change, and "" when it is cleared. |
ในแถบเครื่องมือ
เมื่อไม่มีที่สำหรับป้ายที่มองเห็นได้ เช่น ในแถบเหนือตาราง ส่ง aria-label แทน label
<SearchInput
aria-label="ค้นหาคำขอซื้อ"
placeholder="ค้นหา"
onValueChange={handleValueChange}
/>| Prop | ค่าในตัวอย่างนี้ | ชนิดข้อมูล | คำอธิบาย |
|---|---|---|---|
| aria-label | "ค้นหาคำขอซื้อ" | string | — |
| placeholder | "ค้นหา" | string | — |
| onValueChange | handleValueChange | (next: string) => void | Hears the text on each change, and "" when it is cleared. |
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. |
| icon | LucideIcon | Search | The leading icon. |
| value | string | — | The search text, when the screen keeps it, such as to clear it from a Clear all button. |
| defaultValue | string | "" | The starting search text, when the input keeps it. |
| onValueChange | (next: string) => void | — | Hears the text on each change, and "" when it is cleared. |
รับ attribute ของ element HTML ต้นแบบได้ทั้งหมดด้วย เช่น id, name, required และ disabled
ข้อควรปฏิบัติ
- ส่ง label หรือ aria-label ทุกครั้ง placeholder ไม่ใช่ชื่อของช่อง
- ถ้าค้นผ่านเซิร์ฟเวอร์ ให้หน่วงเวลาเรียกหลังผู้ใช้หยุดพิมพ์
- ปุ่มล้างมีชื่อสำหรับโปรแกรมอ่านหน้าจอ และคืนโฟกัสให้ช่องหลังกด
ซอร์สโค้ด: ui/src/inputs.tsx