Adds a new values prop to FormulateForm
This commit is contained in:
parent
0312802e03
commit
12326a91fd
2
dist/formulate.esm.js
vendored
2
dist/formulate.esm.js
vendored
File diff suppressed because one or more lines are too long
2
dist/formulate.min.js
vendored
2
dist/formulate.min.js
vendored
File diff suppressed because one or more lines are too long
2
dist/formulate.umd.js
vendored
2
dist/formulate.umd.js
vendored
File diff suppressed because one or more lines are too long
@ -31,6 +31,10 @@ export default {
|
|||||||
formulateValue: {
|
formulateValue: {
|
||||||
type: Object,
|
type: Object,
|
||||||
default: () => ({})
|
default: () => ({})
|
||||||
|
},
|
||||||
|
values: {
|
||||||
|
type: [Object, Boolean],
|
||||||
|
default: false
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
data () {
|
data () {
|
||||||
@ -41,14 +45,33 @@ export default {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
computed: {
|
computed: {
|
||||||
hasFormulateValue () {
|
hasInitialValue () {
|
||||||
return this.formulateValue && typeof this.formulateValue === 'object'
|
return (
|
||||||
|
(this.formulateValue && typeof this.formulateValue === 'object') ||
|
||||||
|
(this.values && typeof this.values === 'object')
|
||||||
|
)
|
||||||
},
|
},
|
||||||
isVmodeled () {
|
isVmodeled () {
|
||||||
return !!(this.$options.propsData.hasOwnProperty('formulateValue') &&
|
return !!(this.$options.propsData.hasOwnProperty('formulateValue') &&
|
||||||
this._events &&
|
this._events &&
|
||||||
Array.isArray(this._events.input) &&
|
Array.isArray(this._events.input) &&
|
||||||
this._events.input.length)
|
this._events.input.length)
|
||||||
|
},
|
||||||
|
initialValues () {
|
||||||
|
if (
|
||||||
|
Object.prototype.hasOwnProperty.call(this.$options.propsData, 'formulateValue') &&
|
||||||
|
typeof this.formulateValue === 'object'
|
||||||
|
) {
|
||||||
|
// If there is a v-model on the form, use those values as first priority
|
||||||
|
return Object.assign({}, this.formulateValue) // @todo - use a deep clone to detach reference types
|
||||||
|
} else if (
|
||||||
|
Object.prototype.hasOwnProperty.call(this.$options.propsData, 'values') &&
|
||||||
|
typeof this.values === 'object'
|
||||||
|
) {
|
||||||
|
// If there are values, use them as secondary priority
|
||||||
|
return Object.assign({}, this.values)
|
||||||
|
}
|
||||||
|
return {}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
watch: {
|
watch: {
|
||||||
@ -74,8 +97,8 @@ export default {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
created () {
|
created () {
|
||||||
if (this.$options.propsData.hasOwnProperty('formulateValue')) {
|
if (this.hasInitialValue) {
|
||||||
this.internalFormModelProxy = Object.assign({}, this.formulateValue)
|
this.internalFormModelProxy = this.initialValues
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
methods: {
|
methods: {
|
||||||
@ -89,15 +112,15 @@ export default {
|
|||||||
const hasValue = Object.prototype.hasOwnProperty.call(component.$options.propsData, 'value')
|
const hasValue = Object.prototype.hasOwnProperty.call(component.$options.propsData, 'value')
|
||||||
if (
|
if (
|
||||||
!hasVModelValue &&
|
!hasVModelValue &&
|
||||||
this.hasFormulateValue &&
|
this.hasInitialValue &&
|
||||||
this.formulateValue[field]
|
this.initialValues[field]
|
||||||
) {
|
) {
|
||||||
// In the case that the form is carrying an initial value and the
|
// In the case that the form is carrying an initial value and the
|
||||||
// element is not, set it directly.
|
// element is not, set it directly.
|
||||||
component.context.model = this.formulateValue[field]
|
component.context.model = this.initialValues[field]
|
||||||
} else if (
|
} else if (
|
||||||
(hasVModelValue || hasValue) &&
|
(hasVModelValue || hasValue) &&
|
||||||
!shallowEqualObjects(component.internalModelProxy, this.formulateValue[field])
|
!shallowEqualObjects(component.internalModelProxy, this.initialValues[field])
|
||||||
) {
|
) {
|
||||||
this.setFieldValue(field, component.internalModelProxy)
|
this.setFieldValue(field, component.internalModelProxy)
|
||||||
}
|
}
|
||||||
|
@ -173,4 +173,15 @@ describe('FormulateForm', () => {
|
|||||||
const submission = await wrapper.vm.formSubmitted()
|
const submission = await wrapper.vm.formSubmitted()
|
||||||
expect(submission).toEqual({testinput: 'Justin'})
|
expect(submission).toEqual({testinput: 'Justin'})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('accepts a values prop and uses it to set the initial values', async () => {
|
||||||
|
const wrapper = mount(FormulateForm, {
|
||||||
|
propsData: { values: { name: 'Dave Barnett', candy: true } },
|
||||||
|
slots: { default: `<FormulateInput type="text" name="name" validation="required" /><FormulateInput type="checkbox" name="candy" />` }
|
||||||
|
})
|
||||||
|
await flushPromises()
|
||||||
|
// expect(wrapper.vm.internalFormModelProxy).toEqual({ name: 'Dave Barnett', candy: true })
|
||||||
|
expect(wrapper.find('input[type="text"]').element.value).toBe('Dave Barnett')
|
||||||
|
expect(wrapper.find('input[type="checkbox"]').element.checked).toBe(true)
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
Loading…
Reference in New Issue
Block a user