跳到主要内容

ActionGroup 操作组

从声明式配置数组渲染一组操作按钮,支持上下文感知的按钮显隐和权限检查。

VEF 特有组件,不属于 Ant Design。

何时使用

  • 表格行操作列,按钮因行数据不同而变化。
  • 工具栏中有一组固定操作。
  • 希望将按钮定义为数据而非 JSX 的场景。

基础用法

import { ActionGroup } from '@vef-framework-react/components';
import type { ActionButtonConfig } from '@vef-framework-react/components';

const buttons: ActionButtonConfig[] = [
{
key: 'edit',
label: '编辑',
onClick: async () => { await openEditModal(); },
},
{
key: 'delete',
label: '删除',
danger: true,
confirmable: true,
confirmTitle: '删除记录',
confirmDescription: '此操作不可撤销。',
onClick: async () => { await deleteRecord(); },
},
];

export default function Demo() {
return <ActionGroup buttons={buttons} />;
}

上下文感知按钮

传入 context(如表格行数据)使按钮配置感知上下文:

import { ActionGroup } from '@vef-framework-react/components';
import type { ActionButtonConfig } from '@vef-framework-react/components';

interface User {
id: number;
status: 'active' | 'inactive';
}

const buttons: ActionButtonConfig<User>[] = [
{
key: 'edit',
label: '编辑',
onClick: async (ctx) => { await editUser(ctx.id); },
},
{
key: 'activate',
label: '启用',
hidden: (ctx) => ctx.status === 'active',
onClick: async (ctx) => { await activateUser(ctx.id); },
},
{
key: 'deactivate',
label: '禁用',
hidden: (ctx) => ctx.status === 'inactive',
danger: true,
onClick: async (ctx) => { await deactivateUser(ctx.id); },
},
];

// 在表格列 render 中:
{
title: '操作',
render: (_, record) => (
<ActionGroup<User> buttons={buttons} context={record} />
),
}

自定义包装器

<ActionGroup
buttons={buttons}
renderWrapper={(buttonsNode) => (
<div style={{ display: 'flex', gap: 4 }}>
{buttonsNode}
</div>
)}
/>

API

ActionGroupProps

属性类型默认值说明
buttonsActionButtonConfig<TContext>[]必填按钮配置数组
size'large' | 'middle' | 'small'应用于所有按钮的尺寸
contextTContext传递给按钮回调的上下文值
renderWrapper(buttonsNode: ReactNode) => ReactNode自定义包装器渲染函数

ActionButtonConfig

字段类型默认值说明
keystring必填唯一标识
labelReactNode按钮文字
typeButtonType按钮类型
dangerbooleanfalse危险样式
disabledboolean | (ctx) => booleanfalse禁用条件
hiddenboolean | (ctx) => booleanfalse隐藏条件
confirmablebooleanfalse需要确认
confirmMode`'popover' \'dialog'`'popover'
confirmTitleReactNode确认标题
confirmDescriptionReactNode确认描述
permTokensstring | string[]所需权限令牌
checkMode'any' | 'all''any'权限检查模式
onClick(ctx?: TContext) => Awaitable<void>点击回调
iconReactNode按钮图标

最佳实践

  • 在组件外部定义按钮配置,避免每次渲染重新创建。
  • 对不适用于某条记录的按钮使用 hidden(而非 disabled)。
  • 使用 permTokens 自动隐藏用户无权限的按钮。