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

146 lines
3.6 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,
id: String,
autoFocus: Boolean,
7 years ago
},
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
}
},
mounted () {
this.$nextTick(() => {
if (this.autoFocus) {
this.$refs.input.focus()
}
})
},
7 years ago
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
}
},
focus () {
this.$refs.input.focus()
},
blur () {
this.$refs.input.blur()
},
onFocus (e) {
this.$emit('focus', e)
},
onBlur (e) {
this.$emit('blur', e)
},
onMouseEnter (e) {
this.$emit('mouseenter', e)
},
onMouseLeave (e) {
this.$emit('mouseleave', e)
},
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 { id, prefixCls,
stateChecked, handleChange, $slots,
onFocus,
onBlur,
onMouseEnter,
onMouseLeave,
radioGroupContext,
} = this
let { name, disabled } = this
if (radioGroupContext) {
name = radioGroupContext.name
disabled = disabled || radioGroupContext.disabled
}
const wrapperClassString = {
[`${prefixCls}-wrapper`]: true,
[`${prefixCls}-wrapper-checked`]: stateChecked,
[`${prefixCls}-wrapper-disabled`]: disabled,
}
const checkboxClass = {
[`${prefixCls}`]: true,
[`${prefixCls}-checked`]: stateChecked,
[`${prefixCls}-disabled`]: disabled,
}
return (
<label
class={wrapperClassString}
onMouseenter={onMouseEnter}
onMouseleave={onMouseLeave}
>
<span class={checkboxClass}>
<input name={name} type='radio' disabled={disabled}
class={`${prefixCls}-input`} checked={stateChecked}
onChange={handleChange} id={id} ref='input'
onFocus={onFocus}
onBlur={onBlur}
/>
<span class={`${prefixCls}-inner`} />
</span>
{$slots.default ? <span>
{$slots.default}
</span> : null}
</label>
)
},
7 years ago
}