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

feat(core): remove property of form values with undefined value #1495

Merged
merged 6 commits into from
May 24, 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
41 changes: 29 additions & 12 deletions packages/core/src/__tests__/form.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ test('validate/valid/invalid/errors/warnings/successes/clearErrors/clearWarnings
)
try {
await form.validate()
} catch {}
} catch { }
expect(form.invalid).toBeTruthy()
expect(form.valid).toBeFalsy()
expect(form.errors).toEqual([
Expand Down Expand Up @@ -494,7 +494,7 @@ test('validate/valid/invalid/errors/warnings/successes/clearErrors/clearWarnings
await bb.onInput('')
try {
await form.validate()
} catch {}
} catch { }
expect(form.errors).toEqual([
{
type: 'error',
Expand Down Expand Up @@ -616,15 +616,15 @@ test('validate/valid/invalid/errors/warnings/successes/clearErrors/clearWarnings
form.clearWarnings()
try {
await form.validate('aa')
} catch {}
} catch { }
expect(
form.queryFeedbacks({
type: 'error',
}).length
).toEqual(1)
try {
await form.validate('*')
} catch {}
} catch { }
expect(
form.queryFeedbacks({
type: 'error',
Expand Down Expand Up @@ -797,7 +797,7 @@ test('reset', async () => {
expect(form.values.aa).toEqual('xxxxx')
try {
await form.reset()
} catch {}
} catch { }
expect(form.valid).toBeTruthy()
expect(form.values.aa).toEqual(123)
expect(field.value).toEqual(123)
Expand All @@ -814,7 +814,7 @@ test('reset', async () => {
await form.reset('*', {
validate: true,
})
} catch {}
} catch { }
expect(form.valid).toBeFalsy()
expect(form.values.aa).toEqual(123)
expect(field.value).toEqual(123)
Expand All @@ -826,7 +826,7 @@ test('reset', async () => {
await form.reset('*', {
forceClear: true,
})
} catch {}
} catch { }
expect(form.valid).toBeTruthy()
expect(form.values.aa).toBeUndefined()
expect(field.value).toBeUndefined()
Expand All @@ -838,7 +838,7 @@ test('reset', async () => {
await form.reset('aa', {
forceClear: true,
})
} catch {}
} catch { }
expect(form.valid).toBeTruthy()
expect(form.values.aa).toBeUndefined()
expect(field.value).toBeUndefined()
Expand All @@ -848,8 +848,8 @@ test('reset', async () => {

test('devtools', () => {
window['__FORMILY_DEV_TOOLS_HOOK__'] = {
inject() {},
unmount() {},
inject() { },
unmount() { },
}
const form = attach(createForm())
form.onUnmount()
Expand Down Expand Up @@ -931,7 +931,7 @@ test('initialValues merge values after create field', () => {
const aa = attach(
form.createArrayField({
name: 'aa',
initialValue:'111'
initialValue: '111'
})
)
const array = attach(
Expand All @@ -951,4 +951,21 @@ test('initialValues merge values after create field', () => {
expect(array.value).toEqual([{ aa: '321' }])
expect(arr_0_aa.value).toEqual('321')
expect(aa.value).toEqual('222')
})
})

test('remove property of form values with undefined value', () => {
const form = attach(createForm())
const field = attach(
form.createField({
name: 'aaa',
initialValue: 123,
})
)
expect(form.values).toMatchObject({ aaa: 123 })
field.display = "none";
expect(form.values.hasOwnProperty("aaa")).toBeFalsy()
field.display = "visible";
expect(form.values.hasOwnProperty("aaa")).toBeTruthy()
field.setValue(undefined);
expect(form.values.hasOwnProperty("aaa")).toBeTruthy()
})
2 changes: 1 addition & 1 deletion packages/core/src/models/Field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ export class Field<
(display) => {
if (display === 'none') {
this.caches.value = toJS(this.value)
this.setValue()
this.form.deleteValuesIn(this.path)
} else if (display === 'visible') {
if (isEmpty(this.value)) {
this.setValue(this.caches.value)
Expand Down