You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
ant-design-vue/components/radio/Radio.vue

107 lines
2.7 KiB

7 years ago
<template>
<label :class="classes">
<span :class="checkboxClass">
<input :name="name" type="radio" :disabled="disabled"
7 years ago
:class="`${prefixCls}-input`" :checked="stateChecked"
7 years ago
@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,
},
7 years ago
defaultChecked: Boolean,
checked: { type: Boolean, default: undefined },
7 years ago
disabled: Boolean,
isGroup: Boolean,
value: [String, Number, Boolean],
name: String,
},
model: {
prop: 'checked',
},
7 years ago
inject: {
radioGroupContext: { default: undefined },
},
7 years ago
data () {
7 years ago
const { radioGroupContext, checked, defaultChecked, value } = this
let stateChecked
if (radioGroupContext && radioGroupContext.stateValue) {
stateChecked = radioGroupContext.stateValue === value
}
7 years ago
return {
7 years ago
stateChecked: stateChecked === undefined
? checked === undefined ? defaultChecked : checked
: stateChecked,
7 years ago
}
},
7 years ago
computed: {
hasDefaultSlot () {
return !!this.$slots.default
},
classes () {
7 years ago
const { prefixCls, disabled, stateChecked } = this
7 years ago
return {
[`${prefixCls}-wrapper`]: true,
7 years ago
[`${prefixCls}-wrapper-checked`]: stateChecked,
7 years ago
[`${prefixCls}-wrapper-disabled`]: disabled,
}
},
checkboxClass () {
7 years ago
const { prefixCls, disabled, stateChecked } = this
7 years ago
return {
[`${prefixCls}`]: true,
7 years ago
[`${prefixCls}-checked`]: stateChecked,
7 years ago
[`${prefixCls}-disabled`]: disabled,
}
},
},
methods: {
handleChange (event) {
7 years ago
const targetChecked = event.target.checked
7 years ago
this.$emit('input', targetChecked)
const { name, value, checked, radioGroupContext, stateChecked } = this
if ((checked === undefined && !radioGroupContext) || (radioGroupContext && radioGroupContext.value === undefined)) {
7 years ago
this.stateChecked = targetChecked
}
7 years ago
const target = {
name,
value,
7 years ago
checked: !stateChecked,
7 years ago
}
7 years ago
if (this.radioGroupContext) {
this.radioGroupContext.handleChange({ target })
} else {
this.$emit('change', {
target,
stopPropagation () {
event.stopPropagation()
},
preventDefault () {
event.preventDefault()
},
})
7 years ago
}
},
},
7 years ago
watch: {
checked (val) {
this.stateChecked = val
},
7 years ago
'radioGroupContext.stateValue': function (stateValue) {
this.stateChecked = stateValue === this.value
},
7 years ago
},
7 years ago
}
</script>