ant-design-vue/components/radio/Radio.vue

107 lines
2.7 KiB
Vue
Raw Normal View History

2017-10-27 14:04:48 +08:00
<template>
<label :class="classes">
<span :class="checkboxClass">
<input :name="name" type="radio" :disabled="disabled"
2017-11-02 12:07:20 +08:00
:class="`${prefixCls}-input`" :checked="stateChecked"
2017-10-27 14:04:48 +08:00
@change="handleChange"
/>
<span :class="`${prefixCls}-inner`" />
</span>
<span v-if="hasDefaultSlot">
<slot></slot>
</span>
</label>
</template>
<script>
export default {
name: 'Radio',
props: {
prefixCls: {
default: 'ant-radio',
type: String,
},
2017-11-02 12:07:20 +08:00
defaultChecked: Boolean,
checked: { type: Boolean, default: undefined },
2017-10-27 14:04:48 +08:00
disabled: Boolean,
isGroup: Boolean,
value: [String, Number, Boolean],
name: String,
},
model: {
prop: 'checked',
},
2017-11-07 11:58:47 +08:00
inject: {
radioGroupContext: { default: undefined },
},
2017-11-02 12:07:20 +08:00
data () {
2017-11-07 11:58:47 +08:00
const { radioGroupContext, checked, defaultChecked, value } = this
let stateChecked
if (radioGroupContext && radioGroupContext.stateValue) {
stateChecked = radioGroupContext.stateValue === value
}
2017-11-02 12:07:20 +08:00
return {
2017-11-07 11:58:47 +08:00
stateChecked: stateChecked === undefined
? checked === undefined ? defaultChecked : checked
: stateChecked,
2017-11-02 12:07:20 +08:00
}
},
2017-10-27 14:04:48 +08:00
computed: {
hasDefaultSlot () {
return !!this.$slots.default
},
classes () {
2017-11-02 12:07:20 +08:00
const { prefixCls, disabled, stateChecked } = this
2017-10-27 14:04:48 +08:00
return {
[`${prefixCls}-wrapper`]: true,
2017-11-02 12:07:20 +08:00
[`${prefixCls}-wrapper-checked`]: stateChecked,
2017-10-27 14:04:48 +08:00
[`${prefixCls}-wrapper-disabled`]: disabled,
}
},
checkboxClass () {
2017-11-02 12:07:20 +08:00
const { prefixCls, disabled, stateChecked } = this
2017-10-27 14:04:48 +08:00
return {
[`${prefixCls}`]: true,
2017-11-02 12:07:20 +08:00
[`${prefixCls}-checked`]: stateChecked,
2017-10-27 14:04:48 +08:00
[`${prefixCls}-disabled`]: disabled,
}
},
},
methods: {
handleChange (event) {
2017-11-02 12:07:20 +08:00
const targetChecked = event.target.checked
2017-11-07 11:58:47 +08:00
this.$emit('input', targetChecked)
const { name, value, checked, radioGroupContext, stateChecked } = this
if ((checked === undefined && !radioGroupContext) || (radioGroupContext && radioGroupContext.value === undefined)) {
2017-11-02 12:07:20 +08:00
this.stateChecked = targetChecked
}
2017-10-27 14:04:48 +08:00
const target = {
name,
value,
2017-11-07 11:58:47 +08:00
checked: !stateChecked,
2017-10-27 14:04:48 +08:00
}
2017-11-07 11:58:47 +08:00
if (this.radioGroupContext) {
this.radioGroupContext.handleChange({ target })
} else {
this.$emit('change', {
target,
stopPropagation () {
event.stopPropagation()
},
preventDefault () {
event.preventDefault()
},
})
2017-10-27 14:04:48 +08:00
}
},
},
2017-11-02 12:07:20 +08:00
watch: {
checked (val) {
this.stateChecked = val
},
2017-11-07 11:58:47 +08:00
'radioGroupContext.stateValue': function (stateValue) {
this.stateChecked = stateValue === this.value
},
2017-11-02 12:07:20 +08:00
},
2017-10-27 14:04:48 +08:00
}
</script>