Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(core): add designable tests #1972

Merged
merged 1 commit into from
Aug 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions packages/core/src/__tests__/form.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
6 changes: 3 additions & 3 deletions packages/core/src/models/Field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -668,10 +668,10 @@ export class Field<

getState: IModelGetter<IFieldState> = modelStateGetter(this)

onInit = () => {
onInit = (designable: boolean) => {
this.initialized = true
batch.scope(() => {
initFieldValue(this)
initFieldValue(this, designable)
})
batch.scope(() => {
initFieldUpdate(this)
Expand Down
81 changes: 45 additions & 36 deletions packages/core/src/shared/internals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
}
Expand Down