diff --git a/packages/core/src/__tests__/form.spec.ts b/packages/core/src/__tests__/form.spec.ts index f3a1c324a95..e3e1793b346 100644 --- a/packages/core/src/__tests__/form.spec.ts +++ b/packages/core/src/__tests__/form.spec.ts @@ -1167,3 +1167,37 @@ test('exception validate', async () => { expect(form.invalid).toBeTruthy() expect(form.validating).toBeFalsy() }) + +test('designable form', () => { + const form = attach( + createForm({ + designable: true, + }) + ) + attach( + form.createField({ + name: 'bb', + initialValue: 123, + }) + ) + attach( + form.createField({ + name: 'bb', + initialValue: 321, + }) + ) + attach( + form.createField({ + name: 'aa', + value: 123, + }) + ) + attach( + form.createField({ + name: 'aa', + value: 321, + }) + ) + expect(form.values.aa).toEqual(321) + expect(form.initialValues.bb).toEqual(321) +}) diff --git a/packages/core/src/models/Field.ts b/packages/core/src/models/Field.ts index 86219735fb9..31c19784ce9 100644 --- a/packages/core/src/models/Field.ts +++ b/packages/core/src/models/Field.ts @@ -109,7 +109,7 @@ export class Field< this.makeIndexes(address) this.makeObservable(designable) this.makeReactive(designable) - this.onInit() + this.onInit(designable) } protected makeIndexes(address: FormPathPattern) { @@ -668,10 +668,10 @@ export class Field< getState: IModelGetter = modelStateGetter(this) - onInit = () => { + onInit = (designable: boolean) => { this.initialized = true batch.scope(() => { - initFieldValue(this) + initFieldValue(this, designable) }) batch.scope(() => { initFieldUpdate(this) diff --git a/packages/core/src/shared/internals.ts b/packages/core/src/shared/internals.ts index 6359a6f61d1..3ce3cf0f10e 100644 --- a/packages/core/src/shared/internals.ts +++ b/packages/core/src/shared/internals.ts @@ -411,47 +411,56 @@ export const cleanupObjectChildren = (field: ObjectField, keys: string[]) => { }) } -export const initFieldValue = (field: Field) => { +export const initFieldValue = (field: Field, designable: boolean) => { GlobalState.initializing = true - const fromValue = (value: any) => [ - isValid(value), - isEmpty(value, true), - value, - ] - const [, isEmptySelfValue, selfValue] = fromValue(field.value) - const [, isEmptySelfInitialValue, selfInitialValue] = fromValue( - field.initialValue - ) - const [isValidPropsValue, isEmptyPropsValue, propsValue] = fromValue( - field.props.value - ) - const [ - isValidPropsInitialValue, - isEmptyPropsInitialValue, - propsInitialValue, - ] = fromValue(field.props.initialValue) - if (isEmptySelfInitialValue) { - if (isEmptyPropsInitialValue) { - if (!isEqual(selfInitialValue, propsInitialValue)) { - field.initialValue = shallowClone(propsInitialValue) - } - } else if (isValidPropsInitialValue) { - field.initialValue = shallowClone(propsInitialValue) + if (designable) { + if (isValid(field.props.initialValue)) { + field.initialValue = shallowClone(field.props.initialValue) } - } - if (isEmptySelfValue) { - if (!isEmptyPropsValue) { - field.value = shallowClone(propsValue) - } else { - if (!isEmptyPropsInitialValue) { - field.value = shallowClone(propsInitialValue) - } else if (isValidPropsValue) { - if (!isEqual(selfValue, propsValue)) { - field.value = shallowClone(propsValue) + if (isValid(field.props.value)) { + field.value = field.props.value + } + } else { + const fromValue = (value: any) => [ + isValid(value), + isEmpty(value, true), + value, + ] + const [, isEmptySelfValue, selfValue] = fromValue(field.value) + const [, isEmptySelfInitialValue, selfInitialValue] = fromValue( + field.initialValue + ) + const [isValidPropsValue, isEmptyPropsValue, propsValue] = fromValue( + field.props.value + ) + const [ + isValidPropsInitialValue, + isEmptyPropsInitialValue, + propsInitialValue, + ] = fromValue(field.props.initialValue) + if (isEmptySelfInitialValue) { + if (isEmptyPropsInitialValue) { + if (!isEqual(selfInitialValue, propsInitialValue)) { + field.initialValue = shallowClone(propsInitialValue) } } else if (isValidPropsInitialValue) { - if (!isEqual(selfValue, propsInitialValue)) { + field.initialValue = shallowClone(propsInitialValue) + } + } + if (isEmptySelfValue) { + if (!isEmptyPropsValue) { + field.value = shallowClone(propsValue) + } else { + if (!isEmptyPropsInitialValue) { field.value = shallowClone(propsInitialValue) + } else if (isValidPropsValue) { + if (!isEqual(selfValue, propsValue)) { + field.value = shallowClone(propsValue) + } + } else if (isValidPropsInitialValue) { + if (!isEqual(selfValue, propsInitialValue)) { + field.value = shallowClone(propsInitialValue) + } } } }