# 前端表单规范 > 本文档定义 Valor 前端表单弹窗的尺寸、布局与对齐规范。模块开发架构见 [前端模块开发.md](前端模块开发.md)。 --- ## 1. 全局默认值 表单全局配置在 `src/adapter/form.ts` 中定义,**不允许在单个表单中覆盖**: | 配置项 | 值 | 说明 | |---|---|---| | `labelWidth` | `108`px | 所有表单 label 统一宽度 | | `labelAlign` | 右对齐(框架默认) | 不显式设置 | | 控件宽度 | `w-full` | 通过 `commonConfig.componentProps.class` 全局注入 | | 必填校验 | `$t('ui.formRules.required', [label])` | 输入类用 `rules: 'required'`,下拉类用 `rules: 'selectRequired'` | ```typescript // adapter/form.ts 全局配置 setupVbenForm({ config: { commonConfig: { componentProps: { class: 'w-full' }, }, labelWidth: 108, }, }); ``` --- ## 2. 弹窗尺寸 弹窗宽度根据表单复杂度和附加内容选择,统一通过 `class` 设置: ```typescript const [Modal, modalApi] = useVbenModal({ class: 'w-[{宽度}px]', // ... }); ``` ### 2.1 宽度对照表 | 复杂度 | 宽度 | 适用场景 | 示例 | |---|---|---|---| | **简单** | `600px` | 1-3 字段表单、确认操作 | 认领弹窗、扫描输入 | | **中等** | `720-800px` | 4-6 字段表单、选择器弹窗 | 物品选择、配件查询 | | **标准** | `900px` | 6-10 字段表单 | 订单表单、产品表单、分派表单 | | **大型** | `960px` | 表单 + 内嵌 Grid | 出货单(表单 + 物品表格 + 扫码) | | **超大型** | `980-1100px` | 复合 UI(卡片选择器 + 表单 + Grid) | 入库单、采购单、审批详情 | ### 2.2 选用规则 ``` 字段数量 + 无附加内容 → 900px 字段数量 + 附件管理器 → 900px 字段数量 + 内嵌子表格 → 960px 表单 + 卡片/购物车式选择器 → 980-1000px 完整详情展示(只读) → 960-1100px 简单操作(≤3 字段) → 600-720px ``` > `MODAL_WIDTH` 常量(`#/constants/modal`)提供 SMALL=600 / MEDIUM=700 / LARGE=900 / XLARGE=960 四档,推荐简单弹窗使用常量,复杂弹窗直接写像素值。 --- ## 3. 表单布局 ### 3.1 单列布局(默认) 绝大多数表单使用单列布局,不设置 `wrapperClass`: ```typescript const [Form, formApi] = useVbenForm({ schema: useEditFormSchema(), showDefaultActions: false, }); ``` ``` ┌──────────────────────────────────────┐ │ Label 1 [ Input ] │ │ Label 2 [ Select ] │ │ Label 3 [ InputNumber ] │ │ Label 4 [ DatePicker ] │ │ Remark [ Textarea ] │ │ [ ] │ │ [ ] │ └──────────────────────────────────────┘ ``` ### 3.2 双列布局 字段数 ≥ 8 且字段较短时,使用 `grid-cols-2` 双列布局: ```typescript const [Form, formApi] = useVbenForm({ schema: useXxxFormSchema(), showDefaultActions: false, wrapperClass: 'grid-cols-2', }); ``` ``` ┌────────────────────────┬────────────────────────┐ │ Label 1 [ Input ] │ Label 2 [ Select ] │ │ Label 3 [ Input ] │ Label 4 [ Input ] │ │ Label 5 [ Textarea ] │ │ [ ] │ └──────────────────────────────────────────────────┘ ``` 需要跨列的字段使用 `formItemClass: 'col-span-2'`: ```typescript // 宽字段占满整行 { component: 'Input', fieldName: 'orderTitle', label: $t('order.orderTitle'), formItemClass: 'col-span-2', }, // 备注永远跨列 { component: 'Textarea', fieldName: 'remark', label: $t('system.remark'), formItemClass: 'col-span-2', componentProps: { rows: 3 }, }, // 内嵌子表格占满整行(含边框容器) { component: 'Slot', fieldName: 'skuSlot', slot: 'skuSlot', formItemClass: 'col-span-full', label: $t('inventory.sku.list'), }, ``` ### 3.3 何时用双列 | 条件 | 布局 | |---|---| | 字段 ≤ 6 | 单列 | | 字段 7-10,无长文本字段 | 双列 | | 字段 ≥ 8,含 Divider 分组 | 单列(Divider 不适合双列) | | 任意字段数 + 内嵌 Grid | 双列(Grid 区域 `col-span-full`) | > 当前项目仅使用 `grid-cols-2`,不使用 `grid-cols-3` 或 `grid-cols-4`。 --- ## 4. 字段排列顺序 表单字段按以下顺序排列,从上到下: | 顺序 | 字段类型 | 说明 | |---|---|---| | 1 | **编码/标识** | 如 `orderNo`,通常 `disabled: true`(自动生成) | | 2 | **类型/分类** | 如 `orderType`、`productId`,通常 `rules: 'required'` | | 3 | **关联选择** | 如 `customerId`(ApiSelect)、`orderId` | | 4 | **名称/标题** | 如 `orderTitle`,可跨列 | | 5 | **状态/优先级** | 如 `priority`、`status`,通常有 `defaultValue` | | 6 | **数值/日期** | 如 `expectedItemQty`、`expectedDeliveryTime` | | 7 | **备注** | `Textarea`,`rows: 3`,始终在最后 | 示例(订单表单): ```typescript export function useOrderFormSchema(): VbenFormSchema[] { return [ { component: 'Select', fieldName: 'orderType', rules: 'required' }, { component: 'Input', fieldName: 'orderNo', componentProps: { disabled: true } }, { component: 'ApiSelect', fieldName: 'customerId' }, { component: 'Select', fieldName: 'sourceChannel', defaultValue: 1 }, { component: 'Input', fieldName: 'orderTitle', formItemClass: 'col-span-2' }, { component: 'Input', fieldName: 'customerOrderNo' }, { component: 'Select', fieldName: 'priority', defaultValue: 2 }, { component: 'InputNumber', fieldName: 'expectedItemQty', rules: 'required' }, { component: 'DatePicker', fieldName: 'expectedDeliveryTime' }, { component: 'Textarea', fieldName: 'remark', formItemClass: 'col-span-2', componentProps: { rows: 3 } }, ]; } ``` --- ## 5. 字段分组 ### 5.1 Divider 分组 当表单字段可以按逻辑分组(如公司信息 + 联系人信息)时,使用 `Divider` 组件: ```typescript { component: 'Divider', fieldName: 'divider0', label: $t('basic.customer.companyInfo'), // 分组标题 }, // ...公司信息字段... { component: 'Divider', fieldName: 'divider1', label: $t('basic.customer.contact1.title'), }, // ...联系人字段... ``` > `Divider` 的 `fieldName` 命名为 `divider` + 序号,如 `divider0`、`divider1`。 ### 5.2 视觉分组(模板层) 复杂弹窗在模板中使用带边框的容器对区域做视觉分隔: ```html