ResourceList
A resource’s list screen from manh/shell, declared with one set of columns: status tabs with counts, server-side search and paging, a create form and a link to the detail page.
Everything a business tracks through statuses (repair tickets, purchase requests, orders) is a ResourceList plus a ResourceDetail. The resource block (block_add({ id: "resource" })) writes both, with the table, the API and the status flow.
Usage
import { useApiClient } from "app/api";
import { ResourceList } from "manh/shell";
import { OrderStatus } from "app/shared/order";
import { orderColumns } from "./orders-columns";
export default function OrdersPage() {
const api = useApiClient();
return <ResourceList api={api.orders} title="Đơn hàng" path="/orders" columns={orderColumns} status="status" statusEnum={OrderStatus} />;
}For counts on the tabs, the list endpoint returns listOf(orders, query, { …, counts: "status" }), see listOf.
Signature
function ResourceList<T = any>(props: {
api: { list: (q?: any) => Promise<any>; create?: (body: any) => Promise<any> };
title: ReactNode;
columns: any[];
path?: string;
status?: string;
statusEnum?: Enum; // from defineEnum
titleField?: string;
create?: boolean;
createTitle?: string;
createText?: string;
afterCreate?: (row: T, nav: (to: string) => void) => void;
header?: (reload: () => void) => ReactNode;
params?: Record<string, unknown>;
toolBar?: (reload: () => void) => ReactNode[];
rowActions?: (row: T, reload: () => void) => ReactNode[];
search?: any;
extraColumns?: any[];
pageSize?: number;
}): JSX.Element;Parameters
api: the resource’s group in useApiClient, such asapi.orders. Needslist; withcreatethere is a create button.title: the page title.columns: the resource’s columns (the*-columns.tsxfile), used for the table and the create form.path: the list page’s path. With it, the first column links topath/<id>.status,statusEnum: the status column and its enum; turns on status tabs.titleField: the column that links, instead of the first one.create:falsehides the create button.createTitle,createText: the form title and the button text, “Thêm” by default.afterCreate(row, nav): runs after a create, such as opening the new record. Reloads the list by default.header(reload): content above the table, such as a<StatRow>or a notice.params: a query always sent, such as{ mine: 1 }.toolBar(reload),rowActions(row, reload): extra buttons on the toolbar and on each row.search: theProTablesearch form config;extraColumns: table-only columns;pageSize: 20 by default.
Returns
A page with PageContainer and ProTable. Rows load through tableRequest, so paging, search, filters and sorting run on the server. The create form is built from the same columns, without display-only columns. A create says “Đã thêm”; on error it shows the server’s message and keeps the form open.
On a phone (screens under 768 px) the table keeps the title column, the status, the action column and one money or date column. A column that sets antd’s own responsive follows it: responsive: ["xs", "sm"] keeps a column people need on a phone (a due date) and still shows it on a computer, responsive: ["lg"] hides one on phones and tablets. Never write responsive: ["xs"] alone: antd then shows the column only on screens narrower than 576 px, so it disappears on a computer. The toolbar (toolBar buttons and the create button) wraps on narrow screens, so the page never scrolls sideways.
Examples
Purchase requests: the create button says “Tạo đề xuất”, a new request opens at once, and each row has a copy button:
<ResourceList api={api.requests} title="Đề xuất mua" path="/requests" columns={requestColumns}
status="status" statusEnum={RequestStatus} createText="Tạo đề xuất"
afterCreate={(row, nav) => nav("/requests/" + row.id)}
rowActions={(r, reload) => [<a key="copy" onClick={() => copy(r.id).then(reload)}>Sao chép</a>]} />Screens that are not “a list of X and one X” (dashboards, a point-of-sale counter, a calendar) are written with ProTable and tableRequest.
Next steps
- ResourceDetail: one record’s page, with status buttons.
- defineEnum: labels and colours for the status tabs.
- List pages: when to use
ResourceListand when to write the table yourself.