โครงแอป
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>| Prop | ค่าในตัวอย่างนี้ | ชนิดข้อมูล | คำอธิบาย |
|---|---|---|---|
| brand* จำเป็น | ออบเจกต์ 5 ฟิลด์{
href: "#",
logo: "/bsru-logo.png",
alt: "ตรามหาวิทยาลัยราชภัฏบ้านสมเด็จเจ้าพระยา",
name: "BSRU ERP",
sub: "ระบบการเงิน",
} | ShellBrand | The logo and name at the top of the menu. |
| groups* จำเป็น | รายการ 5 รายการ[
{
icon: Wallet,
label: "งบประมาณ",
links: [
{ href: "#budget-plan", label: "แผนงบประมาณ" },
{ href: "#budget-transfer", label: "โอนงบประมาณ" },
],
},
{
icon: ShoppingCart,
label: "จัดซื้อจัดจ้าง",
links: [
{ href: "#purchase-requests", label: "ใบขอซื้อ" },
{ href: "#purchase-orders", label: "ใบสั่งซื้อ" },
],
},
{
icon: FileText,
label: "เบิกจ่าย",
links: [
{ href: "#vouchers", label: "ใบขอเบิก" },
{ href: "#receipts", label: "ใบเสร็จ" },
],
},
{
icon: Banknote,
label: "เงินเดือน",
links: [{ href: "#payroll", label: "รอบจ่ายเงินเดือน" }],
},
{
icon: CalendarDays,
label: "การลา",
links: [{ href: "#leave", label: "ใบลาของฉัน" }],
},
] | readonly ShellGroup[] | The menu: groups of links. |
| current* จำเป็น | "#vouchers" | string | null | The href of the link for the current page, or null when none matches. |
| crumbs* จำเป็น | { group: "เบิกจ่าย", page: "ใบขอเบิก" } | { 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. |
| demo | "ระบบจำลอง ใช้ข้อมูลทดสอบเท่านั้น" | string | The gold strip above the top bar, for demo and test systems. |
| notifications* จำเป็น | รายการ 4 รายการ[
{
id: "1",
unread: true,
icon: FileCheck,
title: "PR-2570-0001 รอคุณอนุมัติ",
when: "10 นาทีที่แล้ว",
},
{
id: "2",
unread: true,
icon: CalendarCheck,
title: "ใบลาของคุณได้รับอนุมัติแล้ว",
when: "1 ชั่วโมงที่แล้ว",
},
{
id: "3",
unread: false,
icon: TriangleAlert,
title: "หน่วยงาน ก เบิกจ่ายต่ำกว่าเป้าหมาย",
when: "เมื่อวาน",
},
{
id: "4",
unread: false,
icon: ReceiptText,
title: "ใบเสร็จ RC-2570-0001 ออกแล้ว",
when: "2 วันที่แล้ว",
},
] | readonly ShellNotification[] | The items under the bell, newest first. The screen keeps them in its state. |
| account* จำเป็น | ออบเจกต์ 4 ฟิลด์{
initial: "ท",
name: "นางสาวทดสอบ ใจดี",
role: "เจ้าหน้าที่การเงิน หน่วยงาน ก",
items: [
{ icon: UserRound, label: "ข้อมูลของฉัน" },
{ icon: Settings, label: "ตั้งค่า" },
{ icon: LogOut, label: "ออกจากระบบ", danger: true },
],
} | ShellAccount | The signed-in person and their menu. |
| onRead* จำเป็น | (id) => setNotifications((all) => all.map((n) => (n.id === id ? { ...n, unread: false } : n))) | (id: string) => void | Called with an item's id when it is pressed. Mark it read and open it. |
| onReadAll* จำเป็น | () => setNotifications((all) => all.map((n) => ({ ...n, unread: false }))) | () => void | Called by Mark all as read. |
props ทั้งหมด
ตัวอย่างด้านบนใช้ 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. |
| demo | string | — | 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. |
| search | GlobalSearchProps | — | Global search in the top bar, with GlobalSearch's props. Leave it out for no search. |
| defaultOpen | readonly 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.
| ชื่อ | ชนิดข้อมูล | ค่าเริ่มต้น | คำอธิบาย |
|---|---|---|---|
| href* จำเป็น | string | — | Where the logo links to, usually the home page. |
| logo* จำเป็น | string | — | The logo image's URL. |
| 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.
| ชื่อ | ชนิดข้อมูล | ค่าเริ่มต้น | คำอธิบาย |
|---|---|---|---|
| icon* จำเป็น | LucideIcon | — | Shown alone when the menu is collapsed. |
| label* จำเป็น | string | — | The group's name. |
| links* จำเป็น | readonly ShellLink[] | — | The pages in the group, in menu order. |
ShellLink
One link in a menu group.
| ชื่อ | ชนิดข้อมูล | ค่าเริ่มต้น | คำอธิบาย |
|---|---|---|---|
| href* จำเป็น | string | — | Where it goes. Also how current finds the link for the page. |
| label* จำเป็น | string | — | The link text. |
ShellNotification
One item in the notification panel under the bell.
| ชื่อ | ชนิดข้อมูล | ค่าเริ่มต้น | คำอธิบาย |
|---|---|---|---|
| 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.
| ชื่อ | ชนิดข้อมูล | ค่าเริ่มต้น | คำอธิบาย |
|---|---|---|---|
| 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.
| ชื่อ | ชนิดข้อมูล | ค่าเริ่มต้น | คำอธิบาย |
|---|---|---|---|
| icon* จำเป็น | LucideIcon | — | Shown before the label. |
| label* จำเป็น | string | — | What the action does. |
| danger | boolean | — | 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.
| ชื่อ | ชนิดข้อมูล | ค่าเริ่มต้น | คำอธิบาย |
|---|---|---|---|
| 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" | — | loading while the screen's own search runs. |
| shortcut | boolean | — | 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. |
ข้อควรปฏิบัติ
- ส่งข้อความทุกตัวในภาษาของหน้าจออยู่แล้ว แอปแปลเองด้วยไฟล์ข้อความของแอป (t() ของ next-intl) ส่วนคำของ AppShell เอง เช่น ปุ่มเปิดเมนู ปุ่มภาษา และป้ายสำหรับโปรแกรมอ่านหน้าจอ เปลี่ยนตาม LangProvider
- ใส่ลิงก์ข้ามไปยังเนื้อหาไว้ก่อน AppShell และให้ children เป็น main ที่มี id ตรงกัน
- แป้นพิมพ์: Esc ปิดเมนูที่เลื่อนออกมาบนโทรศัพท์ และปิดกระดิ่งกับเมนูบัญชี เมื่อเมนูย่อ ไอคอนแต่ละกลุ่มแสดงชื่อเมื่อชี้หรือโฟกัส
- ส่ง crumbs ให้ตรงกับกลุ่มและลิงก์ของ current เส้นทางคือสิ่งที่โปรแกรมอ่านหน้าจออ่านเป็นชื่อหน้า
- ส่ง demo เฉพาะระบบทดสอบและระบบจำลอง แถบสีทองบอกผู้ใช้ว่าไม่ใช่ข้อมูลจริง
- การย่อเมนูจำไว้ในเบราว์เซอร์ ส่วนการแจ้งเตือนเก็บใน state ของแอป และ onRead กับ onReadAll ต้องอัปเดต state นั้น
ซอร์สโค้ด: ui/src/shell.tsx