From c64be886b4e6b964a86f826b255963ede6e5e163 Mon Sep 17 00:00:00 2001 From: nehc <934298133@qq.com> Date: Sat, 2 Aug 2025 23:28:59 +0800 Subject: [PATCH] =?UTF-8?q?feat(@vben/web-antd):=20vxe-table)=E6=96=B0?= =?UTF-8?q?=E5=A2=9E=E8=A1=A8=E5=8D=95=E9=AA=8C=E8=AF=81=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 在 vxe-table.ts 中添加 createRequiredValidation函数 - 在 data.ts 中使用 createRequiredValidation 替代原有的 className 函数 - 在 vxe-table 插件中添加 validation 相关的工具函数 - 优化表格列的验证逻辑,提高代码复用性和可维护性 --- apps/web-antd/src/adapter/vxe-table.ts | 3 +- apps/web-antd/src/views/erp/stock/in/data.ts | 19 ++---- .../effects/plugins/src/vxe-table/index.ts | 3 +- .../plugins/src/vxe-table/validation.ts | 61 +++++++++++++++++++ 4 files changed, 69 insertions(+), 17 deletions(-) create mode 100644 packages/effects/plugins/src/vxe-table/validation.ts diff --git a/apps/web-antd/src/adapter/vxe-table.ts b/apps/web-antd/src/adapter/vxe-table.ts index 69f855503..e28a6ae1d 100644 --- a/apps/web-antd/src/adapter/vxe-table.ts +++ b/apps/web-antd/src/adapter/vxe-table.ts @@ -7,6 +7,7 @@ import { IconifyIcon } from '@vben/icons'; import { $te } from '@vben/locales'; import { AsyncComponents, + createRequiredValidation, setupVbenVxeTable, useVbenVxeGrid, } from '@vben/plugins/vxe-table'; @@ -354,7 +355,7 @@ setupVbenVxeTable({ useVbenForm, }); -export { useVbenVxeGrid }; +export { createRequiredValidation, useVbenVxeGrid }; const [VxeTable, VxeColumn, VxeToolbar] = AsyncComponents; export { VxeColumn, VxeTable, VxeToolbar }; diff --git a/apps/web-antd/src/views/erp/stock/in/data.ts b/apps/web-antd/src/views/erp/stock/in/data.ts index ad981befd..68b667fc9 100644 --- a/apps/web-antd/src/views/erp/stock/in/data.ts +++ b/apps/web-antd/src/views/erp/stock/in/data.ts @@ -1,6 +1,7 @@ import type { VbenFormSchema } from '#/adapter/form'; import type { VxeTableGridOptions } from '#/adapter/vxe-table'; +import { createRequiredValidation } from '#/adapter/vxe-table'; import { getSupplierSimpleList } from '#/api/erp/purchase/supplier'; import { getSimpleUserList } from '#/api/system/user'; import { DICT_TYPE, getDictOptions } from '#/utils'; @@ -108,22 +109,14 @@ export function useStockInItemTableColumns( title: '仓库名称', minWidth: 150, slots: { default: 'warehouseId' }, - className: ({ row }: { row: any }) => { - return isValidating?.value && !row.warehouseId - ? 'required-field-error' - : ''; - }, + className: createRequiredValidation(isValidating, 'warehouseId'), }, { field: 'productId', title: '产品名称', minWidth: 200, slots: { default: 'productId' }, - className: ({ row }: { row: any }) => { - return isValidating?.value && !row.productId - ? 'required-field-error' - : ''; - }, + className: createRequiredValidation(isValidating, 'productId'), }, { field: 'stockCount', @@ -145,11 +138,7 @@ export function useStockInItemTableColumns( title: '数量', minWidth: 120, slots: { default: 'count' }, - className: ({ row }: { row: any }) => { - return isValidating?.value && (!row.count || row.count <= 0) - ? 'required-field-error' - : ''; - }, + className: createRequiredValidation(isValidating, 'count'), }, { field: 'productPrice', diff --git a/packages/effects/plugins/src/vxe-table/index.ts b/packages/effects/plugins/src/vxe-table/index.ts index 70f4cfa5c..8cc12bef7 100644 --- a/packages/effects/plugins/src/vxe-table/index.ts +++ b/packages/effects/plugins/src/vxe-table/index.ts @@ -1,8 +1,9 @@ export { AsyncComponents, setupVbenVxeTable } from './init'; export type { VxeTableGridOptions } from './types'; export * from './use-vxe-grid'; - export { default as VbenVxeGrid } from './use-vxe-grid.vue'; + +export * from './validation'; export type { VxeGridListeners, VxeGridProps, diff --git a/packages/effects/plugins/src/vxe-table/validation.ts b/packages/effects/plugins/src/vxe-table/validation.ts new file mode 100644 index 000000000..487f24107 --- /dev/null +++ b/packages/effects/plugins/src/vxe-table/validation.ts @@ -0,0 +1,61 @@ +/** + * 创建验证类名的工具函数 + * @param isValidating 验证状态 + * @param fieldName 字段名 + * @param validationRules 验证规则,可以是字符串或自定义函数 + * @returns 返回 className 函数 + */ +function createValidationClassName( + isValidating: any, + fieldName: string, + validationRules: ((row: any) => boolean) | string, +) { + return ({ row }: { row: any }) => { + if (!isValidating?.value) return ''; + + let isValid = true; + if (typeof validationRules === 'string') { + // 处理简单的验证规则 + if (validationRules === 'required') { + isValid = + fieldName === 'count' + ? row[fieldName] && row[fieldName] > 0 + : !!row[fieldName]; + } + } else if (typeof validationRules === 'function') { + // 处理自定义验证函数 + isValid = validationRules(row); + } + + return isValid ? '' : 'required-field-error'; + }; +} + +/** + * 创建必填字段验证 + * @param isValidating 验证状态 + * @param fieldName 字段名 + * @returns 返回 className 函数 + */ +function createRequiredValidation(isValidating: any, fieldName: string) { + return createValidationClassName(isValidating, fieldName, 'required'); +} + +/** + * 创建自定义验证 + * @param isValidating 验证状态 + * @param validationFn 自定义验证函数 + * @returns 返回 className 函数 + */ +function createCustomValidation( + isValidating: any, + validationFn: (row: any) => boolean, +) { + return createValidationClassName(isValidating, '', validationFn); +} + +export { + createCustomValidation, + createRequiredValidation, + createValidationClassName, +};