Skip to content

Commit d5cfdc2

Browse files
committed
fix(selectable): pass disabled prop to internal input
Also DRYed up VRadio a bit Applies to VRadio, VCheckbox, VSwitch fixes #4796, fixes #4797
1 parent 25ae6b3 commit d5cfdc2

File tree

7 files changed

+64
-36
lines changed

7 files changed

+64
-36
lines changed

src/components/VRadioGroup/VRadio.js

Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import VLabel from '../VLabel'
99
import Colorable from '../../mixins/colorable'
1010
import Rippleable from '../../mixins/rippleable'
1111
import Themeable from '../../mixins/themeable'
12+
import Selectable from '../../mixins/selectable'
1213
import {
1314
inject as RegistrableInject
1415
} from '../../mixins/registrable'
@@ -100,31 +101,11 @@ export default {
100101
},
101102

102103
methods: {
103-
genInput (type, attrs) {
104-
return this.$createElement('input', {
105-
attrs: Object.assign({}, attrs, {
106-
'aria-label': this.label,
107-
name: this.radio.name || (this.radio._uid ? 'v-radio-' + this.radio._uid : false),
108-
value: this.value,
109-
role: type,
110-
type
111-
}),
112-
domProps: {
113-
checked: this.isActive
114-
},
115-
on: {
116-
blur: this.onBlur,
117-
change: this.onChange,
118-
focus: this.onFocus,
119-
keydown: e => {
120-
if ([keyCodes.enter, keyCodes.space].includes(e.keyCode)) {
121-
e.preventDefault()
122-
this.onChange()
123-
}
124-
}
125-
},
126-
ref: 'input'
127-
})
104+
genInput (...args) {
105+
// We can't actually use the mixin directly because
106+
// it's made for standalone components, but its
107+
// genInput method is exactly what we need
108+
return Selectable.methods.genInput.call(this, ...args)
128109
},
129110
genLabel () {
130111
return this.$createElement(VLabel, {
@@ -145,7 +126,8 @@ export default {
145126
staticClass: 'v-input--selection-controls__input'
146127
}, [
147128
this.genInput('radio', {
148-
'aria-checked': this.isActive.toString(),
129+
name: this.radio.name || (this.radio._uid ? 'v-radio-' + this.radio._uid : false),
130+
value: this.value,
149131
...this.$attrs
150132
}),
151133
!this.isDisabled && this.genRipple({
@@ -173,6 +155,12 @@ export default {
173155
if (!this.isDisabled && (!this.isActive || !this.radio.mandatory)) {
174156
this.$emit('change', this.value)
175157
}
158+
},
159+
onKeydown (e) {
160+
if ([keyCodes.enter, keyCodes.space].includes(e.keyCode)) {
161+
e.preventDefault()
162+
this.onChange()
163+
}
176164
}
177165
},
178166

src/mixins/selectable.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ export default {
7373

7474
return this.valueComparator(input, this.trueValue)
7575
},
76+
isDisabled () {
77+
return this.disabled
78+
},
7679
isDirty () {
7780
return this.isActive
7881
}
@@ -96,20 +99,25 @@ export default {
9699
},
97100
genInput (type, attrs) {
98101
return this.$createElement('input', {
99-
attrs: Object.assign({}, {
102+
attrs: Object.assign({
100103
'aria-label': this.label,
101104
'aria-checked': this.isActive.toString(),
105+
disabled: this.isDisabled,
102106
id: this.id,
103107
role: type,
104108
type,
105109
value: this.inputValue
106110
}, attrs),
111+
domProps: {
112+
checked: this.isActive
113+
},
107114
on: {
108115
blur: this.onBlur,
109116
change: this.onChange,
110117
focus: this.onFocus,
111118
keydown: this.onKeydown
112-
}
119+
},
120+
ref: 'input'
113121
})
114122
},
115123
onBlur () {

test/unit/components/VCheckbox/VCheckbox.spec.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,4 +406,13 @@ test('VCheckbox.js', ({ mount }) => {
406406

407407
expect(wrapper.html()).toMatchSnapshot()
408408
})
409+
410+
it('should be disabled', () => {
411+
const wrapper = mount(VCheckbox, {
412+
propsData: { disabled: true }
413+
})
414+
const input = wrapper.first('input')
415+
416+
expect(input.html()).toMatchSnapshot()
417+
})
409418
})

test/unit/components/VCheckbox/__snapshots__/VCheckbox.spec.js.snap

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3+
exports[`VCheckbox.js should be disabled 1`] = `
4+
5+
<input aria-checked="false"
6+
disabled="disabled"
7+
role="checkbox"
8+
type="checkbox"
9+
>
10+
11+
`;
12+
313
exports[`VCheckbox.js should render themed component 1`] = `
414
515
<div class="v-input v-input--selection-controls v-input--checkbox theme--light">

test/unit/components/VRadioGroup/VRadioGroup.spec.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,9 +311,11 @@ test('VRadioGroup.vue', ({ mount }) => {
311311

312312
const onChange = jest.fn()
313313
const radio = wrapper.first(VRadio)
314+
const input = radio.first('input')
314315
radio.vm.$on('change', onChange)
315316
radio.first('input').trigger('change')
316317
expect(onChange).not.toBeCalled()
318+
expect(input.html()).toMatchSnapshot()
317319
})
318320

319321
it('should make radios readonly', async () => {

test/unit/components/VRadioGroup/__snapshots__/VRadio.spec.js.snap

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ exports[`VRadio.vue should not render aria-label attribute with no label value o
55
<div class="v-radio">
66
<div class="v-input--selection-controls__input">
77
<input aria-checked="false"
8-
name="name"
98
role="radio"
109
type="radio"
10+
name="name"
1111
>
1212
<div class="v-input--selection-controls__ripple">
1313
</div>
@@ -30,11 +30,11 @@ exports[`VRadio.vue should render aria-label attribute with label value on input
3030
3131
<div class="v-radio">
3232
<div class="v-input--selection-controls__input">
33-
<input aria-checked="false"
34-
aria-label="Test"
35-
name="name"
33+
<input aria-label="Test"
34+
aria-checked="false"
3635
role="radio"
3736
type="radio"
37+
name="name"
3838
>
3939
<div class="v-input--selection-controls__ripple">
4040
</div>
@@ -59,9 +59,9 @@ exports[`VRadio.vue should render proper input name 1`] = `
5959
<div class="v-radio">
6060
<div class="v-input--selection-controls__input">
6161
<input aria-checked="false"
62-
name="name"
6362
role="radio"
6463
type="radio"
64+
name="name"
6565
>
6666
<div class="v-input--selection-controls__ripple">
6767
</div>
@@ -85,9 +85,9 @@ exports[`VRadio.vue should render role and aria-checked attributes on input grou
8585
<div class="v-radio accent--text">
8686
<div class="v-input--selection-controls__input">
8787
<input aria-checked="true"
88-
name="name"
8988
role="radio"
9089
type="radio"
90+
name="name"
9191
>
9292
<div class="v-input--selection-controls__ripple accent--text">
9393
</div>

test/unit/components/VRadioGroup/__snapshots__/VRadioGroup.spec.js.snap

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ exports[`VRadioGroup.vue should be in error when v-radio-group is 1`] = `
1313
<div class="v-radio">
1414
<div class="v-input--selection-controls__input">
1515
<input aria-checked="false"
16-
name="v-radio-4"
1716
role="radio"
1817
type="radio"
18+
name="v-radio-4"
1919
>
2020
<div class="v-input--selection-controls__ripple error--text">
2121
</div>
@@ -55,9 +55,9 @@ exports[`VRadioGroup.vue should be in error when v-radio-group is 2`] = `
5555
<div class="v-radio">
5656
<div class="v-input--selection-controls__input">
5757
<input aria-checked="false"
58-
name="v-radio-4"
5958
role="radio"
6059
type="radio"
60+
name="v-radio-4"
6161
>
6262
<div class="v-input--selection-controls__ripple">
6363
</div>
@@ -84,6 +84,17 @@ exports[`VRadioGroup.vue should be in error when v-radio-group is 2`] = `
8484
8585
`;
8686
87+
exports[`VRadioGroup.vue should make radios disabled 1`] = `
88+
89+
<input aria-checked="false"
90+
disabled="disabled"
91+
role="radio"
92+
type="radio"
93+
name="v-radio-43"
94+
>
95+
96+
`;
97+
8798
exports[`VRadioGroup.vue should render role on radio group 1`] = `
8899
89100
<div class="v-input v-input--selection-controls v-input--radio-group v-input--radio-group--column">

0 commit comments

Comments
 (0)