fix(BasicForm->Upload): setValue and defaultValue uncertain && rule about first render (#3900)

* chore: upload component defaulttype should be array

* chore: upload component setFieldsValue  should be array

* chore(upload): split event between change and  update:value

* update

* update
This commit is contained in:
Electrolux 2024-06-06 15:27:15 +08:00 committed by GitHub
parent 1a5692060b
commit cca7f59fac
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 66 additions and 17 deletions

View File

@ -54,7 +54,7 @@
import { useFormValues } from './hooks/useFormValues'; import { useFormValues } from './hooks/useFormValues';
import useAdvanced from './hooks/useAdvanced'; import useAdvanced from './hooks/useAdvanced';
import { useFormEvents } from './hooks/useFormEvents'; import { itemIsUploadComponent, useFormEvents } from './hooks/useFormEvents';
import { createFormContext } from './hooks/useFormContext'; import { createFormContext } from './hooks/useFormContext';
import { useAutoFocus } from './hooks/useAutoFocus'; import { useAutoFocus } from './hooks/useAutoFocus';
import { useModalContext } from '@/components/Modal'; import { useModalContext } from '@/components/Modal';
@ -64,7 +64,7 @@
import { useDesign } from '@/hooks/web/useDesign'; import { useDesign } from '@/hooks/web/useDesign';
import { cloneDeep } from 'lodash-es'; import { cloneDeep } from 'lodash-es';
import { TableActionType } from '@/components/Table'; import { TableActionType } from '@/components/Table';
import { isFunction } from '@/utils/is'; import { isArray, isFunction } from '@/utils/is';
defineOptions({ name: 'BasicForm' }); defineOptions({ name: 'BasicForm' });
@ -165,14 +165,17 @@
schema.defaultValue = def; schema.defaultValue = def;
} }
} }
// handle upload type
if (defaultValue && itemIsUploadComponent(schema?.component)) {
if (isArray(defaultValue)) {
schema.defaultValue = defaultValue;
} else if (typeof defaultValue == 'string') {
schema.defaultValue = [defaultValue];
}
}
// handle schema.valueFormat // handle schema.valueFormat
if ( if (isHandleDefaultValue && defaultValue && component && isFunction(valueFormat)) {
isHandleDefaultValue &&
defaultValue &&
component &&
isFunction(valueFormat)
) {
schema.defaultValue = valueFormat({ schema.defaultValue = valueFormat({
value: defaultValue, value: defaultValue,
schema, schema,

View File

@ -31,6 +31,15 @@ export function createPlaceholderMessage(component: ComponentType) {
const DATE_TYPE = ['DatePicker', 'MonthPicker', 'WeekPicker', 'TimePicker']; const DATE_TYPE = ['DatePicker', 'MonthPicker', 'WeekPicker', 'TimePicker'];
/**
*
*/
export const uploadItemType: ComponentType[] = [
'Upload',
'ImageUpload'
];
function genType() { function genType() {
return [...DATE_TYPE, 'RangePicker',"TimeRangePicker"]; return [...DATE_TYPE, 'RangePicker',"TimeRangePicker"];
} }
@ -45,7 +54,7 @@ export function setComponentRuleType(
} }
if (['DatePicker', 'MonthPicker', 'WeekPicker', 'TimePicker'].includes(component)) { if (['DatePicker', 'MonthPicker', 'WeekPicker', 'TimePicker'].includes(component)) {
rule.type = valueFormat ? 'string' : 'object'; rule.type = valueFormat ? 'string' : 'object';
} else if (['RangePicker', 'Upload', 'CheckboxGroup'].includes(component)) { } else if (['RangePicker', 'CheckboxGroup'].includes(component)) {
rule.type = 'array'; rule.type = 'array';
} else if (['InputNumber'].includes(component)) { } else if (['InputNumber'].includes(component)) {
rule.type = 'number'; rule.type = 'number';

View File

@ -4,10 +4,16 @@ import type { NamePath } from 'ant-design-vue/lib/form/interface';
import { unref, toRaw, nextTick } from 'vue'; import { unref, toRaw, nextTick } from 'vue';
import { isArray, isFunction, isObject, isString, isNil } from '@/utils/is'; import { isArray, isFunction, isObject, isString, isNil } from '@/utils/is';
import { deepMerge } from '@/utils'; import { deepMerge } from '@/utils';
import { dateItemType, defaultValueComponents, isIncludeSimpleComponents } from '../helper'; import {
dateItemType,
defaultValueComponents,
isIncludeSimpleComponents,
uploadItemType,
} from '../helper';
import { dateUtil } from '@/utils/dateUtil'; import { dateUtil } from '@/utils/dateUtil';
import { cloneDeep, has, uniqBy, get, set } from 'lodash-es'; import { cloneDeep, has, uniqBy, get, set } from 'lodash-es';
import { error } from '@/utils/log'; import { error } from '@/utils/log';
import { ComponentProps } from '../types';
interface UseFormActionContext { interface UseFormActionContext {
emit: EmitType; emit: EmitType;
@ -19,7 +25,12 @@ interface UseFormActionContext {
schemaRef: Ref<FormSchema[]>; schemaRef: Ref<FormSchema[]>;
handleFormValues: Fn; handleFormValues: Fn;
} }
/**
* @description: Is it upload
*/
export function itemIsUploadComponent(key: keyof ComponentProps) {
return uploadItemType.includes(key);
}
function tryConstructArray(field: string, values: Recordable = {}): any[] | undefined { function tryConstructArray(field: string, values: Recordable = {}): any[] | undefined {
const pattern = /^\[(.+)\]$/; const pattern = /^\[(.+)\]$/;
if (pattern.test(field)) { if (pattern.test(field)) {
@ -123,7 +134,20 @@ export function useFormEvents({
} }
} }
} }
// Adapt upload component
if (itemIsUploadComponent(schema?.component)) {
constructValue = get(value, key);
const fieldValue = constructValue || value;
if (fieldValue) {
if (isArray(fieldValue)) {
unref(formModel)[key] = fieldValue;
} else if (typeof fieldValue == 'string') {
unref(formModel)[key] = [fieldValue];
}
}
validKeys.push(key);
return
}
// Adapt common component // Adapt common component
if (hasKey) { if (hasKey) {
constructValue = get(value, key); constructValue = get(value, key);

View File

@ -85,6 +85,9 @@
const value = { ...attrs, ...props }; const value = { ...attrs, ...props };
return omit(value, 'onChange'); return omit(value, 'onChange');
}); });
const isFirstRender = ref<boolean>(true)
function getValue(valueKey="url") { function getValue(valueKey="url") {
const list = (fileList.value || []).map((item: any) => { const list = (fileList.value || []).map((item: any) => {
return item[valueKey]; return item[valueKey];
@ -124,9 +127,15 @@
}) as any; }) as any;
} }
emit('update:value', values); emit('update:value', values);
if(!isFirstRender.value){
emit('change', values); emit('change', values);
isFirstRender.value = false
}
},
{
immediate: true,
deep: true,
}, },
{ immediate: true },
); );
// modal // modal

View File

@ -63,6 +63,7 @@
const fileList = ref<UploadProps['fileList']>([]); const fileList = ref<UploadProps['fileList']>([]);
const isLtMsg = ref<boolean>(true); const isLtMsg = ref<boolean>(true);
const isActMsg = ref<boolean>(true); const isActMsg = ref<boolean>(true);
const isFirstRender = ref<boolean>(true)
watch( watch(
() => props.value, () => props.value,
@ -94,7 +95,10 @@
}) as UploadProps['fileList']; }) as UploadProps['fileList'];
} }
emit('update:value', value); emit('update:value', value);
if(!isFirstRender.value){
emit('change', value); emit('change', value);
isFirstRender.value = false
}
}, },
{ {
immediate: true, immediate: true,