fix: naive ui form reset does not meet expectations (#4569)

* fix: naive ui form reset does not meet expectations

* fix: typo
This commit is contained in:
Vben
2024-10-05 17:09:42 +08:00
committed by GitHub
parent 402eaf4275
commit d37e2f599c
13 changed files with 34 additions and 19 deletions

View File

@@ -43,8 +43,13 @@ export function setupVbenForm<
>(options: VbenFormAdapterOptions<T>) {
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)) {

View File

@@ -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<string, any>) {
if (bindEventField) {
return {
[`onUpdate:${bindEventField}`]: handler,
[bindEventField]: value,
[bindEventField]: value === undefined ? emptyStateValue : value,
onChange: disabledOnChangeListener
? undefined
: (e: Record<string, any>) => {
@@ -300,7 +301,7 @@ function autofocus() {
}"
>
<component
:is="fieldComponent"
:is="FieldComponent"
ref="fieldComponentRef"
:class="{
'border-destructive focus:border-destructive hover:border-destructive/80 focus:shadow-[0_0_0_2px_rgba(255,38,5,0.06)]':

View File

@@ -87,6 +87,7 @@ const computedSchema = computed(
controlClass = '',
disabled,
disabledOnChangeListener = false,
emptyStateValue = undefined,
formFieldProps = {},
formItemClass = '',
hideLabel = false,
@@ -107,6 +108,7 @@ const computedSchema = computed(
return {
disabled,
disabledOnChangeListener,
emptyStateValue,
hideLabel,
hideRequiredMark,
labelWidth,

View File

@@ -153,6 +153,10 @@ export interface FormCommonConfig {
* @default false
*/
disabledOnChangeListener?: boolean;
/**
* 所有表单项的空状态值,默认都是undefinednaive-ui的空状态值是null
*/
emptyStateValue?: null | undefined;
/**
* 所有表单项的控件样式
* @default {}
@@ -341,6 +345,7 @@ export interface VbenFormAdapterOptions<
config?: {
baseModelPropName?: string;
disabledOnChangeListener?: boolean;
emptyStateValue?: null | undefined;
modelPropNameMap?: Partial<Record<T, string>>;
};
defineRules?: {