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

ค้นหาทั้งระบบ

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

ค้นหาผ่าน 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}
/>

ในแถบด้านบนของ 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 เพียงบางตัว ตารางนี้แสดงทุกตัวที่คอมโพเนนต์รับ พร้อมค่าเริ่มต้นและค่าที่ใช้ได้

GlobalSearch props
ชื่อชนิดข้อมูลค่าเริ่มต้นคำอธิบาย
items* จำเป็นreadonly SearchItem[]—Everything it can find, or with onQueryChange, the results for the current query.
labelstring—The button's text and the box's placeholder, already in the screen language. Defaults to Search.
hintstring—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.
shortcutbooleantrueOpens 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.

SearchItem fields
ชื่อชนิดข้อมูลค่าเริ่มต้นคำอธิบาย
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.
detailstring—A second line, such as where it is or what it's for. Also searched.
keywordsstring—Other words that find it but aren't shown, such as the English name of a Thai page.
iconLucideIcon—Shown before the title.

ข้อควรปฏิบัติ

  • ช่องค้นหาเป็น combobox ลูกศรขึ้นลงเลื่อนผลลัพธ์ Enter เปิด และ Esc ปิด จำนวนผลลัพธ์ประกาศให้โปรแกรมอ่านหน้าจอ
  • แต่ละกลุ่มแสดงไม่เกิน 8 รายการ เรียงจากตรงที่สุด: ชื่อตรงทั้งหมด ขึ้นต้นด้วยคำค้น มีคำค้นอยู่ในชื่อ แล้วจึงเป็น keywords และ detail
  • ส่งเฉพาะสิ่งที่ผู้ใช้มีสิทธิ์เห็น ผลลัพธ์จาก API ต้องผ่านการตรวจสิทธิ์ที่ฝั่งเซิร์ฟเวอร์
  • label และ hint เป็นข้อความในภาษาของหน้าจอ

ซอร์สโค้ด: ui/src/global-search.tsx