Skip to content
This repository was archived by the owner on Mar 27, 2025. It is now read-only.

Commit 8a588c9

Browse files
committed
fix(BFormCheckbox): can use primitives and arrays. fixes bootstrap-vue-next#1382
1 parent f55e248 commit 8a588c9

File tree

2 files changed

+38
-17
lines changed

2 files changed

+38
-17
lines changed

packages/bootstrap-vue-next/src/components/BFormCheckbox/BFormCheckbox.vue

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ const props = withDefaults(
9292
inline: false,
9393
size: undefined,
9494
value: true,
95-
uncheckedValue: false,
95+
uncheckedValue: undefined,
9696
}
9797
)
9898
@@ -146,27 +146,46 @@ const localValue = computed({
146146
? parentData.modelValue.value
147147
.map((el) => JSON.stringify(el))
148148
.includes(JSON.stringify(props.value))
149+
: Array.isArray(modelValue.value)
150+
? modelValue.value?.map((el) => JSON.stringify(el)).includes(JSON.stringify(props.value))
149151
: JSON.stringify(modelValue.value) === JSON.stringify(props.value),
150152
set: (newValue) => {
151-
const updateValue = newValue ? props.value : props.uncheckedValue
152-
153+
const updateValue = newValue
154+
? props.value
155+
: props.uncheckedValue === undefined
156+
? false
157+
: props.uncheckedValue
153158
emit('input', updateValue)
154-
modelValue.value = updateValue
159+
160+
if (parentData === null && Array.isArray(modelValue.value)) {
161+
if (newValue) {
162+
modelValue.value.push(props.value)
163+
if (props.uncheckedValue !== undefined)
164+
modelValue.value = modelValue.value.filter(
165+
(x) => JSON.stringify(x) !== JSON.stringify(props.uncheckedValue)
166+
)
167+
} else {
168+
if (props.uncheckedValue !== undefined) modelValue.value.push(props.uncheckedValue)
169+
modelValue.value = modelValue.value.filter(
170+
(x) => JSON.stringify(x) !== JSON.stringify(props.value)
171+
)
172+
}
173+
} else {
174+
modelValue.value = updateValue
175+
if (parentData !== null) {
176+
if (updateValue === false) {
177+
parentData.remove(props.value)
178+
} else {
179+
parentData.set(props.value)
180+
}
181+
}
182+
}
155183
nextTick(() => {
156184
emit('change', updateValue)
157185
})
158186
},
159187
})
160188
161-
watch(modelValue, (newValue) => {
162-
if (parentData === null) return
163-
if (newValue === false) {
164-
parentData.remove(props.value)
165-
return
166-
}
167-
parentData.set(props.value)
168-
})
169-
170189
const computedRequired = computed(
171190
() =>
172191
!!(props.name ?? parentData?.name.value) &&

packages/bootstrap-vue-next/src/components/BFormCheckbox/BFormCheckboxGroup.vue

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,9 @@ provide(checkboxGroupKey, {
150150
set: (
151151
value: unknown[] | Set<unknown> | boolean | string | Record<string, unknown> | number | null
152152
) => {
153-
const localValue = [...modelValue.value]
153+
let localValue = [...modelValue.value]
154+
// clean up the value so we don't end up with duplicates
155+
localValue = localValue.filter((x) => JSON.stringify(x) !== JSON.stringify(value))
154156
localValue.push(value)
155157
156158
emit('input', localValue)
@@ -162,9 +164,9 @@ provide(checkboxGroupKey, {
162164
remove: (
163165
value: unknown[] | Set<unknown> | boolean | string | Record<string, unknown> | number | null
164166
) => {
165-
const localValue = [...modelValue.value]
166-
// TODO if the value is an array, set, or object, indexOf may not work correctly
167-
localValue.splice(modelValue.value.indexOf(value), 1)
167+
let localValue = [...modelValue.value]
168+
// TODO if the value is an array, set, or object, filter may not work correctly
169+
localValue = localValue.filter((x) => JSON.stringify(x) !== JSON.stringify(value))
168170
169171
emit('input', localValue)
170172
modelValue.value = localValue

0 commit comments

Comments
 (0)