Skip to content
This repository was archived by the owner on Nov 30, 2020. It is now read-only.

Commit 8de3032

Browse files
authored
feat(radio): allowing no js instantiation (#398)
1 parent c00da75 commit 8de3032

File tree

4 files changed

+55
-37
lines changed

4 files changed

+55
-37
lines changed

components/radio/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ data() {
2525
| disabled | Boolean | false | disabled radio |
2626
| value | String | '' | value of radio (will be v-modeled) |
2727
| name | String | '' | radio group name |
28+
| js | Boolean | true | Whether or not to use an optional JavaScript component to enhance it with a ripple interaction effect |
2829

2930
Non prop attributes are mapped to the inner input element.
3031

components/radio/Radio.vue

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
<template>
2-
<div class="mdc-radio">
2+
<div class="mdc-radio" :class="classes">
33
<input
44
class="mdc-radio__native-control"
55
type="radio"
66
v-bind="$attrs"
7+
v-on="$listeners"
78
@change="onChange"
9+
ref="input"
810
>
911
<div class="mdc-radio__background">
1012
<div class="mdc-radio__outer-circle" />
@@ -25,47 +27,50 @@ export default {
2527
event: 'change'
2628
},
2729
props: {
28-
checked: {
30+
js: {
2931
type: Boolean,
30-
default: false
31-
},
32-
disabled: {
33-
type: Boolean,
34-
default: false
35-
},
36-
value: {
37-
type: String,
38-
default: ''
32+
default: true
3933
}
4034
},
4135
data () {
4236
return {
4337
mdcRadio: undefined
4438
}
4539
},
40+
computed: {
41+
classes () {
42+
return {
43+
'mdc-radio--disabled': this.$attrs.disabled != null
44+
}
45+
}
46+
},
4647
watch: {
47-
checked () {
48-
this.mdcRadio.checked = this.checked
49-
},
50-
disabled () {
51-
this.mdcRadio.disabled = this.disabled
52-
},
53-
value () {
54-
this.mdcRadio.value = this.value
48+
js () {
49+
this.reInstantiate()
5550
}
5651
},
5752
mounted () {
58-
this.mdcRadio = MDCRadio.attachTo(this.$el)
59-
this.mdcRadio.checked = this.checked
60-
this.mdcRadio.disabled = this.disabled
61-
this.mdcRadio.value = this.value
53+
if (this.js) this.mdcRadio = MDCRadio.attachTo(this.$el)
6254
},
6355
beforeDestroy () {
64-
this.mdcRadio.destroy()
56+
if (this.js) this.mdcRadio.destroy()
6557
},
6658
methods: {
67-
onChange (event) {
68-
this.$emit('change', this.mdcRadio.value)
59+
onChange () {
60+
this.$emit('change', this.$refs.input.value)
61+
},
62+
reInstantiate () {
63+
if (this.js) {
64+
if (this.mdcRadio) {
65+
this.mdcRadio.destroy()
66+
}
67+
MDCRadio.attachTo(this.$el)
68+
} else {
69+
if (this.js) {
70+
this.mdcRadio.destroy()
71+
}
72+
this.mdcRadio = undefined
73+
}
6974
}
7075
}
7176
}

components/radio/__test__/Radio.spec.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,38 +2,41 @@ import 'mutationobserver-shim'
22
import { mount } from '@vue/test-utils'
33
import Radio from '../Radio.vue'
44

5-
describe('Checkbox', () => {
5+
describe('Radio', () => {
66
it('should mount', () => {
7-
let wrapper = mount(Radio)
7+
const wrapper = mount(Radio)
88
expect(wrapper.isVueInstance()).toBeTruthy()
99
expect(wrapper.vm.$data.mdcRadio).toBeDefined()
1010
})
1111

1212
it('should render with no prop', () => {
13-
let wrapper = mount(Radio)
13+
const wrapper = mount(Radio)
1414
expect(wrapper).toMatchSnapshot()
1515
expect(wrapper.classes()).toContain('mdc-radio')
1616
expect(wrapper.find('input').attributes('disabled')).toBeUndefined()
1717
})
1818

1919
it('should render as disabled', () => {
20-
let wrapper = mount(Radio, {
21-
propsData: {
20+
const wrapper = mount(Radio, {
21+
attrs: {
2222
disabled: true
2323
}
2424
})
25+
2526
expect(wrapper).toMatchSnapshot()
2627
expect(wrapper.classes()).toContain('mdc-radio--disabled')
2728
expect(wrapper.find('input').attributes('disabled')).toBeDefined()
2829
})
2930

3031
it('should render and emit', () => {
31-
let wrapper = mount(Radio, {
32-
propsData: {
32+
const wrapper = mount(Radio, {
33+
attrs: {
3334
value: 'val'
3435
}
3536
})
3637

38+
expect(wrapper).toMatchSnapshot()
39+
3740
const radio = wrapper.find('input')
3841
radio.setChecked(true)
3942
expect(wrapper.emitted().change.length).toBe(1)

components/radio/__test__/__snapshots__/Radio.spec.js.snap

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,25 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3-
exports[`Checkbox should render as disabled 1`] = `
4-
<div class="mdc-radio mdc-radio--disabled"><input type="radio" class="mdc-radio__native-control" disabled="" value="">
3+
exports[`Radio should render and emit 1`] = `
4+
<div class="mdc-radio"><input type="radio" class="mdc-radio__native-control" value="val">
55
<div class="mdc-radio__background">
66
<div class="mdc-radio__outer-circle"></div>
77
<div class="mdc-radio__inner-circle"></div>
88
</div>
99
</div>
1010
`;
1111
12-
exports[`Checkbox should render with no prop 1`] = `
13-
<div class="mdc-radio"><input type="radio" class="mdc-radio__native-control" value="">
12+
exports[`Radio should render as disabled 1`] = `
13+
<div class="mdc-radio mdc-radio--disabled"><input type="radio" disabled="disabled" class="mdc-radio__native-control">
14+
<div class="mdc-radio__background">
15+
<div class="mdc-radio__outer-circle"></div>
16+
<div class="mdc-radio__inner-circle"></div>
17+
</div>
18+
</div>
19+
`;
20+
21+
exports[`Radio should render with no prop 1`] = `
22+
<div class="mdc-radio"><input type="radio" class="mdc-radio__native-control">
1423
<div class="mdc-radio__background">
1524
<div class="mdc-radio__outer-circle"></div>
1625
<div class="mdc-radio__inner-circle"></div>

0 commit comments

Comments
 (0)