Skip to content

Commit 3b41e16

Browse files
Fixes a bug that can cause checkbox groups to reset themselves when changing their options
1 parent 8369371 commit 3b41e16

File tree

7 files changed

+79
-4
lines changed

7 files changed

+79
-4
lines changed

dist/formulate.esm.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/formulate.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/formulate.umd.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/FormulateInput.vue

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,10 @@ export default {
276276
debounce: {
277277
type: [Boolean, Number],
278278
default: false
279+
},
280+
preventDeregister: {
281+
type: Boolean,
282+
default: false
279283
}
280284
},
281285
data () {
@@ -405,7 +409,7 @@ export default {
405409
this.removeErrorObserver(this.setGroupErrors)
406410
}
407411
}
408-
if (typeof this.formulateDeregister === 'function') {
412+
if (typeof this.formulateDeregister === 'function' && !this.preventDeregister) {
409413
this.formulateDeregister(this.nameOrFallback)
410414
}
411415
},

src/inputs/FormulateInputGroup.vue

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
v-model="context.model"
2525
v-bind="optionContext"
2626
:disable-errors="true"
27+
:prevent-deregister="true"
2728
class="formulate-input-group-item"
2829
@blur="context.blurHandler"
2930
/>

test/unit/FormulateForm.test.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1229,4 +1229,39 @@ describe('FormulateForm', () => {
12291229
await flushPromises()
12301230
expect(wrapper.find('[data-output]').text()).toBe('4567')
12311231
})
1232+
1233+
it('Can keep-model-data on box type inputs', async () => {
1234+
const wrapper = mount({
1235+
template: `
1236+
<FormulateForm
1237+
v-model="formValues"
1238+
>
1239+
<FormulateInput
1240+
v-if="showCheckboxes"
1241+
type="checkbox"
1242+
name="flavor"
1243+
:keep-model-data="true"
1244+
:options="options"
1245+
/>
1246+
</FormulateForm>
1247+
`,
1248+
data () {
1249+
return {
1250+
showCheckboxes: true,
1251+
options: ['Apple', 'Banana'],
1252+
formValues: {}
1253+
}
1254+
}
1255+
})
1256+
wrapper.find('input[type="checkbox"]').setChecked(true)
1257+
await flushPromises()
1258+
expect(wrapper.vm.formValues).toEqual({ flavor: ['Apple'] })
1259+
wrapper.vm.options = ['Pizza']
1260+
await flushPromises()
1261+
expect(wrapper.vm.formValues).toEqual({ flavor: ['Apple'] })
1262+
wrapper.vm.showCheckboxes = false
1263+
await flushPromises()
1264+
expect(wrapper.vm.formValues).toEqual({ flavor: ['Apple'] })
1265+
1266+
})
12321267
})

test/unit/FormulateInputGroup.test.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1059,4 +1059,39 @@ describe('FormulateInputGroup', () => {
10591059
).toEqual(['@fb-jane'])
10601060
})
10611061

1062+
it.only('does not let checkboxes wipe their own value out', async () => {
1063+
const wrapper = mount({
1064+
template: `
1065+
<FormulateForm
1066+
v-model="formData"
1067+
>
1068+
<FormulateInput
1069+
name="pizzas"
1070+
type="group"
1071+
:repeatable="true"
1072+
>
1073+
<FormulateInput
1074+
type="checkbox"
1075+
name="flavors"
1076+
:options="options"
1077+
/>
1078+
</FormulateInput>
1079+
</FormulateForm>
1080+
`,
1081+
data () {
1082+
return {
1083+
options: ['cheese', 'pepperoni', 'pineapple', 'sausage'],
1084+
formData: {
1085+
pizzas: [{ flavors: ['pepperoni', 'pineapple'] }]
1086+
}
1087+
}
1088+
}
1089+
})
1090+
await flushPromises()
1091+
expect(wrapper.vm.formData).toEqual({
1092+
pizzas: [{
1093+
flavors: ['pepperoni', 'pineapple']
1094+
}]
1095+
})
1096+
})
10621097
})

0 commit comments

Comments
 (0)