Khung app và đầu trang
Dựng khung app bằng AppShell, gồm menu theo vai trò, tài khoản và nút đăng xuất. Dựng đầu mỗi trang bằng PageContainer, gồm tiêu đề, trạng thái, tóm tắt, số chính và nút quyết định.
Khung là phần người dùng thấy ở mọi trang sau khi đăng nhập: logo, menu, tài khoản. Đầu trang cho biết người dùng đang ở đâu và làm được gì. Cả hai đều dựng bằng prop của component Pro, không cần viết CSS.
// Khung app: ProLayout mix + menu nhóm 3 cấp + app launcher 9 chấm + icon header + avatar.
// Trong app thật: menuItemRender dùng useNavigate()/<Link>, location = useLocation().
import { useState } from "react";
import { PageContainer, ProCard, ProLayout } from "@ant-design/pro-components";
import { Dropdown, Typography, theme } from "antd";
import {
AppstoreOutlined, AuditOutlined, BarChartOutlined, FileTextOutlined, GithubFilled,
InfoCircleFilled, LogoutOutlined, QuestionCircleFilled, ShoppingOutlined, TeamOutlined,
} from "@ant-design/icons";
const route = {
path: "/",
children: [
{
path: "/ops", name: "Vận hành", icon: <AppstoreOutlined />,
children: [
{ path: "/ops/orders", name: "Đơn hàng", icon: <ShoppingOutlined />, children: [
{ path: "/ops/orders/all", name: "Tất cả đơn" },
{ path: "/ops/orders/pending", name: "Chờ xử lý" },
] },
{ path: "/ops/requests", name: "Đề xuất mua sắm", icon: <FileTextOutlined />, children: [
{ path: "/ops/requests/mine", name: "Của tôi" },
{ path: "/ops/requests/inbox", name: "Cần tôi duyệt" },
] },
],
},
{
path: "/report", name: "Báo cáo", icon: <BarChartOutlined />,
children: [
{ path: "/report/sales", name: "Doanh số", icon: <BarChartOutlined />, children: [
{ path: "/report/sales/month", name: "Theo tháng" },
] },
],
},
],
};
const appList = [
{ title: "Nội bộ", desc: "Ứng dụng của công ty", children: [
{ title: "CRM", desc: "Khách hàng & cơ hội", icon: <TeamOutlined />, url: "#crm" },
{ title: "Kho", desc: "Tồn kho & phiếu xuất", icon: <ShoppingOutlined />, url: "#kho" },
] },
{ title: "Công cụ", desc: "Dùng chung", children: [
{ title: "Duyệt mua sắm", desc: "Đề xuất & phê duyệt", icon: <AuditOutlined />, url: "#duyet" },
] },
];
export default function Page() {
const { token } = theme.useToken();
const [pathname, setPathname] = useState("/ops/orders/all");
return (
<div style={{ height: "100vh" }}>
<ProLayout
title="Acme Admin"
logo={<AuditOutlined style={{ fontSize: 22, color: token.colorPrimary }} />}
layout="mix"
splitMenus
siderMenuType="group"
menu={{ collapsedShowGroupTitle: true, locale: false }}
fixSiderbar
route={route}
location={{ pathname }}
menuItemRender={(item, dom) => <a onClick={() => item.path && setPathname(item.path)}>{dom}</a>}
appList={appList}
bgLayoutImgList={[
{ src: "https://img.alicdn.com/imgextra/i2/O1CN01O4etvp1DvpFLKfuWq_!!6000000000279-2-tps-609-606.png", left: 85, bottom: 100, height: "303px" },
{ src: "https://img.alicdn.com/imgextra/i3/O1CN018NxReL1shX85Yz6Cx_!!6000000005798-2-tps-884-496.png", bottom: 0, left: 0, width: "331px" },
]}
actionsRender={(p) => (p.isMobile ? [] : [<InfoCircleFilled key="i" />, <QuestionCircleFilled key="q" />, <GithubFilled key="g" />])}
avatarProps={{
size: "small",
title: "Nguyễn Thu Hà",
src: "https://manh.run/avatars/06.svg",
render: (_, dom) => <Dropdown menu={{ items: [{ key: "out", icon: <LogoutOutlined />, label: "Đăng xuất" }] }}>{dom}</Dropdown>,
}}
menuFooterRender={(p) => (p?.collapsed ? undefined : <div style={{ textAlign: "center", paddingBlockStart: 12, color: token.colorTextTertiary }}>v1.0 · Acme</div>)}
>
<PageContainer subTitle={pathname}>
<ProCard>
<Typography.Paragraph>
Bấm các mục menu (trên: nhóm cấp 1, trái: tiêu đề nhóm cấp 2 + mục cấp 3) và nút 9 chấm cạnh logo.
</Typography.Paragraph>
</ProCard>
</PageContainer>
</ProLayout>
</div>
);
}
Làm màn hình "Khung app ProLayout" cho app của tôi theo mẫu manh.run UI playbook (framework_docs("pro")): Khung app: ProLayout mix + menu nhóm 3 cấp + app launcher 9 chấm + icon header + avatar. Dữ liệu lấy từ API của app qua useApiClient(); nhãn và trường theo spec của tôi.Vị trí trong cây app
Khung là AppShell từ manh/shell, đặt trong app/layout.tsx. Trang đăng nhập và đăng ký tự hiện không có khung.
app/
├── layout.tsx AppShell: menu, vai trò, người đang đăng nhập
├── theme.tsx màu chính, bo góc, chế độ tối
├── login/page.tsx trang công khai, không có khung
└── requests/page.tsx trang trong khung, mở đầu bằng PageContainerDựng khung bằng AppShell
app/layout.tsx chỉ khai báo khung có gì. Menu theo vai trò, nút đăng xuất, tiếng Việt và giao diện đã có sẵn trong AppShell.
import { AppShell } from "manh/shell";
import { DashboardOutlined, FileTextOutlined, TeamOutlined } from "@ant-design/icons";
import { getUser, signOut } from "app/hooks";
import { APP_THEME } from "app/theme";
const MENU = [
{ path: "/", name: "Tổng quan", icon: <DashboardOutlined /> },
{ path: "/requests", name: "Đề xuất", icon: <FileTextOutlined /> },
{ path: "/users", name: "Người dùng", icon: <TeamOutlined />, roles: ["admin"] },
];
export default function RootLayout() {
return (
<AppShell appName="Duyệt mua sắm" menu={MENU} roleLabels={{ admin: "Quản trị", staff: "Nhân viên" }}
user={getUser()} onSignOut={signOut} theme={APP_THEME} />
);
}rolesẩn mục với người không có vai đó. Nhóm menu viết{ name, icon, children: [...] }.count: () => sốhiện số việc đang chờ cạnh mục menu, tải lại mỗi lần chuyển trang.- Trang chi tiết
/requests/12vẫn làm sáng mục Đề xuất. Người chưa đăng nhập được đưa về/login.
Bỏ khung ở trang công khai
/login và /register không có khung. Trang công khai khác, như form đặt lịch hay tra cứu đơn, liệt kê trong publicPaths. API của trang đó đặt export const access = "public".
const PUBLIC_PATHS = ["/login", "/register", "/dat-lich"];
<AppShell /* … */ publicPaths={PUBLIC_PATHS} />Cả một nhánh đường dẫn công khai thì dùng isPublic={(p) => p.startsWith("/tra-cuu")}.
Tự dựng khung bằng ProLayout
Cần menu cấp 1 trên thanh trên cùng hay nút chín chấm để chuyển app thì thay AppShell bằng ProLayout của bạn.
<ProLayout
layout="mix" splitMenus // cấp 1 trên thanh trên cùng, cấp 2–3 bên trái
siderMenuType="group" // cấp 2 thành tiêu đề nhóm, cấp 3 thành mục
menu={{ collapsedShowGroupTitle: true, locale: false }}
route={{ path: "/", children: [
{ path: "/ops", name: "Vận hành", icon: <AppstoreOutlined />, children: [
{ path: "/ops/orders", name: "Đơn hàng", children: [{ path: "/ops/orders/all", name: "Tất cả đơn" }] },
] },
] }}
appList={[ // nút chín chấm cạnh logo
{ title: "Nội bộ", desc: "Ứng dụng của công ty", children: [
{ title: "CRM", desc: "Khách hàng và cơ hội", icon: <TeamOutlined />, url: "https://crm.example.vn", target: "_blank" },
] },
]}
menuFooterRender={(p) => (p?.collapsed ? undefined : <div>v1.0</div>)}
location={{ pathname }}
menuItemRender={(item, dom) => <a onClick={() => item.path && nav(item.path)}>{dom}</a>}
/>-
menuItemRendervàlocationlà bắt buộc: thiếu chúng thì bấm menu không chuyển trang, không mục nào sáng. -
appList: mục cóchildrenhiện thành nhóm có tiêu đề. Khi mọi mục đều códesc, bảng chọn app hiện dạng thẻ lớn, nếu không thì hiện lưới icon gọn. -
splitMenuschỉ có nghĩa khilayout="mix". Mục đang chọn trên thanh trên cùng tính từlocation.pathname. -
breakpoint(mặc địnhlg) tự thu gọn menu trái; muốn tự điều khiển thì dùngcollapsed+onCollapse.
Đầu trang bằng PageContainer
Mỗi trang trong khung mở đầu bằng PageContainer. Mỗi loại thông tin ở đầu trang có một prop riêng:
<PageContainer
header={{
title: r.title,
breadcrumb: { items: [{ title: <a onClick={() => nav("/requests")}>Đề xuất</a> }, { title: `#${r.id}` }] },
tags: <Tag color="processing">Chờ duyệt</Tag>,
extra: <FlowActions flow={RequestFlow} record={r} labels={RequestStatus} move={move} onDone={refresh} />,
}}
content={<ProDescriptions column={{ xs: 1, sm: 2, lg: 4 }}>{/* 3–4 thông tin chính */}</ProDescriptions>}
extraContent={<Statistic title="Số tiền đề xuất" value={vnd(r.amount)} />} // con số quan trọng nhất
>
{/* … */}
</PageContainer>contentlà dải tóm tắt dưới tiêu đề;extraContentlà 1–3 con số canh phải.header.extralà các nút chuyển trạng thái.FlowActionschỉ hiện những bước người đang đăng nhập được làm, xem Trang chi tiết.- Trang có nhiều phần như thông tin, lịch sử, mục liên quan: dùng
tabList+tabActiveKey+onTabChange. - Dashboard:
titlelà lời chào,subTitlelà số việc cần làm (“2 đề xuất đang chờ bạn duyệt”), một nút chính ởextra.
Cần biết
- Không dùng
onBacktrênPageContainervì trang sẽ sập. Dùngheader.breadcrumb. - Không tự ghép
Layout.Sider+Menu, không ghi đè class.ant-pro-*, không đểSettingDrawertrong bản chạy thật. - Màu khung lấy từ
app/theme.tsx. Khung tự dựng thì dùngtokencủaProLayout, không gõ mã màu.
Bước tiếp theo
- Trang danh sách — trang mở từ menu Đề xuất.
- Trang chi tiết —
PageContainerđầy đủ với nút duyệt. - Trang và layout —
app/layout.tsx, trang và đường dẫn động.