Components now deregister themselves when removed
This commit is contained in:
parent
0300519b41
commit
c1e4b3948c
16
dist/index.js
vendored
16
dist/index.js
vendored
File diff suppressed because one or more lines are too long
@ -102,6 +102,12 @@ export default {
|
|||||||
this.$store.commit(`${this.m}setFieldMeta`, {form: this.name, field, data})
|
this.$store.commit(`${this.m}setFieldMeta`, {form: this.name, field, data})
|
||||||
this.updateFormValidation()
|
this.updateFormValidation()
|
||||||
},
|
},
|
||||||
|
deregisterField (field) {
|
||||||
|
this.$store.commit(`${this.m}removeField`, {
|
||||||
|
form: this.name,
|
||||||
|
field
|
||||||
|
})
|
||||||
|
},
|
||||||
hydrate (values) {
|
hydrate (values) {
|
||||||
for (let field of this.fields) {
|
for (let field of this.fields) {
|
||||||
if (field.type !== 'submit' && typeof this.values[field.name] === 'undefined') {
|
if (field.type !== 'submit' && typeof this.values[field.name] === 'undefined') {
|
||||||
|
@ -308,12 +308,25 @@ export default {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
created () {
|
created () {
|
||||||
this.form.registerField(
|
if (typeof window === 'undefined') {
|
||||||
this.name,
|
this.register()
|
||||||
filter(this.$props, (prop, value) => ['name', 'type', 'id', 'label', 'validation', 'validationLabel'].includes(prop))
|
}
|
||||||
)
|
},
|
||||||
if (this.initial !== false) {
|
mounted () {
|
||||||
this.form.setInitial(this.name, this.initial)
|
this.register()
|
||||||
|
},
|
||||||
|
beforeDestroy () {
|
||||||
|
this.form.deregisterField(this.name)
|
||||||
|
},
|
||||||
|
methods: {
|
||||||
|
register () {
|
||||||
|
this.form.registerField(
|
||||||
|
this.name,
|
||||||
|
filter(this.$props, (prop, value) => ['name', 'type', 'id', 'label', 'validation', 'validationLabel'].includes(prop))
|
||||||
|
)
|
||||||
|
if (this.initial !== false) {
|
||||||
|
this.form.setInitial(this.name, this.initial)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import {map, reduce} from './utils'
|
import {map, reduce, filter} from './utils'
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Curried function for creating the formState
|
* Curried function for creating the formState
|
||||||
@ -83,6 +83,13 @@ export const formulateMutations = (mutations = {}) => Object.assign({
|
|||||||
[form]: map(state.values[form], (key, value) => undefined)
|
[form]: map(state.values[form], (key, value) => undefined)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
removeField (state, {form, field}) {
|
||||||
|
for (let group in state) {
|
||||||
|
if (state[group][form] && state[group][form].hasOwnProperty(field)) {
|
||||||
|
state[group][form] = filter(state[group][form], (key, value) => key !== field)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}, mutations)
|
}, mutations)
|
||||||
|
|
||||||
|
@ -176,3 +176,22 @@ test('wont mutate undefined form on reset', async t => {
|
|||||||
formulateMutations().resetForm(state, 'register')
|
formulateMutations().resetForm(state, 'register')
|
||||||
t.deepEqual(state.values, {login: {username: 'testuser', password: 'secret'}})
|
t.deepEqual(state.values, {login: {username: 'testuser', password: 'secret'}})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
test('removeField deletes values from store', async t => {
|
||||||
|
let state = {
|
||||||
|
values: {
|
||||||
|
register: {
|
||||||
|
username: 'testuser',
|
||||||
|
password: 'secret'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
formulateMutations().removeField(state, {form: 'register', field: 'password'})
|
||||||
|
t.deepEqual({
|
||||||
|
values: {
|
||||||
|
register: {
|
||||||
|
username: 'testuser'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}, state)
|
||||||
|
})
|
||||||
|
Loading…
x
Reference in New Issue
Block a user