ค้นหาทั้งระบบ
GlobalSearch
Search across the whole system from the top bar. The button, or Ctrl K (⌘ K on a Mac), opens a dialog that lists results by group as the person types. The arrow keys move through them and Enter opens one. With only items, it searches them itself. With onQueryChange, the screen searches, such as through the API, and passes the results back as items. AppShell shows it when given its search prop.
ใช้ในแถบด้านบนของ AppShell ผ่าน prop search เพื่อค้นหาเมนู เอกสาร หรือข้อมูลอื่นจากทุกหน้า กดปุ่มหรือ Ctrl K (⌘ K บน Mac) เพื่อเปิด ส่งรายการทั้งหมดใน items ให้คอมโพเนนต์ค้นหาเอง หรือส่ง onQueryChange เมื่อต้องค้นหาผ่าน API ถ้าค้นหาในรายการเดียวบนหน้า ใช้ SearchInput หรือ search ของ DataTable
import { GlobalSearch } from "@bsru/ui";ตัวอย่าง
ค้นหาในรายการที่ส่งเข้าไป
ส่งทุกสิ่งที่ค้นหาได้ใน items แต่ละรายการมี group เป็นหัวข้อ title คือสิ่งที่พบ และ detail เป็นบรรทัดที่สอง keywords คือคำอื่นที่ค้นเจอแต่ไม่แสดง เช่นชื่อภาษาอังกฤษ ลองพิมพ์ PR หรือ vendor onNavigate รับ href ของผลที่เลือก ในแอปให้ส่ง router.push
const router = useRouter();
<GlobalSearch
items={[
{
group: "เมนู",
title: "คำขอซื้อขอจ้าง",
detail: "จัดซื้อจัดจ้าง",
href: "#purchase-requests",
icon: ClipboardList,
keywords: "purchase request PR",
},
{
group: "เมนู",
title: "อนุมัติเอกสาร",
detail: "งานของฉัน",
href: "#approvals",
icon: FileText,
keywords: "approve",
},
]}
label="ค้นหาเมนูและเอกสาร"
hint="ค้นหาเมนู เลขที่เอกสาร หรือชื่อผู้ขาย"
onNavigate={router.push}
/>| Prop | ค่าในตัวอย่างนี้ | ชนิดข้อมูล | คำอธิบาย |
|---|---|---|---|
| items* จำเป็น | รายการ 9 รายการ[
{
group: "เมนู",
title: "คำขอซื้อขอจ้าง",
detail: "จัดซื้อจัดจ้าง",
href: "#purchase-requests",
icon: ClipboardList,
keywords: "purchase request PR",
},
{
group: "เมนู",
title: "อนุมัติเอกสาร",
detail: "งานของฉัน",
href: "#approvals",
icon: FileText,
keywords: "approve",
},
{
group: "เมนู",
title: "งบประมาณคงเหลือ",
detail: "งบประมาณ",
href: "#budget",
icon: Wallet,
keywords: "budget",
},
{
group: "เมนู",
title: "ตั้งค่าระบบ",
detail: "ผู้ดูแลระบบ",
href: "#settings",
icon: Settings,
keywords: "settings",
},
{
group: "เอกสาร",
title: "PR-2570-0001",
detail: "ครุภัณฑ์คอมพิวเตอร์โน้ตบุ๊ก 5 เครื่อง",
href: "#pr-0001",
icon: FileText,
},
{
group: "เอกสาร",
title: "PR-2570-0002",
detail: "จ้างทำความสะอาดอาคาร 3",
href: "#pr-0002",
icon: FileText,
},
{
group: "เอกสาร",
title: "BG-2570-ข-002",
detail: "งบดำเนินงาน คณะวิทยาศาสตร์",
href: "#bg-002",
icon: Landmark,
},
{
group: "ผู้ขาย",
title: "บริษัท ทดสอบ จำกัด",
detail: "V-0001",
href: "#v-0001",
icon: Store,
keywords: "vendor",
},
{
group: "ผู้ขาย",
title: "ห้างหุ้นส่วน สมมติ",
detail: "V-0002",
href: "#v-0002",
icon: Store,
keywords: "vendor",
},
] | readonly SearchItem[] | Everything it can find, or with onQueryChange, the results for the current query. |
| label | "ค้นหาเมนูและเอกสาร" | string | The button's text and the box's placeholder, already in the screen language. Defaults to Search. |
| hint | "ค้นหาเมนู เลขที่เอกสาร หรือชื่อผู้ขาย" | string | Shown before anything is typed, such as what can be found. |
| shortcut | false | boolean | Opens it with Ctrl K, or ⌘ K on a Mac. Turn off when the screen uses that key for something else. |
| onNavigate | router.push | (href: string) => void | Opens a result. Pass the router's push, so the page changes without a full load. Defaults to a link. |
| onOpen | ไม่ได้ส่งในตัวอย่างนี้ | () => void | Called when the dialog opens, such as to load the items the first time. |
| onQueryChange | ไม่ได้ส่งในตัวอย่างนี้ | (query: string) => void | For a search the screen runs itself. Called 300 ms after typing stops. Items are then shown as given. |
ค้นหาผ่าน API
เมื่อข้อมูลมีมากเกินจะส่งทั้งหมด ให้ส่ง onQueryChange คอมโพเนนต์เรียกเมื่อหยุดพิมพ์ 300 มิลลิวินาที หน้าจอค้นหาผ่าน API ของ core แล้วส่งผลกลับใน items โดยไม่กรองซ้ำ status เป็น loading ระหว่างรอ
const [results, setResults] = useState<SearchItem[]>([]);
const [status, setStatus] = useState<"ready" | "loading">("ready");
async function search(query: string) {
if (!query) return setResults([]);
setStatus("loading");
setResults(await api.search(query));
setStatus("ready");
}
<GlobalSearch
items={results}
status={status}
onQueryChange={search}
label="ค้นหาเมนูและเอกสาร"
onNavigate={router.push}
/>| Prop | ค่าในตัวอย่างนี้ | ชนิดข้อมูล | คำอธิบาย |
|---|---|---|---|
| items* จำเป็น | รายการ 2 รายการ[
{
group: "เอกสาร",
title: "PR-2570-0001",
detail: "ครุภัณฑ์คอมพิวเตอร์โน้ตบุ๊ก 5 เครื่อง",
href: "#pr-0001",
icon: FileText,
},
{
group: "เอกสาร",
title: "PR-2570-0002",
detail: "จ้างทำความสะอาดอาคาร 3",
href: "#pr-0002",
icon: FileText,
},
] | readonly SearchItem[] | Everything it can find, or with onQueryChange, the results for the current query. |
| status | "loading" | "ready" | "loading" ค่าที่ใช้ได้: "ready""loading" | loading while the screen's own search runs. |
| label | "ค้นหาเมนูและเอกสาร" | string | The button's text and the box's placeholder, already in the screen language. Defaults to Search. |
| onQueryChange | search | (query: string) => void | For a search the screen runs itself. Called 300 ms after typing stops. Items are then shown as given. |
| onNavigate | router.push | (href: string) => void | Opens a result. Pass the router's push, so the page changes without a full load. Defaults to a link. |
| onOpen | ไม่ได้ส่งในตัวอย่างนี้ | () => void | Called when the dialog opens, such as to load the items the first time. |
ในแถบด้านบนของ AppShell
ส่ง props ชุดเดียวกันใน search ของ AppShell ปุ่มค้นหาจะอยู่ระหว่างเส้นทางกับปุ่มเลือกภาษา ทางลัด Ctrl K ใช้ได้จากทุกหน้า เว็บนี้ก็ใช้แบบนี้ ลองกดปุ่มค้นหาที่ด้านบน
ปุ่มค้นหาอยู่ในแถบด้านบนของหน้านี้ / The search button is in this page's top bar.
<AppShell
search={{ items: searchItems, label: t("search"), onNavigate: router.push }}
…
>props ทั้งหมด
ตัวอย่างด้านบนใช้ props เพียงบางตัว ตารางนี้แสดงทุกตัวที่คอมโพเนนต์รับ พร้อมค่าเริ่มต้นและค่าที่ใช้ได้
| ชื่อ | ชนิดข้อมูล | ค่าเริ่มต้น | คำอธิบาย |
|---|---|---|---|
| items* จำเป็น | readonly SearchItem[] | — | Everything it can find, or with onQueryChange, the results for the current query. |
| label | string | — | The button's text and the box's placeholder, already in the screen language. Defaults to Search. |
| hint | string | — | Shown before anything is typed, such as what can be found. |
| onNavigate | (href: string) => void | — | Opens a result. Pass the router's push, so the page changes without a full load. Defaults to a link. |
| onOpen | () => void | — | Called when the dialog opens, such as to load the items the first time. |
| onQueryChange | (query: string) => void | — | For a search the screen runs itself. Called 300 ms after typing stops. Items are then shown as given. |
| status | "ready" | "loading" | "ready" | loading while the screen's own search runs. |
| shortcut | boolean | true | Opens it with Ctrl K, or ⌘ K on a Mac. Turn off when the screen uses that key for something else. |
ชนิดข้อมูล
SearchItem
One thing global search can find, such as a page, a document or a setting.
| ชื่อ | ชนิดข้อมูล | ค่าเริ่มต้น | คำอธิบาย |
|---|---|---|---|
| href* จำเป็น | string | — | Where the result goes. Also its key, so each href is listed once. |
| title* จำเป็น | string | — | What it is, such as a page name or a document number. Matches score highest here. |
| group* จำเป็น | string | — | The heading it's listed under, such as Pages or Documents. The group with the best match comes first. |
| detail | string | — | A second line, such as where it is or what it's for. Also searched. |
| keywords | string | — | Other words that find it but aren't shown, such as the English name of a Thai page. |
| icon | LucideIcon | — | Shown before the title. |
ข้อควรปฏิบัติ
- ช่องค้นหาเป็น combobox ลูกศรขึ้นลงเลื่อนผลลัพธ์ Enter เปิด และ Esc ปิด จำนวนผลลัพธ์ประกาศให้โปรแกรมอ่านหน้าจอ
- แต่ละกลุ่มแสดงไม่เกิน 8 รายการ เรียงจากตรงที่สุด: ชื่อตรงทั้งหมด ขึ้นต้นด้วยคำค้น มีคำค้นอยู่ในชื่อ แล้วจึงเป็น keywords และ detail
- ส่งเฉพาะสิ่งที่ผู้ใช้มีสิทธิ์เห็น ผลลัพธ์จาก API ต้องผ่านการตรวจสิทธิ์ที่ฝั่งเซิร์ฟเวอร์
- label และ hint เป็นข้อความในภาษาของหน้าจอ
ซอร์สโค้ด: ui/src/global-search.tsx