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

โครงแอป

AppShell

The application frame: the left menu, which collapses to icons on wide screens and slides in on phones, and the top bar with the breadcrumb, global search, language switch, notifications and account menu.

ใช้ครั้งเดียวต่อแอป ในเลย์เอาต์ราก เพื่อวางเมนูซ้าย แถบบน เส้นทาง ปุ่มภาษา การแจ้งเตือน และเมนูบัญชี หน้าจอส่งเมนู ผู้ใช้ และการแจ้งเตือนเป็นข้อมูล ไม่ต้องเขียนโครงหน้าเอง หน้าที่ไม่ต้องเข้าสู่ระบบ เช่น หน้าเข้าสู่ระบบ ไม่ใช้ AppShell

import { AppShell } from "@bsru/ui";

ตัวอย่าง

โครงแอปแบบเต็ม

กรอบของเว็บนี้ก็เป็น AppShell ตัวอย่างนี้อยู่ในกล่องสูงคงที่ ส่วนในแอปจริง AppShell เต็มหน้าจอ current คือ href ของลิงก์หน้าปัจจุบัน กลุ่มของลิงก์นั้นจะเปิดเอง โค้ดแปลข้อความด้วย t() ของ next-intl ข้อมูลจึงเป็นข้อความที่แปลแล้ว

ระบบจำลอง ใช้ข้อมูลทดสอบเท่านั้น

ใบขอเบิก

พื้นที่นี้เป็นของหน้าจอ ลองย่อเมนู เปิดกระดิ่ง หรือเปิดเมนูบัญชี ปุ่มภาษาในแถบบนเปลี่ยนภาษาของทั้งเว็บนี้

const t = useTranslations();

const brand: ShellBrand = {
  href: "/",
  logo: "/bsru-logo.png",
  alt: t("brand.seal"),
  name: "BSRU ERP",
  sub: t("brand.finance"),
};

const menu: ShellGroup[] = [
  {
    icon: Wallet,
    label: t("nav.budget"),
    links: [
      { href: "#budget-plan", label: t("nav.budgetPlan") },
      { href: "#budget-transfer", label: t("nav.budgetTransfers") },
    ],
  },
  {
    icon: ShoppingCart,
    label: t("nav.procurement"),
    links: [
      { href: "#purchase-requests", label: t("nav.purchaseRequests") },
      { href: "#purchase-orders", label: t("nav.purchaseOrders") },
    ],
  },
  {
    icon: FileText,
    label: t("nav.disbursement"),
    links: [
      { href: "#vouchers", label: t("nav.vouchers") },
      { href: "#receipts", label: t("nav.receipts") },
    ],
  },
  { icon: Banknote, label: t("nav.payroll"), links: [{ href: "#payroll", label: t("nav.payRuns") }] },
  { icon: CalendarDays, label: t("nav.leave"), links: [{ href: "#leave", label: t("nav.myLeave") }] },
];

const account: ShellAccount = {
  initial: user.initial,
  name: user.name,
  role: user.role,
  items: [
    { icon: UserRound, label: t("account.profile") },
    { icon: Settings, label: t("account.settings") },
    { icon: LogOut, label: t("account.signOut"), danger: true },
  ],
};

const [notifications, setNotifications] = useState<ShellNotification[]>([
  { id: "1", unread: true, icon: FileCheck, title: t("notices.approve", { no: "PR-2570-0001" }), when: t("time.minutesAgo", { n: 10 }) },
  { id: "2", unread: true, icon: CalendarCheck, title: t("notices.leaveApproved"), when: t("time.hoursAgo", { n: 1 }) },
  { id: "3", unread: false, icon: TriangleAlert, title: t("notices.belowTarget", { unit: "หน่วยงาน ก" }), when: t("time.yesterday") },
  { id: "4", unread: false, icon: ReceiptText, title: t("notices.receiptIssued", { no: "RC-2570-0001" }), when: t("time.daysAgo", { n: 2 }) },
]);

<AppShell
  brand={brand}
  groups={menu}
  current="#vouchers"
  crumbs={{ group: t("nav.disbursement"), page: t("nav.vouchers") }}
  homeHref="/"
  demo={t("shell.demo")}
  notifications={notifications}
  onRead={(id) => setNotifications((all) => all.map((n) => (n.id === id ? { ...n, unread: false } : n)))}
  onReadAll={() => setNotifications((all) => all.map((n) => ({ ...n, unread: false })))}
  account={account}
>
  <main id="main">{/* The screen's own components */}</main>
</AppShell>

props ทั้งหมด

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

AppShell props
ชื่อชนิดข้อมูลค่าเริ่มต้นคำอธิบาย
brand* จำเป็นShellBrand—The logo and name at the top of the menu.
groups* จำเป็นreadonly ShellGroup[]—The menu: groups of links.
current* จำเป็นstring | null—The href of the link for the current page, or null when none matches.
crumbs* จำเป็น{ group: string; page: string }—The breadcrumb in the top bar: the menu group, then the page.
homeHref* จำเป็นstring—Where the home icon in the breadcrumb links to.
demostring—The gold strip above the top bar, for demo and test systems.
notifications* จำเป็นreadonly ShellNotification[]—The items under the bell, newest first. The screen keeps them in its state.
onRead* จำเป็น(id: string) => void—Called with an item's id when it is pressed. Mark it read and open it.
onReadAll* จำเป็น() => void—Called by Mark all as read.
account* จำเป็นShellAccount—The signed-in person and their menu.
defaultOpenreadonly number[][0]Indexes of the groups open on first render.
children* จำเป็นReactNode—The page, usually a main element.

ชนิดข้อมูล

ShellBrand

The app's name and logo at the top of the left menu.

ShellBrand fields
ชื่อชนิดข้อมูลค่าเริ่มต้นคำอธิบาย
href* จำเป็นstring—Where the logo links to, usually the home page.
alt* จำเป็นstring—The logo's alternative text.
name* จำเป็นstring—Shown on two lines beside the logo.
sub* จำเป็นstring—The second line, such as the system's name.

ShellGroup

A group in the left menu, which opens to show its links.

ShellGroup fields
ชื่อชนิดข้อมูลค่าเริ่มต้นคำอธิบาย
icon* จำเป็นLucideIcon—Shown alone when the menu is collapsed.
label* จำเป็นstring—The group's name.

One link in a menu group.

ShellLink fields
ชื่อชนิดข้อมูลค่าเริ่มต้นคำอธิบาย

ShellNotification

One item in the notification panel under the bell.

ShellNotification fields
ชื่อชนิดข้อมูลค่าเริ่มต้นคำอธิบาย
id* จำเป็นstring—Passed to onRead when the item is pressed.
icon* จำเป็นLucideIcon—Shows what the item is about, such as a document or a warning.
title* จำเป็นstring—What happened, in one line.
when* จำเป็นstring—When it happened, as text, such as 10 minutes ago.
unread* จำเป็นboolean—Bold, with a dot, and counted on the bell.

ShellAccount

The signed-in person, shown in the account menu at the top right.

ShellAccount fields
ชื่อชนิดข้อมูลค่าเริ่มต้นคำอธิบาย
initial* จำเป็นstring—One letter for the round avatar.
name* จำเป็นstring—Their full name.
role* จำเป็นstring—Their position and unit.
items* จำเป็นreadonly ShellMenuItem[]—The menu's actions, such as My profile and Sign out.

ShellMenuItem

One action in the account menu.

ShellMenuItem fields
ชื่อชนิดข้อมูลค่าเริ่มต้นคำอธิบาย
icon* จำเป็นLucideIcon—Shown before the label.
label* จำเป็นstring—What the action does.
dangerboolean—Red, for an action such as Sign out.
onSelect() => void—Called after the menu closes.

GlobalSearchProps

What GlobalSearch takes. AppShell passes its search prop through.

GlobalSearchProps fields
ชื่อชนิดข้อมูลค่าเริ่มต้นคำอธิบาย
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"—loading while the screen's own search runs.
shortcutboolean—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.

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.

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

  • ส่งข้อความทุกตัวในภาษาของหน้าจออยู่แล้ว แอปแปลเองด้วยไฟล์ข้อความของแอป (t() ของ next-intl) ส่วนคำของ AppShell เอง เช่น ปุ่มเปิดเมนู ปุ่มภาษา และป้ายสำหรับโปรแกรมอ่านหน้าจอ เปลี่ยนตาม LangProvider
  • ใส่ลิงก์ข้ามไปยังเนื้อหาไว้ก่อน AppShell และให้ children เป็น main ที่มี id ตรงกัน
  • แป้นพิมพ์: Esc ปิดเมนูที่เลื่อนออกมาบนโทรศัพท์ และปิดกระดิ่งกับเมนูบัญชี เมื่อเมนูย่อ ไอคอนแต่ละกลุ่มแสดงชื่อเมื่อชี้หรือโฟกัส
  • ส่ง crumbs ให้ตรงกับกลุ่มและลิงก์ของ current เส้นทางคือสิ่งที่โปรแกรมอ่านหน้าจออ่านเป็นชื่อหน้า
  • ส่ง demo เฉพาะระบบทดสอบและระบบจำลอง แถบสีทองบอกผู้ใช้ว่าไม่ใช่ข้อมูลจริง
  • การย่อเมนูจำไว้ในเบราว์เซอร์ ส่วนการแจ้งเตือนเก็บใน state ของแอป และ onRead กับ onReadAll ต้องอัปเดต state นั้น

ซอร์สโค้ด: ui/src/shell.tsx