Form
Form nhập và sửa bản ghi: chọn giữa DrawerForm, ModalForm, StepsForm và ProForm trên trang, định dạng tiền và ngày kiểu Việt Nam, nhập dòng hàng và hiện lỗi server.
Form của pro-components tự lo nút gửi, trạng thái đang gửi, đóng form khi xong và kiểm field bắt buộc. Bạn khai báo field, gọi API trong onFinish rồi trả true để đóng form. Ví dụ dưới đây là form tạo đề xuất mua có số tiền, hạng mục và ngày cần hàng.
// Form tạo/sửa trong DrawerForm: ProFormMoney (₫), nhóm field, tooltip, chặn ngày quá khứ, trả true để đóng.
import { useState } from "react";
import { DrawerForm, PageContainer, ProCard, ProFormDatePicker, ProFormGroup, ProFormMoney, ProFormSelect, ProFormText, ProFormTextArea } from "@ant-design/pro-components";
import { App, Button, Empty, Listy } from "antd";
import { PlusOutlined } from "@ant-design/icons";
import dayjs from "dayjs";
type Req = { title: string; amount: number; category: string; neededBy?: string };
export default function Page() {
const { message } = App.useApp();
const [items, setItems] = useState<Req[]>([]);
return (
<PageContainer title="Đề xuất của tôi">
<ProCard
title="Đã gửi"
headerBordered
extra={
<DrawerForm<Record<string, unknown>>
title="Tạo yêu cầu mua sắm"
width={560}
trigger={<Button type="primary" icon={<PlusOutlined />}>Tạo yêu cầu</Button>}
drawerProps={{ destroyOnHidden: true }}
submitter={{ searchConfig: { submitText: "Gửi đề xuất", resetText: "Huỷ" } }}
initialValues={{ category: "office" }}
onFinish={async (v) => {
setItems((x) => [{ title: String(v.title), amount: Number(v.amount), category: String(v.category), neededBy: v.neededBy ? String(v.neededBy) : undefined }, ...x]);
message.success("Đã gửi đề xuất — đang chờ quản lý duyệt");
return true; // đóng drawer
}}
>
<ProFormText name="title" label="Tiêu đề" placeholder="VD: Gia hạn Microsoft 365" tooltip="Viết đủ để quản lý hiểu không cần mở chi tiết" rules={[{ required: true, min: 3, message: "Ít nhất 3 ký tự" }]} />
<ProFormGroup>
<ProFormMoney name="amount" label="Số tiền" width="sm" customSymbol="₫" locale="vi-VN" min={1} fieldProps={{ precision: 0 }} rules={[{ required: true, message: "Nhập số tiền" }]} />
<ProFormSelect name="category" label="Hạng mục" width="sm" valueEnum={{ office: "Văn phòng phẩm", equipment: "Thiết bị", software: "Phần mềm", service: "Dịch vụ" }} rules={[{ required: true }]} />
</ProFormGroup>
<ProFormDatePicker name="neededBy" label="Cần trước ngày" width="sm" fieldProps={{ format: "DD/MM/YYYY", disabledDate: (d: dayjs.Dayjs) => d.isBefore(dayjs().startOf("day")) }}
// submitted in the DISPLAY format ("20/11/2026") — convert to ISO for the API
transform={(v: string | undefined) => ({ neededBy: v ? v.split("/").reverse().join("-") : undefined })} />
<ProFormTextArea name="description" label="Mô tả" placeholder="Lý do, nhà cung cấp, link báo giá…" fieldProps={{ rows: 4, maxLength: 2000, showCount: true }} />
</DrawerForm>
}
>
{items.length ? (
(items?.length ? (
<Listy items={items} itemRender={(r) => <div className="flex items-start gap-4">
<div className="min-w-0 flex-1">{r.title}{r.neededBy ? ` · cần trước ${r.neededBy}` : ""}</div>
<div className="shrink-0"><b>{r.amount.toLocaleString("vi-VN")} ₫</b></div>
</div>}
rowKey={(_, i) => i}
rowHoverable={false}
/>
) : (
<Empty image={Empty.PRESENTED_IMAGE_SIMPLE} />
))
) : (
<Empty image={Empty.PRESENTED_IMAGE_SIMPLE} description="Chưa có đề xuất nào — bấm “Tạo yêu cầu”." />
)}
</ProCard>
</PageContainer>
);
}
Làm màn hình "Form trong Drawer" cho app của tôi theo mẫu manh.run UI playbook (framework_docs("pro")): Form tạo/sửa trong DrawerForm: ProFormMoney (₫), nhóm field, tooltip, chặn ngày quá khứ, trả true để đóng. Dữ liệu lấy từ API của app qua useApiClient(); nhãn và trường theo spec của tôi.Chọn kiểu form
DrawerForm: tạo hoặc sửa một bản ghi từ danh sách, khi form có từ 6 field trở lên hoặc có dòng hàng.ModalForm: form tối đa 4–5 field, hoặc một quyết định như duyệt, từ chối kèm ghi chú.StepsForm: luồng dài chia thành nhiều bước, ví dụ điền → xác nhận → kết quả.ProFormtrên trang: form dài như cài đặt, hồ sơ, chứng từ nhiều phần. Nút gửi đặt ởFooterToolbar.
Không mở trang riêng chỉ để tạo một bản ghi từ danh sách.
Form tạo và sửa trong Drawer
Dùng một component cho cả nút Tạo yêu cầu ở danh sách lẫn nút Sửa ở trang chi tiết. Khi initial có giá trị, form ở chế độ sửa.
<DrawerForm
title={title} trigger={trigger} width={560}
drawerProps={{ destroyOnHidden: true }}
submitter={{ searchConfig: { submitText: initial ? "Lưu thay đổi" : "Gửi đề xuất", resetText: "Huỷ" } }}
initialValues={initial ?? { category: "office" }}
onFinish={async (v) => { await onSubmit(v); return true; }} // trả true thì Drawer đóng
>
<ProFormText name="title" label="Tiêu đề" tooltip="Viết đủ để quản lý hiểu không cần mở chi tiết"
rules={[{ required: true, min: 3, max: 200, message: "3–200 ký tự" }]} />
<ProFormGroup>
<ProFormMoney name="amount" label="Số tiền" width="sm" customSymbol="₫" locale="vi-VN" min={1}
fieldProps={{ precision: 0 }} rules={[{ required: true, message: "Nhập số tiền lớn hơn 0" }]} />
<ProFormSelect name="category" label="Hạng mục" width="sm" valueEnum={CATEGORY_ENUM} rules={[{ required: true }]} />
</ProFormGroup>
<ProFormTextArea name="description" label="Mô tả" fieldProps={{ rows: 5, maxLength: 2000, showCount: true }} />
</DrawerForm>Ở trang danh sách, gửi xong thì gọi actionRef.current?.reload() để bảng tải lại.
Ngày hiện DD/MM/YYYY, gửi YYYY-MM-DD
Field ngày có fieldProps.format gửi đi đúng chuỗi đang hiện (“20/11/2026”), nên API từ chối. Đổi lại định dạng ngay trên field bằng transform:
<ProFormDatePicker
name="neededBy" label="Cần trước ngày" width="sm"
fieldProps={{ format: "DD/MM/YYYY", disabledDate: (d) => d.isBefore(dayjs().startOf("day")) }}
transform={(v) => ({ neededBy: v ? v.split("/").reverse().join("-") : undefined })}
/>Hoặc bỏ format để form gửi YYYY-MM-DD.
Độ rộng và nhóm field
width="xs" | "sm" | "md" | "lg" | "xl"(104 / 216 / 328 / 440 / 552 px) để các field rộng đều nhau.- Cần cột co giãn theo màn hình:
gridtrên form +colProps={{ xl: 12 }}trên field. - Không trộn
widthvớifieldProps.style.width. - Nhiều field ngắn trên một hàng: bọc trong
ProFormGroup.
Field phụ thuộc field khác
Hiện field theo giá trị field khác bằng ProFormDependency:
<ProFormRadio.Group name="delivery" label="Nhận hàng" options={[
{ label: "Giao tận nơi", value: "ship" }, { label: "Lấy tại cửa hàng", value: "pickup" },
]} />
<ProFormDependency name={["delivery"]}>
{({ delivery }) => delivery === "ship"
? <ProFormText name="address" label="Địa chỉ giao" rules={[{ required: true }]} />
: <ProFormSelect name="storeId" label="Cửa hàng" request={loadStores} showSearch />}
</ProFormDependency>Dòng hàng trong form
Đặt name cho EditableProTable thì bảng trở thành một field, và onFinish nhận values.lines. Bỏ key tạm trước khi gửi. Server tự tính giá và tổng.
const [keys, setKeys] = useState<React.Key[]>([]);
<EditableProTable<Line>
name="lines" rowKey="key" columns={lineColumns} // cột tiền chỉ đọc
recordCreatorProps={{ position: "bottom", record: () => ({ key: Date.now() }), creatorButtonText: "Thêm dòng" }}
editable={{ type: "multiple", editableKeys: keys, onChange: setKeys }}
/>
// onFinish: lines.map(({ key, ...l }) => l)Dòng có 1–2 field thì dùng ProFormList cho gọn.
Form dài trên trang
Form cài đặt hoặc chứng từ nhiều phần đặt thẳng trên trang. Mỗi phần một ProCard, nút gửi cố định ở cuối trang:
<ProForm layout="vertical" onFinish={save}
submitter={{ render: (_, dom) => <FooterToolbar>{dom}</FooterToolbar> }}>
<ProCard title="Công ty" headerBordered>{/* … */}</ProCard>
<ProCard title="Hoá đơn" headerBordered>{/* … */}</ProCard>
</ProForm>Hiện lỗi server
API trả lỗi 4xx với nội dung { error, message, code }. Bọc onFinish bằng submit từ app/hooks: hàm gọi API, báo thành công, tải lại bảng và trả true để đóng form. Khi lỗi, server gửi kèm lỗi từng ô thì hiện ngay dưới ô đó, còn lại hiện message của server, và form không đóng.
<DrawerForm form={form} title="Đề xuất mới"
onFinish={submit((v) => api.requests.create(v), { done: "Đã gửi đề xuất", reload: tableRef, form })}>
{/* … */}
</DrawerForm>Cần câu lỗi để hiện ở chỗ khác thì dùng errorText(e) từ manh/shell.
Form nhiều bước
StepsForm chia luồng dài thành nhiều bước. Mỗi StepsForm.StepForm kiểm field của bước đó trước khi sang bước sau. Bước cuối là Result nên ẩn nút ở bước này.
// Form nhiều bước: StepsForm (điền → xác nhận → kết quả). Bước cuối ẩn nút, hiện Result.
import { useState } from "react";
import { PageContainer, ProCard, ProFormDigit, ProFormSelect, ProFormText, StepsForm } from "@ant-design/pro-components";
import { Alert, Button, Descriptions, Result } from "antd";
export default function Page() {
const [current, setCurrent] = useState(0);
const [data, setData] = useState<Record<string, unknown>>({});
return (
<PageContainer title="Chuyển kho" subTitle="Chuyển hàng giữa các kho trong 3 bước">
<ProCard>
<StepsForm
current={current}
onCurrentChange={setCurrent}
submitter={{
render: (props, dom) => (props.step === 2 ? null : dom),
submitButtonProps: { style: { minWidth: 120 } },
}}
onFinish={async () => true}
>
<StepsForm.StepForm name="fill" title="Điền phiếu" onFinish={async (v) => { setData(v); return true; }}>
<ProFormSelect name="from" label="Từ kho" width="md" rules={[{ required: true }]} valueEnum={{ hn: "Kho Hà Nội", hcm: "Kho TP.HCM" }} />
<ProFormSelect name="to" label="Đến kho" width="md" rules={[{ required: true }]} valueEnum={{ hn: "Kho Hà Nội", hcm: "Kho TP.HCM", dn: "Kho Đà Nẵng" }} />
<ProFormText name="sku" label="Mã hàng" width="md" placeholder="SKU-0001" rules={[{ required: true }]} />
<ProFormDigit name="qty" label="Số lượng" width="sm" min={1} fieldProps={{ precision: 0 }} rules={[{ required: true }]} />
</StepsForm.StepForm>
<StepsForm.StepForm name="confirm" title="Xác nhận">
<Alert type="info" showIcon title="Kiểm tra lại trước khi tạo phiếu — phiếu đã tạo không sửa được." style={{ marginBottom: 24 }} />
<Descriptions column={1} bordered size="small" items={[{ label: "Từ kho", children: String(data.from ?? "") }, { label: "Đến kho", children: String(data.to ?? "") }, { label: "Mã hàng", children: String(data.sku ?? "") }, { label: "Số lượng", children: String(data.qty ?? "") }]} />
</StepsForm.StepForm>
<StepsForm.StepForm name="done" title="Hoàn tất">
<Result status="success" title="Đã tạo phiếu chuyển kho" subTitle="Kho nhận sẽ thấy phiếu trong hộp “Chờ nhận”."
extra={[<Button key="again" type="primary" onClick={() => setCurrent(0)}>Tạo phiếu khác</Button>, <Button key="view">Xem phiếu</Button>]} />
</StepsForm.StepForm>
</StepsForm>
</ProCard>
</PageContainer>
);
}
Làm màn hình "Form nhiều bước" cho app của tôi theo mẫu manh.run UI playbook (framework_docs("pro")): Form nhiều bước: StepsForm (điền → xác nhận → kết quả). Bước cuối ẩn nút, hiện Result. Dữ liệu lấy từ API của app qua useApiClient(); nhãn và trường theo spec của tôi.<StepsForm onFinish={async (v) => { await api.shops.create(v); return true; }}
submitter={{ render: (p, dom) => (p.step === 2 ? null : dom) }}>
<StepsForm.StepForm name="info" title="Điền thông tin">{/* … */}</StepsForm.StepForm>
<StepsForm.StepForm name="confirm" title="Xác nhận">{/* Descriptions + Alert */}</StepsForm.StepForm>
<StepsForm.StepForm name="done" title="Hoàn tất"><Result status="success" title="Đã tạo cửa hàng" /></StepsForm.StepForm>
</StepsForm>Cần biết
- Nếu chỉ có
destroyOnHidden, mở lạiModalForm/DrawerFormthì dữ liệu cũ vẫn còn trong field. Với form tạo mới, thêmformRef.current?.resetFields()trongonOpenChange(false), hoặc dùngdrawerProps.destroyOnClose/modalProps.destroyOnClosevà chịu một cảnh báo khi chạy ở chế độ dev. - Không render nguyên đối tượng lỗi. Truyền object làm phần tử con thì React báo lỗi và trang bị sập.
- Tiền:
ProFormMoney(customSymbol="₫",locale="vi-VN",precision: 0); số:ProFormDigit; khoảng ngày:ProFormDateRangePicker. - Đặt chữ trên nút bằng từ người dùng hay nói, qua
submitter.searchConfig.
Bước tiếp theo
- Trang chi tiết —
ModalFormduyệt và từ chối có ghi chú. - Trang danh sách — nơi nút Tạo yêu cầu mở Drawer.
- API handler — kiểm dữ liệu gửi lên bằng Zod ở server.