From d37e2f599c69a877c9da9dbc112f598d6532f6c8 Mon Sep 17 00:00:00 2001 From: Vben Date: Sat, 5 Oct 2024 17:09:42 +0800 Subject: [PATCH] fix: naive ui form reset does not meet expectations (#4569) * fix: naive ui form reset does not meet expectations * fix: typo --- apps/web-antd/src/api/core/auth.ts | 4 ++-- apps/web-ele/src/api/core/auth.ts | 4 ++-- apps/web-naive/src/adapter/form.ts | 3 +++ apps/web-naive/src/api/core/auth.ts | 4 ++-- packages/@core/ui-kit/form-ui/src/config.ts | 9 +++++++-- .../@core/ui-kit/form-ui/src/form-render/form-field.vue | 7 ++++--- packages/@core/ui-kit/form-ui/src/form-render/form.vue | 2 ++ packages/@core/ui-kit/form-ui/src/types.ts | 5 +++++ .../effects/common-ui/src/ui/authentication/types.ts | 5 +---- packages/effects/plugins/src/vxe-table/use-vxe-grid.vue | 4 ++-- packages/stores/src/modules/user.ts | 1 + packages/utils/src/index.ts | 1 + playground/src/api/core/auth.ts | 4 ++-- 13 files changed, 34 insertions(+), 19 deletions(-) diff --git a/apps/web-antd/src/api/core/auth.ts b/apps/web-antd/src/api/core/auth.ts index 53b8366d..71d9f994 100644 --- a/apps/web-antd/src/api/core/auth.ts +++ b/apps/web-antd/src/api/core/auth.ts @@ -3,8 +3,8 @@ import { baseRequestClient, requestClient } from '#/api/request'; export namespace AuthApi { /** 登录接口参数 */ export interface LoginParams { - password: string; - username: string; + password?: string; + username?: string; } /** 登录接口返回值 */ diff --git a/apps/web-ele/src/api/core/auth.ts b/apps/web-ele/src/api/core/auth.ts index 53b8366d..71d9f994 100644 --- a/apps/web-ele/src/api/core/auth.ts +++ b/apps/web-ele/src/api/core/auth.ts @@ -3,8 +3,8 @@ import { baseRequestClient, requestClient } from '#/api/request'; export namespace AuthApi { /** 登录接口参数 */ export interface LoginParams { - password: string; - username: string; + password?: string; + username?: string; } /** 登录接口返回值 */ diff --git a/apps/web-naive/src/adapter/form.ts b/apps/web-naive/src/adapter/form.ts index 1c051b4a..eb765476 100644 --- a/apps/web-naive/src/adapter/form.ts +++ b/apps/web-naive/src/adapter/form.ts @@ -84,7 +84,10 @@ setupVbenForm({ Upload: NUpload, }, config: { + // naive-ui组件不接受onChang事件,所以需要禁用 disabledOnChangeListener: true, + // naive-ui组件的空值为null,不能是undefined,否则重置表单时不生效 + emptyStateValue: null, baseModelPropName: 'value', modelPropNameMap: { Checkbox: 'checked', diff --git a/apps/web-naive/src/api/core/auth.ts b/apps/web-naive/src/api/core/auth.ts index 53b8366d..71d9f994 100644 --- a/apps/web-naive/src/api/core/auth.ts +++ b/apps/web-naive/src/api/core/auth.ts @@ -3,8 +3,8 @@ import { baseRequestClient, requestClient } from '#/api/request'; export namespace AuthApi { /** 登录接口参数 */ export interface LoginParams { - password: string; - username: string; + password?: string; + username?: string; } /** 登录接口返回值 */ diff --git a/packages/@core/ui-kit/form-ui/src/config.ts b/packages/@core/ui-kit/form-ui/src/config.ts index 7c118634..33b11317 100644 --- a/packages/@core/ui-kit/form-ui/src/config.ts +++ b/packages/@core/ui-kit/form-ui/src/config.ts @@ -43,8 +43,13 @@ export function setupVbenForm< >(options: VbenFormAdapterOptions) { const { components, config, defineRules } = options; - DEFAULT_FORM_COMMON_CONFIG.disabledOnChangeListener = - config?.disabledOnChangeListener ?? false; + const { disabledOnChangeListener = false, emptyStateValue = undefined } = + (config || {}) as FormCommonConfig; + + Object.assign(DEFAULT_FORM_COMMON_CONFIG, { + disabledOnChangeListener, + emptyStateValue, + }); if (defineRules) { for (const key of Object.keys(defineRules)) { diff --git a/packages/@core/ui-kit/form-ui/src/form-render/form-field.vue b/packages/@core/ui-kit/form-ui/src/form-render/form-field.vue index c116d49c..f66a4bb6 100644 --- a/packages/@core/ui-kit/form-ui/src/form-render/form-field.vue +++ b/packages/@core/ui-kit/form-ui/src/form-render/form-field.vue @@ -33,6 +33,7 @@ const { description, disabled, disabledOnChangeListener, + emptyStateValue, fieldName, formFieldProps, label, @@ -55,7 +56,7 @@ const formApi = formRenderProps.form; const isInValid = computed(() => errors.value?.length > 0); -const fieldComponent = computed(() => { +const FieldComponent = computed(() => { const finalComponent = isString(component) ? componentMap.value[component] : component; @@ -213,7 +214,7 @@ function fieldBindEvent(slotProps: Record) { if (bindEventField) { return { [`onUpdate:${bindEventField}`]: handler, - [bindEventField]: value, + [bindEventField]: value === undefined ? emptyStateValue : value, onChange: disabledOnChangeListener ? undefined : (e: Record) => { @@ -300,7 +301,7 @@ function autofocus() { }" >