Skip to content

Commit f06e76d

Browse files
Fixes issue wearebraid#138
1 parent cb42ffb commit f06e76d

File tree

7 files changed

+42
-4
lines changed

7 files changed

+42
-4
lines changed

src/FormulateInputMixin.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export default {
1616
return this.context.attributes || {}
1717
},
1818
hasValue () {
19-
return !!this.context.model
19+
return this.context.hasValue
2020
}
2121
}
2222
}

src/libs/context.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -462,11 +462,15 @@ function modelGetter () {
462462
* Set the value from a model.
463463
**/
464464
function modelSetter (value) {
465+
let didUpdate = false
465466
if (!shallowEqualObjects(value, this.proxy)) {
466467
this.proxy = value
467-
this.$emit('input', value)
468+
didUpdate = true
468469
}
469470
if (this.context.name && typeof this.formulateSetter === 'function') {
470471
this.formulateSetter(this.context.name, value)
471472
}
473+
if (didUpdate) {
474+
this.$emit('input', value)
475+
}
472476
}

src/libs/registry.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { shallowEqualObjects, has } from './utils'
1+
import { shallowEqualObjects, has, isEmpty } from './utils'
22

33
/**
44
* Component registry with inherent depth to handle complex nesting. This is
@@ -85,7 +85,7 @@ class Registry {
8585
if (
8686
!hasVModelValue &&
8787
this.ctx.hasInitialValue &&
88-
this.ctx.initialValues[field]
88+
!isEmpty(this.ctx.initialValues[field])
8989
) {
9090
// In the case that the form is carrying an initial value and the
9191
// element is not, set it directly.
@@ -191,6 +191,7 @@ export function useRegistryMethods (without = []) {
191191
},
192192
setFieldValue (field, value) {
193193
if (value === undefined) {
194+
// undefined values should be removed from the form model
194195
const { [field]: value, ...proxy } = this.proxy
195196
this.proxy = proxy
196197
} else {

src/libs/utils.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,9 @@ export function setId (o, id) {
302302
* @param {any} value
303303
*/
304304
export function isEmpty (value) {
305+
if (typeof value === 'number') {
306+
return false
307+
}
305308
return (
306309
value === undefined ||
307310
value === '' ||

test/unit/FormulateInput.test.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -743,4 +743,10 @@ describe('FormulateInput', () => {
743743
expect(wrapper.find('.formulate-input-help').exists()).toBe(false)
744744
resetInstance()
745745
})
746+
747+
it('allows an empty string as a validation prop', async () => {
748+
const wrapper = mount(FormulateInput, { propsData: { validation: '', errorBehavior: 'live' }})
749+
await flushPromises()
750+
expect(wrapper.find('.formulate-errors').exists()).toBe(false)
751+
})
746752
})

test/unit/FormulateInputSlider.test.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import Vue from 'vue'
2+
import flushPromises from 'flush-promises'
23
import { mount } from '@vue/test-utils'
34
import Formulate from '@/Formulate.js'
45
import FormulateInput from '@/FormulateInput.vue'
@@ -62,4 +63,26 @@ describe('FormulateInputSlider', () => {
6263
})
6364
expect(wrapper.find('.formulate-input-element-range-value.custom-class').exists()).toBe(true)
6465
})
66+
67+
it('allows a numeric zero value but still hasValue', async () => {
68+
const wrapper = mount({
69+
template: `
70+
<FormulateForm
71+
v-model="model"
72+
>
73+
<FormulateInput type="slider" name="range" />
74+
</FormulateForm>
75+
`,
76+
data () {
77+
return {
78+
model: {
79+
range: 0
80+
}
81+
}
82+
}
83+
})
84+
await flushPromises();
85+
const input = wrapper.findComponent(FormulateInput)
86+
expect(input.vm.context.hasValue).toBe(true)
87+
})
6588
})

test/unit/utils.test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,7 @@ describe('groupBails', () => {
281281

282282
it('is false when string', () => expect(isEmpty('pizza')).toBe(false))
283283
it('is false when zero string', () => expect(isEmpty('0')).toBe(false))
284+
it('is false when zero value', () => expect(isEmpty(0)).toBe(false))
284285
it('is false when has array values', () => expect(isEmpty(['first'])).toBe(false))
285286
it('is false when has object has values', () => expect(isEmpty([{ key: 'value' }])).toBe(false))
286287
})

0 commit comments

Comments
 (0)