feat(form): group-list 支持 max 限制和 beforeAddRow 前置校验

- group-list 新增 max 配置项,限制最大行数
- group-list 支持 beforeAddRow 异步前置校验回调
- table 的 beforeAddRow 支持返回 Promise

Made-with: Cursor
This commit is contained in:
roymondchen 2026-04-23 15:06:45 +08:00
parent 9bf42f9007
commit ac755ac3d0
3 changed files with 20 additions and 3 deletions

View File

@ -761,7 +761,7 @@ export interface TableConfig extends FormItem {
titleTip?: FilterFunction<string>;
rowKey?: string;
/** table 新增行时前置回调 */
beforeAddRow?: (mForm: FormState | undefined, data: any) => boolean;
beforeAddRow?: (mForm: FormState | undefined, data: any) => boolean | Promise<boolean>;
addButtonConfig?: {
props?: Record<string, any>;
text?: string;
@ -804,6 +804,9 @@ export interface GroupListConfig<T = never> extends FormItem {
props?: Record<string, any>;
text?: string;
};
/** 最大行数 */
max?: number;
beforeAddRow?: (mForm: FormState | undefined, data: any) => boolean | Promise<boolean>;
}
interface StepItemConfig<T = never> extends FormItem, ContainerCommonConfig<T> {

View File

@ -50,7 +50,7 @@ import { computed, inject } from 'vue';
import { Grid, Plus } from '@element-plus/icons-vue';
import { cloneDeep } from 'lodash-es';
import { TMagicButton } from '@tmagic/design';
import { TMagicButton, tMagicMessage } from '@tmagic/design';
import type { ContainerChangeEventData, FormState, GroupListConfig } from '../schema';
import { initValue } from '../utils/form';
@ -102,6 +102,20 @@ const changeHandler = (v: any, eventData: ContainerChangeEventData) => {
const addHandler = async () => {
if (!props.name) return false;
if (props.config.max && props.model[props.name].length >= props.config.max) {
tMagicMessage.error(`最多新增配置不能超过${props.config.max}`);
return;
}
if (typeof props.config.beforeAddRow === 'function') {
const beforeCheckRes = await props.config.beforeAddRow(mForm, {
model: props.model[props.name],
formValue: mForm?.values,
prop: props.prop,
});
if (!beforeCheckRes) return;
}
let initValues = {};
if (typeof props.config.defaultAdd === 'function') {

View File

@ -37,7 +37,7 @@ export const useAdd = (
}
if (typeof props.config.beforeAddRow === 'function') {
const beforeCheckRes = props.config.beforeAddRow(mForm, {
const beforeCheckRes = await props.config.beforeAddRow(mForm, {
model: props.model[modelName],
formValue: mForm?.values,
prop: props.prop,