1
0
mirror of synced 2024-11-25 23:06:02 +03:00

Adds the ability to reset a form with new resetForm mutation

This commit is contained in:
Justin Schroeder 2018-03-02 15:14:47 -05:00
parent 9448479ada
commit 2ea9ed4134
3 changed files with 20 additions and 1 deletions

2
dist/index.js vendored

File diff suppressed because one or more lines are too long

View File

@ -76,6 +76,13 @@ export const formulateMutations = (mutations = {}) => Object.assign({
state.meta = Object.assign({}, state.meta, { state.meta = Object.assign({}, state.meta, {
[form]: Object.assign({}, state.meta[form] || {}, {[field]: data}) [form]: Object.assign({}, state.meta[form] || {}, {[field]: data})
}) })
},
resetForm (state, form) {
if (state.values[form]) {
state.values = Object.assign({}, state.values, {
[form]: map(state.values[form], (key, value) => undefined)
})
}
} }
}, mutations) }, mutations)

View File

@ -164,3 +164,15 @@ test('adds a validationError to existing field', async t => {
}) })
t.deepEqual(state.validationErrors, {form: {name: ['i dislike this field']}}) t.deepEqual(state.validationErrors, {form: {name: ['i dislike this field']}})
}) })
test('reset a form', async t => {
let state = {values: {login: {username: 'testuser', password: 'secret'}}}
formulateMutations().resetForm(state, 'login')
t.deepEqual(state.values, {login: {username: undefined, password: undefined}})
})
test('wont mutate undefined form on reset', async t => {
let state = {values: {login: {username: 'testuser', password: 'secret'}}}
formulateMutations().resetForm(state, 'register')
t.deepEqual(state.values, {login: {username: 'testuser', password: 'secret'}})
})