表单与表格适配层
@vben/components/adapter 是组件包中使用最广的模块(三个应用近 200 处引用):它把 antdv-next 全家桶与 mdp 业务组件接入 vben 的表单(useVbenForm)和表格(useVbenVxeGrid)体系,并提供 mdp 定制版的 vxe 单元格渲染器。日常开发页面时,你直接接触的 useVbenForm、useVbenVxeGrid 就来自这里。
1. 应用启动时的初始化
每个应用的 src/bootstrap.ts 在启动阶段依次执行三个初始化函数(顺序固定,新增应用时照抄即可):
import {
initComponentAdapter,
initSetupVbenForm,
initVxeTable,
} from '@vben/components/adapter';
await initComponentAdapter(); // 注册全局表单/详情组件
await initSetupVbenForm(); // 配置 vben-form 的 v-model 适配
initVxeTable(); // 注册 vxe-table 单元格渲染器各自做了什么:
| 初始化函数 | 作用 |
|---|---|
initComponentAdapter() | 把 antdv-next 组件(Input/Select/DatePicker/Upload 等,全部 defineAsyncComponent 按需加载)与 mdp 业务组件(ApiSelect、ApiDict、IconPicker、MarkdownEditor、富文本等)注册进 vben 表单与详情的全局组件表;同时注入全局 message/Modal/notification |
initSetupVbenForm() | 设定表单控件的 v-model 适配:baseModelPropName: 'value',Checkbox/Radio/Switch 映射为 checked,Upload 映射为 fileList;内置 required/selectRequired 国际化校验规则 |
initVxeTable() | 注册 CellDict、CellImage、CellLink、CellOperation 等单元格渲染器,并把 view 组件注册为列渲染器、全局组件注册为可编辑渲染器 |
2. useVbenForm 与 VbenFormSchema
adapter 导出的 useVbenForm 是对 @vben/common-ui 同名函数的泛型包装,配套类型 VbenFormSchema / VbenFormProps / z(zod):
import { useVbenForm, z } from '@vben/components/adapter';
import type { VbenFormSchema } from '@vben/components/adapter';
const schema: VbenFormSchema[] = [
{
fieldName: 'name',
label: '应用名称',
component: 'Input', // antdv 组件,已全局注册
rules: 'required', // 内置国际化校验
},
{
fieldName: 'state',
label: '状态',
component: 'ApiDict', // mdp 业务组件,同样直接写名字
componentProps: { type: 'common.state' },
},
];
const [Form, formApi] = useVbenForm<FormValues, AppDto>({ schema });component 字段可写的名字由 adapter 的 ComponentType 联合类型约束(含全部 antdv 组件 + CustomComponentType 业务组件 + CustomDetailComponentType 详情组件),ComponentPropsMap 为每个组件名提供 props 类型提示。
component字段支持的完整ComponentType类型请看源码: packages/effects/components/src/adapter/component/index.ts
约定式扩展:业务组件自动注册
除包内组件外,apps/*/src/views/**/components/form/*.vue 会被 form/component-map.ts 通过 import.meta.glob 自动注册为表单组件(文件名转 PascalCase 即组件名,文件名含 -ignore 则跳过)。同理 views/**/components/view/*.vue 自动注册为 TextXxx 详情组件。应用级私有的表单控件按此约定放置即可在 schema 中直接使用,无需手动注册。
3. useVbenVxeGrid 与单元格渲染器
adapter 的 useVbenVxeGrid 是 setupVbenVxeTable 之后的泛型包装,列配置中通过 cellRender.name 使用 mdp 注册的内置渲染器:
| 渲染器 name | 用途 | 关键参数(attrs) |
|---|---|---|
CellDict | 字典回显(最常用) | type(字典类型,必填)、mode: 'tag'/'badge'/'text'、dataType、useCss |
CellImage | 图片缩略图 | - |
CellLink | 链接(点击跳转) | - |
CellVxeLink | vxe 风格链接 | - |
CellText | 文本(省略/复制) | - |
CellTag | 标签 | - |
CellSwitch | 开关(行内切换状态) | - |
CellOperation | 行内操作按钮 | 见下文 |
// 真实示例:apps/web-console/src/views/open/admin/app/data/index.tsx
const columns: VxeGridPropTypes.Columns = [
{ field: 'name', title: '应用名称' },
{
field: 'state',
title: '状态',
cellRender: {
name: 'CellDict',
attrs: { type: DictUniqKey.console.app.state, mode: 'tag' },
},
},
{
field: 'operation',
title: '操作',
cellRender: {
name: 'CellOperation',
attrs: {
actions: [
{ code: 'edit', text: '编辑', auth: 'open:app:update' },
{ code: 'delete', text: '删除', auth: 'open:app:delete' },
],
},
},
},
];
const [Grid, gridApi] = useVbenVxeGrid<AppVo>({ gridOptions: { columns } });4. VbenTableAction
vxe-table.ts 导出的 VbenTableAction 是对官方同名组件的增强:内置权限注入——ActionItem.auth 声明权限码后,无权限的按钮自动隐藏,无需手动包 v-access。
TableAction 与 VbenTableAction 的区别
@vben/components/table-action 的 TableAction 用于工具栏/模板中手动放置的操作组(见 表格操作列);VbenTableAction 配合 vxe 表格的 CellOperation 渲染器用于行内操作列。两者 ActionItem 结构一致,都支持 auth 权限码。