Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(radio): checked calculation simplified #27

Merged
merged 1 commit into from
Aug 20, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion __tests__/radio.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,12 +165,20 @@ describe('RadioButton', () => {
disabled: true
}
});
});

test('event is not emitted', () => {
const label = wrapper.find('label');
label.trigger('click');

expect(wrapper.emitted()['input']).toBeUndefined();
});

test('event is not emitted', () => {
test('toggle doesn\'t emit event', () => {
const toggleResult = wrapper.vm.toggle({
target: { value: 'blue' }
});
expect(toggleResult).toBeUndefined();
expect(wrapper.emitted()['input']).toBeUndefined();
});

Expand Down
3 changes: 2 additions & 1 deletion docs/.vuepress/components/radio-basic.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<div class="playground">
<vnt-radio name="abc" value="123" v-model="radioValue">Radio 1</vnt-radio>
<vnt-radio name="abc" value="456" v-model="radioValue">Radio 2</vnt-radio>
<vnt-radio name="abc" value="789" v-model="radioValue">Radio 3</vnt-radio>
<div class="result">radioValue: {{ radioValue }}</div>
</div>
</template>
Expand All @@ -16,7 +17,7 @@ export default {
},
data() {
return {
radioValue: '123'
radioValue: '456'
}
}
}
Expand Down
1 change: 1 addition & 0 deletions docs/components/radio.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
```html
<vnt-radio name="abc" value="123" v-model="radioValue">Radio 1</vnt-radio>
<vnt-radio name="abc" value="456" v-model="radioValue">Radio 2</vnt-radio>
<vnt-radio name="abc" value="789" v-model="radioValue">Radio 3</vnt-radio>
```

## Disabled
Expand Down
24 changes: 13 additions & 11 deletions src/components/radio/RadioButton.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<label class="vnt-radio">
<input class="vnt-radio__input"
type="radio"
:checked="checked"
:disabled="disabled"
:name="name"
v-bind="$attrs"
Expand All @@ -17,7 +18,16 @@
export default {
name: 'VntRadio',

model: {
prop: 'model',
event: 'input'
},

props: {
model: {
type: [String, Number, Boolean, Object],
default: undefined
},
disabled: {
type: Boolean,
default: false
Expand All @@ -32,17 +42,9 @@ export default {
}
},

mounted() {
if (!this.$vnode) {
return;
}

const vModel = this.$vnode.data.model || {};
const vModelValue = vModel.value;

if (!!vModelValue && vModelValue === this.$attrs.value) {
const input = this.$el.querySelector('input');
input.setAttribute('checked', true);
computed: {
checked() {
return this.$attrs.value === this.model;
}
},

Expand Down