Skip to content

Commit 4b6755c

Browse files
committed
fix radio also
1 parent a37e2cd commit 4b6755c

File tree

3 files changed

+16
-53
lines changed

3 files changed

+16
-53
lines changed

src/App.vue

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -692,6 +692,7 @@
692692
<b-form-radio value="second">Or toggle this other custom radio</b-form-radio>
693693
<b-form-radio value="third" disabled>This one is Disabled</b-form-radio>
694694
<b-form-radio :value="{fourth: 4}">This is the 4th radio</b-form-radio>
695+
<b-form-radio :value="{fifth: 5}">This is the 5th radio</b-form-radio>
695696
</b-form-radio-group>
696697

697698
<div class="mt-3">
@@ -715,6 +716,10 @@
715716
</div>
716717
</div>
717718
<h4 class="m-2">Button styles radios</h4>
719+
<div class="mt-3">
720+
Selected:
721+
<strong>{{ radios.ex3.selected }}</strong>
722+
</div>
718723
<div class="m-4">
719724
<b-form-radio-group
720725
id="btn-radios-1"
@@ -1955,21 +1960,24 @@ export default defineComponent({
19551960
],
19561961
},
19571962
ex2: {
1958-
selected: {d: 1},
1963+
selected: {e: 1},
19591964
options: [
19601965
{item: 'A', name: 'Option A'},
19611966
{item: 'B', name: 'Option B'},
19621967
{item: 'D', name: 'Option C', notEnabled: true},
19631968
{item: {d: 1}, name: 'Option D'},
1969+
{item: {e: 1}, name: 'Option E'},
19641970
],
19651971
},
19661972
ex3: {
1967-
selected: 'radio1',
1973+
selected: {e: 1},
19681974
options: [
19691975
{text: 'Radio 1', value: 'radio1'},
19701976
{text: 'Radio 3', value: 'radio2'},
19711977
{text: 'Radio 3 (disabled)', value: 'radio3', disabled: true},
19721978
{text: 'Radio 4', value: 'radio4'},
1979+
{value: {d: 1}, text: 'Option D'},
1980+
{value: {e: 1}, text: 'Option E'},
19731981
],
19741982
},
19751983
})

src/components/BFormRadio/BFormRadio.vue

Lines changed: 3 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
:id="computedId"
55
v-bind="$attrs"
66
ref="input"
7+
v-model="localValue"
78
:class="inputClasses"
89
type="radio"
910
:disabled="disabled"
@@ -13,11 +14,7 @@
1314
:aria-label="ariaLabel"
1415
:aria-labelledby="ariaLabelledBy"
1516
:value="value"
16-
:checked="isChecked"
1717
:aria-required="name && required ? 'true' : null"
18-
@click.stop="handleClick($event.target.checked)"
19-
@focus="focus"
20-
@blur="blur"
2118
/>
2219
<label
2320
v-if="$slots.default || !plain"
@@ -61,7 +58,7 @@ export default defineComponent({
6158
const input: Ref<HTMLElement> = ref(null as unknown as HTMLElement)
6259
const isFocused = ref(false)
6360
64-
const localChecked: any = computed({
61+
const localValue: any = computed({
6562
get: () => props.modelValue,
6663
set: (newValue: any) => {
6764
emit('input', newValue)
@@ -70,18 +67,6 @@ export default defineComponent({
7067
},
7168
})
7269
73-
const focus = () => {
74-
isFocused.value = true
75-
if (!props.disabled) input.value.focus()
76-
}
77-
78-
const blur = () => {
79-
isFocused.value = false
80-
if (!props.disabled) {
81-
input.value.blur()
82-
}
83-
}
84-
8570
const isChecked = computed(() => {
8671
if (Array.isArray(props.modelValue)) {
8772
return (props.modelValue || []).find((e) => e === props.value)
@@ -93,23 +78,6 @@ export default defineComponent({
9378
const inputClasses = getInputClasses(props)
9479
const labelClasses = getLabelClasses(props)
9580
96-
const handleClick = async (checked: boolean) => {
97-
if (Array.isArray(props.modelValue)) {
98-
if ((props.modelValue || [])[0] !== props.value) {
99-
localChecked.value = [props.value]
100-
}
101-
} else if (checked && props.modelValue !== props.value) {
102-
localChecked.value = props.value
103-
}
104-
}
105-
106-
watch(
107-
() => props.modelValue,
108-
(newValue) => {
109-
emit('input', newValue)
110-
}
111-
)
112-
11381
// TODO: make jest tests compatible with the v-focus directive
11482
if (props.autofocus) {
11583
onMounted(() => {
@@ -118,17 +86,14 @@ export default defineComponent({
11886
}
11987
12088
return {
121-
localChecked,
89+
localValue,
12290
computedId,
12391
classes,
12492
inputClasses,
12593
labelClasses,
12694
isChecked,
12795
isFocused,
12896
input,
129-
handleClick,
130-
focus,
131-
blur,
13297
}
13398
},
13499
})

src/components/BFormRadio/BFormRadioGroup.vue

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
tabindex="-1"
99
>
1010
<template v-for="(item, key) in checkboxList" :key="key">
11-
<b-form-radio v-model="item.model" v-bind="item.props" @change="childUpdated">
11+
<b-form-radio v-model="localValue" v-bind="item.props">
1212
<!-- eslint-disable vue/no-v-html -->
1313
<span v-if="item.html" v-html="item.html" />
1414
<!--eslint-enable-->
@@ -60,7 +60,7 @@ export default defineComponent({
6060
const computedId = useId(props.id, 'radio')
6161
const computedName = useId(props.name, 'checkbox')
6262
63-
const localChecked = computed({
63+
const localValue = computed({
6464
get: () => props.modelValue,
6565
set: (newValue: any) => {
6666
emit('input', newValue)
@@ -76,18 +76,9 @@ export default defineComponent({
7676
.map((e, idx) => bindGroupProps(e, idx, props, computedName, computedId))
7777
.map((e) => ({
7878
...e,
79-
model:
80-
JSON.stringify(props.modelValue) === JSON.stringify(e.props?.value)
81-
? e.props?.value
82-
: null,
8379
}))
8480
)
8581
86-
const childUpdated = (newValue: any) => {
87-
emit('change', newValue)
88-
emit('update:modelValue', newValue)
89-
}
90-
9182
const attrs = getGroupAttr(props)
9283
const classes = getGroupClasses(props)
9384
@@ -97,9 +88,8 @@ export default defineComponent({
9788
attrs,
9889
classes,
9990
checkboxList,
100-
childUpdated,
10191
computedId,
102-
localChecked,
92+
localValue,
10393
}
10494
},
10595
})

0 commit comments

Comments
 (0)