Skip to content

Commit 74dc419

Browse files
skjnldsvsusnux
authored andcommitted
fix(NcCheckboxRadioSwitch): keep data attributes bound to the wrapper
Signed-off-by: John Molakvoæ (skjnldsv) <[email protected]>
1 parent 229f6c1 commit 74dc419

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

src/components/NcCheckboxRadioSwitch/NcCheckboxRadioSwitch.vue

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ export default {
258258
class="checkbox-radio-switch"
259259
:style="cssVars"
260260
:type="isButtonType ? 'button' : null"
261-
v-bind="isButtonType ? $attrs : {}"
261+
v-bind="isButtonType ? $attrs : dataAttrs"
262262
v-on="isButtonType ? listeners : null">
263263
<input v-if="!isButtonType"
264264
:id="id"
@@ -272,7 +272,7 @@ export default {
272272
:indeterminate.prop="hasIndeterminate ? indeterminate : null"
273273
:required="required"
274274
:name="name"
275-
v-bind="$attrs"
275+
v-bind="nonDataAttrs"
276276
v-on="listeners">
277277
<NcCheckboxContent :id="id"
278278
class="checkbox-radio-switch__content"
@@ -450,6 +450,18 @@ export default {
450450
emits: ['update:checked'],
451451

452452
computed: {
453+
dataAttrs() {
454+
// filter all data attributes
455+
return Object.fromEntries(Object.entries(this.$attrs)
456+
.filter(([key]) => key.startsWith('data-')))
457+
},
458+
459+
nonDataAttrs() {
460+
// filter all non-data attributes
461+
return Object.fromEntries(Object.entries(this.$attrs)
462+
.filter(([key]) => !key.startsWith('data-')))
463+
},
464+
453465
isButtonType() {
454466
return this.type === TYPE_BUTTON
455467
},

tests/unit/components/NcCheckboxRadioSwitch/checkbox.spec.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,25 @@ describe('NcCheckboxRadioSwitch', () => {
1717
expect(wrapper.text()).toContain('Test')
1818
})
1919

20-
it('forwards aria-invalid and aria-errormessage to input', () => {
20+
it('forwards all but data- attributes to the input', () => {
2121
const wrapper = shallowMount(NcCheckboxRadioSwitch, {
2222
slots: {
2323
default: 'Test',
2424
},
2525
attrs: {
2626
'aria-invalid': 'true',
2727
'aria-errormessage': 'id-test',
28+
'data-test': 'checkbox-test-data-attr',
29+
title: 'Test title',
2830
},
2931
})
3032

3133
const input = wrapper.find('input')
3234
expect(input.attributes('aria-invalid')).toBe('true')
3335
expect(input.attributes('aria-errormessage')).toBe('id-test')
36+
expect(input.attributes('title')).toBe('Test title')
37+
expect(input.attributes('data-test')).not.toBe('checkbox-test-data-attr')
38+
expect(wrapper.attributes('data-test')).toBe('checkbox-test-data-attr')
39+
expect(wrapper.attributes('title')).not.toBe('Test title')
3440
})
3541
})

0 commit comments

Comments
 (0)