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.jsx

108 lines
2.9 KiB

import PropTypes from '../_util/vue-types'
7 years ago
import hasProp from '../_util/props-util'
7 years ago
export default {
name: 'ARadio',
7 years ago
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: PropTypes.any,
7 years ago
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 !== undefined) {
7 years ago
stateChecked = radioGroupContext.stateValue === value
}
7 years ago
return {
7 years ago
stateChecked: stateChecked === undefined
7 years ago
? !hasProp(this, 'checked') ? defaultChecked : checked
7 years ago
: stateChecked,
7 years ago
}
},
7 years ago
computed: {
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)
7 years ago
const { name, value, radioGroupContext } = this
7 years ago
if ((!hasProp(this, 'checked') && !radioGroupContext) || (radioGroupContext && radioGroupContext.value === undefined)) {
7 years ago
this.stateChecked = targetChecked
}
7 years ago
const target = {
name,
value,
7 years ago
checked: targetChecked,
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
},
render () {
const { classes, checkboxClass, disabled, prefixCls, stateChecked, handleChange, name, $slots } = this
return (
<label class={classes}>
<span class={checkboxClass}>
<input name={name} type='radio' disabled={disabled}
class={`${prefixCls}-input`} checked={stateChecked}
onChange={handleChange}
/>
<span class={`${prefixCls}-inner`} />
</span>
{$slots.default ? <span>
{$slots.default}
</span> : null}
</label>
)
},
7 years ago
}