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

ช่องค้นหา

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

มีคำค้นอยู่แล้ว

defaultValue ใส่คำค้นเริ่มต้นแล้วให้ช่องเก็บข้อความเอง ปุ่มล้างแสดงทันที

<SearchInput
  label="ค้นหา"
  defaultValue="ครุภัณฑ์"
  onValueChange={handleValueChange}
/>

เปลี่ยนไอคอน

icon เปลี่ยนไอคอนนำหน้า เช่น เมื่อช่องใช้กรองมากกว่าค้นหา

<SearchInput
  label="กรองหน่วยงาน"
  icon={Funnel}
  placeholder="พิมพ์ชื่อหน่วยงาน"
  onValueChange={handleValueChange}
/>

ในแถบเครื่องมือ

เมื่อไม่มีที่สำหรับป้ายที่มองเห็นได้ เช่น ในแถบเหนือตาราง ส่ง aria-label แทน label

<SearchInput
  aria-label="ค้นหาคำขอซื้อ"
  placeholder="ค้นหา"
  onValueChange={handleValueChange}
/>

props ทั้งหมด

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

SearchInput 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.
iconLucideIconSearchThe leading icon.
valuestring—The search text, when the screen keeps it, such as to clear it from a Clear all button.
defaultValuestring""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