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

Fixes issue #55, removing form data on deregister

This commit is contained in:
Justin Schroeder 2020-05-14 16:45:29 -04:00
parent fdcf7b3dbf
commit af5e23098d
6 changed files with 36 additions and 33 deletions

View File

@ -104,7 +104,7 @@ export default {
for (const field in newValue) { for (const field in newValue) {
if (this.registry.has(field) && if (this.registry.has(field) &&
!shallowEqualObjects(newValue[field], this.proxy[field]) && !shallowEqualObjects(newValue[field], this.proxy[field]) &&
!shallowEqualObjects(newValue[field], this.registry.get(field).internalModelProxy[field]) !shallowEqualObjects(newValue[field], this.registry.get(field).proxy[field])
) { ) {
this.setFieldValue(field, newValue[field]) this.setFieldValue(field, newValue[field])
this.registry.get(field).context.model = newValue[field] this.registry.get(field).context.model = newValue[field]

View File

@ -240,7 +240,7 @@ export default {
defaultId: this.$formulate.nextId(this), defaultId: this.$formulate.nextId(this),
localAttributes: {}, localAttributes: {},
localErrors: [], localErrors: [],
internalModelProxy: this.getInitialValue(), proxy: this.getInitialValue(),
behavioralErrorVisibility: (this.errorBehavior === 'live'), behavioralErrorVisibility: (this.errorBehavior === 'live'),
formShouldShowErrors: false, formShouldShowErrors: false,
validationErrors: [], validationErrors: [],
@ -284,7 +284,7 @@ export default {
}, },
deep: true deep: true
}, },
internalModelProxy (newValue, oldValue) { proxy (newValue, oldValue) {
this.performValidation() this.performValidation()
if (!this.isVmodeled && !shallowEqualObjects(newValue, oldValue)) { if (!this.isVmodeled && !shallowEqualObjects(newValue, oldValue)) {
this.context.model = newValue this.context.model = newValue
@ -339,11 +339,11 @@ export default {
// This should only be run immediately on created and ensures that the // This should only be run immediately on created and ensures that the
// proxy and the model are both the same before any additional registration. // proxy and the model are both the same before any additional registration.
if ( if (
!shallowEqualObjects(this.context.model, this.internalModelProxy) && !shallowEqualObjects(this.context.model, this.proxy) &&
// we dont' want to set the model if we are a sub-box of a multi-box field // we dont' want to set the model if we are a sub-box of a multi-box field
(Object.prototype.hasOwnProperty(this.$options.propsData, 'options') && this.classification === 'box') (Object.prototype.hasOwnProperty(this.$options.propsData, 'options') && this.classification === 'box')
) { ) {
this.context.model = this.internalModelProxy this.context.model = this.proxy
} }
}, },
updateLocalAttributes (value) { updateLocalAttributes (value) {

View File

@ -316,7 +316,7 @@ function defineModel (context) {
* Get the value from a model. * Get the value from a model.
**/ **/
function modelGetter () { function modelGetter () {
const model = this.isVmodeled ? 'formulateValue' : 'internalModelProxy' const model = this.isVmodeled ? 'formulateValue' : 'proxy'
if (this.type === 'checkbox' && !Array.isArray(this[model]) && this.options) { if (this.type === 'checkbox' && !Array.isArray(this[model]) && this.options) {
return [] return []
} }
@ -330,8 +330,8 @@ function modelGetter () {
* Set the value from a model. * Set the value from a model.
**/ **/
function modelSetter (value) { function modelSetter (value) {
if (!shallowEqualObjects(value, this.internalModelProxy)) { if (!shallowEqualObjects(value, this.proxy)) {
this.internalModelProxy = value this.proxy = value
} }
this.$emit('input', value) this.$emit('input', value)
if (this.context.name && typeof this.formulateSetter === 'function') { if (this.context.name && typeof this.formulateSetter === 'function') {

View File

@ -30,6 +30,8 @@ class Registry {
*/ */
remove (name) { remove (name) {
this.registry.delete(name) this.registry.delete(name)
const { [name]: value, ...newProxy } = this.ctx.proxy
this.ctx.proxy = newProxy
return this return this
} }
@ -88,11 +90,11 @@ class Registry {
component.context.model = this.ctx.initialValues[field] component.context.model = this.ctx.initialValues[field]
} else if ( } else if (
(hasVModelValue || hasValue) && (hasVModelValue || hasValue) &&
!shallowEqualObjects(component.internalModelProxy, this.ctx.initialValues[field]) !shallowEqualObjects(component.proxy, this.ctx.initialValues[field])
) { ) {
// In this case, the field is v-modeled or has an initial value and the // In this case, the field is v-modeled or has an initial value and the
// form has no value or a different value, so use the field value // form has no value or a different value, so use the field value
this.ctx.setFieldValue(field, component.internalModelProxy) this.ctx.setFieldValue(field, component.proxy)
} }
if (this.childrenShouldShowErrors) { if (this.childrenShouldShowErrors) {
component.formShouldShowErrors = true component.formShouldShowErrors = true
@ -118,7 +120,7 @@ class Registry {
proxy: {}, proxy: {},
registry: this, registry: this,
register: this.register.bind(this), register: this.register.bind(this),
deregister: field => this.registry.delete(field), deregister: field => this.remove(field),
childrenShouldShowErrors: false childrenShouldShowErrors: false
} }
} }

View File

@ -514,25 +514,26 @@ describe('FormulateForm', () => {
}) })
}) })
// it('removes field data when that field is de-registered', async () => { it('removes field data when that field is de-registered', async () => {
// const wrapper = mount({ const wrapper = mount({
// template: ` template: `
// <FormulateForm <FormulateForm
// v-model="formData" v-model="formData"
// > >
// <FormulateInput type="text" name="foo" value="abc123" /> <FormulateInput type="text" name="foo" value="abc123" />
// <FormulateInput type="checkbox" name="bar" v-if="formData.foo !== 'bar'" :value="true" /> <FormulateInput type="checkbox" name="bar" v-if="formData.foo !== 'bar'" :value="1" />
// </FormulateForm> </FormulateForm>
// `, `,
// data () { data () {
// return { return {
// formData: {} formData: {}
// } }
// } }
// }) })
// await flushPromises() await flushPromises()
// wrapper.find('input[type="text"]').setValue('bar') wrapper.find('input[type="text"]').setValue('bar')
// await flushPromises() await flushPromises()
// expect(wrapper.vm.formData).toEqual({ bar: true }) expect(wrapper.findComponent(FormulateForm).vm.proxy).toEqual({ foo: 'bar' })
// }) expect(wrapper.vm.formData).toEqual({ foo: 'bar' })
})
}) })

View File

@ -180,7 +180,7 @@ describe('FormulateInputText', () => {
const wrapper = mount(FormulateInput, { propsData: { type: 'textarea' } }) const wrapper = mount(FormulateInput, { propsData: { type: 'textarea' } })
const input = wrapper.find('textarea') const input = wrapper.find('textarea')
input.setValue('changed value') input.setValue('changed value')
expect(wrapper.vm.internalModelProxy).toBe('changed value') expect(wrapper.vm.proxy).toBe('changed value')
}) })