ช่องค้นหา
SearchInput
A search field with a leading icon and a clear button that shows once something is typed. Pass suggestions to show a list under the field as people type, such as matching people or records from the core API. The screen chooses the suggestions and what a pick does. Arrows move through them, Enter picks, and Esc closes the list.
ใช้ค้นหาหรือกรองรายการด้วยข้อความอิสระ เช่น ค้นในตาราง ปุ่มล้างจะแสดงเมื่อมีข้อความ ถ้าต้องเลือกรายการเดียวจากรายการยาว เช่น ผู้ค้า ใช้ 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. |
| onSuggestionPick | ไม่ได้ส่งในตัวอย่างนี้ | (value: string) => void | Hears the value of the suggestion people pick, such as to open that record or fill in the text. |
มีรายการแนะนำ
suggestions แสดงรายการใต้ช่องขณะพิมพ์ หน้าจอเป็นผู้เลือกรายการที่ตรงกับคำค้น เช่น จาก API หลัก และกำหนดว่าเลือกแล้วทำอะไร เช่น เปิดระเบียนนั้น ตัวอย่างนี้ใส่ชื่อลงในช่อง
const [query, setQuery] = useState("");
<SearchInput
label="ค้นหาบุคลากร"
placeholder="พิมพ์ชื่อหรือหน่วยงาน"
value={query}
onValueChange={setQuery}
suggestions={matches(query)}
onSuggestionPick={(v) => setQuery(nameOf(v))}
menuWidth="sm"
/>| 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. |
| suggestions | matches(query) | readonly SelectOption[] | Suggestions to show under the field while it has focus, already matched to the text by the screen. Leave it out for a plain search field. An empty array shows no list. |
| onSuggestionPick | (v) => setQuery(nameOf(v)) | (value: string) => void | Hears the value of the suggestion people pick, such as to open that record or fill in the text. |
| menuWidth | "sm" | MenuWidth ค่าที่ใช้ได้: "trigger""sm""md""lg""xl" | How wide the suggestions are. trigger matches the field. The others may be wider than the field, but never wider than the screen. |
รายการแนะนำเป็นการ์ด
layout cards แสดงรายการแนะนำเป็นการ์ดมีรูปด้านบน menuWidth lg ให้กว้างกว่าช่อง ลูกศรขึ้นลงเลื่อนทีละแถว
const [query, setQuery] = useState("");
<SearchInput
label="ค้นหาห้อง"
placeholder="พิมพ์เลขห้องหรือรูปแบบ"
value={query}
onValueChange={setQuery}
suggestions={matches(query)}
onSuggestionPick={(v) => setQuery(nameOf(v))}
layout="cards"
menuWidth="lg"
/>| 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. |
| suggestions | matches(query) | readonly SelectOption[] | Suggestions to show under the field while it has focus, already matched to the text by the screen. Leave it out for a plain search field. An empty array shows no list. |
| onSuggestionPick | (v) => setQuery(nameOf(v)) | (value: string) => void | Hears the value of the suggestion people pick, such as to open that record or fill in the text. |
| layout | "cards" | OptionLayout ค่าที่ใช้ได้: "list""cards" | How the suggestions show: rows, or cards in a grid with each one's image on top. |
| menuWidth | "lg" | MenuWidth ค่าที่ใช้ได้: "trigger""sm""md""lg""xl" | How wide the suggestions are. trigger matches the field. The others may be wider than the field, but never wider than the screen. |
มีคำค้นอยู่แล้ว
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. |
| onSuggestionPick | ไม่ได้ส่งในตัวอย่างนี้ | (value: string) => void | Hears the value of the suggestion people pick, such as to open that record or fill in the text. |
เปลี่ยนไอคอน
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. |
| onSuggestionPick | ไม่ได้ส่งในตัวอย่างนี้ | (value: string) => void | Hears the value of the suggestion people pick, such as to open that record or fill in the text. |
ในแถบเครื่องมือ
เมื่อไม่มีที่สำหรับป้ายที่มองเห็นได้ เช่น ในแถบเหนือตาราง ส่ง 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. |
| onSuggestionPick | ไม่ได้ส่งในตัวอย่างนี้ | (value: string) => void | Hears the value of the suggestion people pick, such as to open that record or fill in the text. |
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. |
| suggestions | readonly SelectOption[] | — | Suggestions to show under the field while it has focus, already matched to the text by the screen. Leave it out for a plain search field. An empty array shows no list. |
| onSuggestionPick | (value: string) => void | — | Hears the value of the suggestion people pick, such as to open that record or fill in the text. |
| layout | OptionLayout ค่าที่ใช้ได้: "list""cards" | "list" | How the suggestions show: rows, or cards in a grid with each one's image on top. |
| menuWidth | MenuWidth ค่าที่ใช้ได้: "trigger""sm""md""lg""xl" | "trigger" | How wide the suggestions are. trigger matches the field. The others may be wider than the field, but never wider than the screen. |
รับ attribute ของ element HTML ต้นแบบได้ทั้งหมดด้วย เช่น id, name, required และ disabled
ชนิดข้อมูล
SelectOption
| ชื่อ | ชนิดข้อมูล | ค่าเริ่มต้น | คำอธิบาย |
|---|---|---|---|
| value* จำเป็น | V | — | — |
| label* จำเป็น | string | — | The text shown, already in the screen language. Typing jumps to an option by the start of this text. |
| note | string | — | A second line, such as the balance left on a budget code. |
| group | string | — | Options with the same group in a row sit under one heading. |
| icon | LucideIcon | — | — |
| image | string | — | A picture's address, such as a product photo or a room plan. It shows in place of the icon: a small square in a list, and across the top of a card. The label names the option, so the picture needs no text of its own. |
OptionLayout
How a dropdown shows its options: rows, or cards in a grid, each with its picture on top.
type OptionLayout = "list" | "cards";MenuWidth
How wide a dropdown's list is. trigger matches the field. The others are fixed widths that may be wider than the field: sm 20rem, md 28rem, lg 42rem and xl 56rem, never wider than the screen. A list wider than the room to the right of the field lines up with the field's right edge instead.
type MenuWidth = "trigger" | "sm" | "md" | "lg" | "xl";ข้อควรปฏิบัติ
- ส่ง label หรือ aria-label ทุกครั้ง placeholder ไม่ใช่ชื่อของช่อง
- ถ้าค้นผ่านเซิร์ฟเวอร์ ให้หน่วงเวลาเรียกหลังผู้ใช้หยุดพิมพ์
- ปุ่มล้างมีชื่อสำหรับโปรแกรมอ่านหน้าจอ และคืนโฟกัสให้ช่องหลังกด
- เมื่อมี suggestions ช่องจะทำงานแบบ combobox ลูกศรเลื่อน Enter เลือก Esc ครั้งแรกปิดรายการ ครั้งที่สองล้างข้อความ ถ้าต้องการให้เลือกได้รายการเดียวและเก็บรายการที่เลือกไว้ ใช้ Combobox
ซอร์สโค้ด: ui/src/inputs.tsx