Skip to content

Commit ccf7517

Browse files
committed
add uncheckedValue to fixed version
1 parent 4b6755c commit ccf7517

File tree

2 files changed

+56
-4
lines changed

2 files changed

+56
-4
lines changed

src/App.vue

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,29 @@
482482
<strong>{{ checkboxes.status }}</strong>
483483
</div>
484484
</div>
485+
<h4 class="m-2">array with uncheckedValue</h4>
486+
<div class="m-4">
487+
<b-form-checkbox
488+
id="checkbox-1"
489+
v-model="checkboxes.statusArray"
490+
name="checkbox-1"
491+
value="accepted"
492+
unchecked-value="not_accepted"
493+
>I accept the terms and use</b-form-checkbox
494+
>
495+
<b-form-checkbox
496+
id="checkbox-2"
497+
v-model="checkboxes.statusArray"
498+
name="checkbox-2"
499+
value="accepted2"
500+
unchecked-value="not_accepted2"
501+
>I accept the terms and use</b-form-checkbox
502+
>
503+
<div>
504+
State:
505+
<strong>{{ checkboxes.statusArray }}</strong>
506+
</div>
507+
</div>
485508
<h4 class="m-2">Individual</h4>
486509
<div class="row mx-4">
487510
<b-form-checkbox v-model="checkedDefault" class="col-4">Default</b-form-checkbox>
@@ -1929,6 +1952,7 @@ export default defineComponent({
19291952
}
19301953
const checkboxes = reactive({
19311954
status: 'accepted',
1955+
statusArray: ['accepted'],
19321956
selected: ['pineapple', {foo: 1}],
19331957
options: [
19341958
{text: 'Orange', value: 'orange'},

src/components/BFormCheckbox/BFormCheckbox.vue

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,39 @@ export default defineComponent({
6464
const isFocused = ref(false)
6565
6666
const localValue = computed({
67-
get: () => props.modelValue,
67+
get: () => {
68+
if (props.uncheckedValue) {
69+
if (!Array.isArray(props.modelValue)) {
70+
return props.modelValue ? props.value : props.uncheckedValue
71+
}
72+
return props.modelValue.indexOf(props.value) > -1
73+
}
74+
return props.modelValue
75+
},
6876
set: (newValue: any) => {
69-
emit('input', newValue)
70-
emit('update:modelValue', newValue)
71-
emit('change', newValue)
77+
let emitValue = newValue
78+
console.log(newValue)
79+
if (props.uncheckedValue) {
80+
if (!Array.isArray(props.modelValue)) {
81+
emitValue = newValue ? props.value : props.uncheckedValue
82+
} else {
83+
emitValue = props.modelValue
84+
console.log('array', emitValue, newValue)
85+
if (newValue) {
86+
if (emitValue.indexOf(props.uncheckedValue) > -1)
87+
emitValue.splice(emitValue.indexOf(props.uncheckedValue), 1)
88+
emitValue.push(props.value)
89+
} else {
90+
if (emitValue.indexOf(props.value) > -1)
91+
emitValue.splice(emitValue.indexOf(props.value), 1)
92+
emitValue.push(props.uncheckedValue)
93+
}
94+
}
95+
}
96+
console.log(emitValue)
97+
emit('input', emitValue)
98+
emit('update:modelValue', emitValue)
99+
emit('change', emitValue)
72100
},
73101
})
74102

0 commit comments

Comments
 (0)