Skip to content

Commit

Permalink
feat(core): support auto clean field value with visible false (#3452)
Browse files Browse the repository at this point in the history
  • Loading branch information
janryWang authored Oct 13, 2022
1 parent 9809637 commit 2c9b332
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 9 deletions.
4 changes: 2 additions & 2 deletions packages/core/src/__tests__/field.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -861,7 +861,7 @@ test('setState/getState', () => {
})
)
expect(aa.value).toEqual('123')
expect(bb.value).toEqual('123')
expect(bb.value).toBeUndefined()
expect(cc.value).toEqual('123')
form.setFieldState(form.query('cc'), (state) => {
state.value = 'ccc'
Expand Down Expand Up @@ -1093,7 +1093,7 @@ test('fault tolerance', () => {
field.setDisplay('none')
expect(field.value).toBeUndefined()
field.setValue(321)
expect(field.value).toEqual(321)
expect(field.value).toBeUndefined()
field.setDisplay('visible')
expect(field.value).toEqual(321)
form.setDisplay(null)
Expand Down
72 changes: 70 additions & 2 deletions packages/core/src/__tests__/form.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -440,8 +440,8 @@ test('setState/getState/setFormState/getFormState/setFieldState/getFieldState',
name: 'kk',
})
)
expect(oo.value).toEqual(123)
expect(kk.value).toEqual(321)
expect(oo.value).toBeUndefined()
expect(kk.value).toBeUndefined()
})

test('validate/valid/invalid/errors/warnings/successes/clearErrors/clearWarnings/clearSuccesses/queryFeedbacks', async () => {
Expand Down Expand Up @@ -1615,3 +1615,71 @@ test('form clearFormGraph not clear field values', () => {
form.clearFormGraph('*', false)
expect(form.values.aa).toEqual('123')
})

// test('form values auto clean with visible false', () => {
// const form = attach(
// createForm({
// initialValues: {
// aa: '123',
// bb: '321',
// cc: 'cc',
// },
// })
// )
// attach(
// form.createField({
// name: 'aa',
// })
// )
// attach(
// form.createField({
// name: 'bb',
// reactions: (field) => {
// field.visible = form.values.aa === '1233'
// },
// })
// )
// attach(
// form.createField({
// name: 'cc',
// })
// )

// expect(form.values).toEqual({
// aa: '123',
// cc: 'cc',
// })
// })

// test('form values auto clean with visible false in async setInitialValues', () => {
// const form = attach(createForm())
// attach(
// form.createField({
// name: 'aa',
// })
// )
// attach(
// form.createField({
// name: 'bb',
// reactions: (field) => {
// field.visible = form.values.aa === '1233'
// },
// })
// )
// attach(
// form.createField({
// name: 'cc',
// })
// )

// form.setInitialValues({
// aa: '123',
// bb: '321',
// cc: 'cc',
// })

// expect(form.values).toEqual({
// aa: '123',
// cc: 'cc',
// })
// })
10 changes: 8 additions & 2 deletions packages/core/src/models/Field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,8 +224,14 @@ export class Field<
() => this.value,
(value) => {
this.notify(LifeCycleTypes.ON_FIELD_VALUE_CHANGE)
if (isValid(value) && this.selfModified && !this.caches.inputting) {
validateSelf(this)
if (isValid(value)) {
if (this.selfModified && !this.caches.inputting) {
validateSelf(this)
}
if (!this.visible) {
this.caches.value = toJS(value)
this.form.deleteValuesIn(this.path)
}
}
}
),
Expand Down
5 changes: 2 additions & 3 deletions packages/core/src/shared/internals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -986,10 +986,9 @@ export const resetSelf = batch.bound(
if (options?.forceClear) {
target.value = typedDefaultValue
} else {
const initialValue = target.initialValue
target.value = toJS(
!isUndef(target.initialValue)
? target.initialValue
: typedDefaultValue
!isUndef(initialValue) ? initialValue : typedDefaultValue
)
}
}
Expand Down

0 comments on commit 2c9b332

Please sign in to comment.