Skip to content

Commit

Permalink
fix(core): fix nest visible defualt value is not work (#2772)
Browse files Browse the repository at this point in the history
  • Loading branch information
janryWang authored Jan 19, 2022
1 parent 36143ef commit 72f6560
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 2 deletions.
58 changes: 57 additions & 1 deletion packages/core/src/__tests__/field.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { autorun, batch } from '@formily/reactive'
import { createForm } from '../'
import { createForm, onFieldReact, isField } from '../'
import { DataField } from '../types'
import { attach, sleep } from './shared'

Expand Down Expand Up @@ -1879,3 +1879,59 @@ test('object field reset', async () => {
})
expect(input.value).toBe('123')
})

test('field visible default value should work', () => {
const form = attach(
createForm({
effects(form) {
onFieldReact('obj.input1', (field) => {
field.pattern = 'disabled'
})
onFieldReact('obj', (field) => {
field.visible = form.values.select !== 'none'
})
onFieldReact('obj.input1', (field) => {
if (isField(field)) {
field.initialValue = '123'
}
})
onFieldReact('obj.input2', (field) => {
if (isField(field)) {
field.value = form.values.select
}
})
},
})
)

const select = attach(
form.createField({
name: 'select',
})
)

attach(
form.createObjectField({
name: 'obj',
})
)

attach(
form.createField({
name: 'input1',
basePath: 'obj',
})
)

attach(
form.createField({
name: 'input2',
basePath: 'obj',
})
)

select.value = 'none'
expect(form.values.obj?.input1).toBeUndefined()
select.value = 'visible'
expect(form.values.obj.input1).toBe('123')
})
2 changes: 1 addition & 1 deletion packages/core/src/models/Field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ export class Field<
this.caches.value = undefined
}
} else {
this.caches.value = toJS(value)
this.caches.value = toJS(value) ?? toJS(this.initialValue)
if (display === 'none') {
this.form.deleteValuesIn(this.path)
}
Expand Down

0 comments on commit 72f6560

Please sign in to comment.