2018-09-05 21:28:54 +08:00
|
|
|
import classNames from 'classnames'
|
2018-03-20 19:06:04 +08:00
|
|
|
import PropTypes from '../_util/vue-types'
|
2018-03-19 10:16:27 +08:00
|
|
|
import Radio from './Radio'
|
2018-09-05 21:28:54 +08:00
|
|
|
import { getOptionProps, filterEmpty, hasProp } from '../_util/props-util'
|
|
|
|
function noop () {}
|
|
|
|
|
|
|
|
function getCheckedValue (children) {
|
|
|
|
let value = null
|
|
|
|
let matched = false
|
|
|
|
children.forEach((radio) => {
|
|
|
|
if (radio && radio.componentOptions && radio.componentOptions.propsData.checked) {
|
|
|
|
value = radio.componentOptions.propsData.value
|
|
|
|
matched = true
|
|
|
|
}
|
|
|
|
})
|
|
|
|
return matched ? { value } : undefined
|
|
|
|
}
|
|
|
|
|
2017-10-27 14:04:48 +08:00
|
|
|
export default {
|
2018-04-08 21:17:20 +08:00
|
|
|
name: 'ARadioGroup',
|
2017-10-27 14:04:48 +08:00
|
|
|
props: {
|
|
|
|
prefixCls: {
|
2018-09-05 21:28:54 +08:00
|
|
|
default: 'ant-radio',
|
2017-10-27 14:04:48 +08:00
|
|
|
type: String,
|
|
|
|
},
|
2018-03-20 19:06:04 +08:00
|
|
|
defaultValue: PropTypes.any,
|
|
|
|
value: PropTypes.any,
|
2017-10-27 14:04:48 +08:00
|
|
|
size: {
|
|
|
|
default: 'default',
|
|
|
|
validator (value) {
|
|
|
|
return ['large', 'default', 'small'].includes(value)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
options: {
|
|
|
|
default: () => [],
|
|
|
|
type: Array,
|
|
|
|
},
|
2017-11-07 11:58:47 +08:00
|
|
|
disabled: Boolean,
|
|
|
|
name: String,
|
2018-09-05 21:28:54 +08:00
|
|
|
buttonStyle: PropTypes.string.def('outline'),
|
2017-10-27 14:04:48 +08:00
|
|
|
},
|
|
|
|
data () {
|
2017-11-02 12:07:20 +08:00
|
|
|
const { value, defaultValue } = this
|
2017-10-27 14:04:48 +08:00
|
|
|
return {
|
2017-11-02 12:07:20 +08:00
|
|
|
stateValue: value || defaultValue,
|
2017-10-27 14:04:48 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
model: {
|
|
|
|
prop: 'value',
|
|
|
|
},
|
2017-11-07 11:58:47 +08:00
|
|
|
provide () {
|
|
|
|
return {
|
|
|
|
radioGroupContext: this,
|
|
|
|
}
|
2017-10-27 14:04:48 +08:00
|
|
|
},
|
2017-11-07 11:58:47 +08:00
|
|
|
computed: {
|
|
|
|
radioOptions () {
|
|
|
|
const { disabled } = this
|
|
|
|
return this.options.map(option => {
|
|
|
|
return typeof option === 'string'
|
|
|
|
? { label: option, value: option }
|
|
|
|
: { ...option, disabled: option.disabled === undefined ? disabled : option.disabled }
|
|
|
|
})
|
|
|
|
},
|
|
|
|
classes () {
|
|
|
|
const { prefixCls, size } = this
|
|
|
|
return {
|
|
|
|
[`${prefixCls}`]: true,
|
|
|
|
[`${prefixCls}-${size}`]: size,
|
|
|
|
}
|
|
|
|
},
|
2017-10-27 14:04:48 +08:00
|
|
|
},
|
|
|
|
methods: {
|
2018-09-05 21:28:54 +08:00
|
|
|
onRadioChange (ev) {
|
|
|
|
const lastValue = this.stateValue
|
|
|
|
const { value } = ev.target
|
|
|
|
if (!hasProp(this, 'value')) {
|
|
|
|
this.stateValue = value
|
|
|
|
}
|
|
|
|
if (value !== lastValue) {
|
|
|
|
this.$emit('input', value)
|
|
|
|
this.$emit('change', ev)
|
2017-11-02 12:07:20 +08:00
|
|
|
}
|
2018-06-17 17:22:26 +08:00
|
|
|
},
|
2017-10-27 14:04:48 +08:00
|
|
|
},
|
|
|
|
watch: {
|
|
|
|
value (val) {
|
2017-11-02 12:07:20 +08:00
|
|
|
this.stateValue = val
|
2017-10-27 14:04:48 +08:00
|
|
|
},
|
|
|
|
},
|
2018-01-15 18:54:26 +08:00
|
|
|
render () {
|
2018-09-05 21:28:54 +08:00
|
|
|
const { mouseenter = noop, mouseleave = noop } = this.$listeners
|
|
|
|
const props = getOptionProps(this)
|
|
|
|
const { prefixCls, options, buttonStyle } = props
|
|
|
|
const groupPrefixCls = `${prefixCls}-group`
|
|
|
|
const classString = classNames(groupPrefixCls, `${groupPrefixCls}-${buttonStyle}`, {
|
|
|
|
[`${groupPrefixCls}-${props.size}`]: props.size,
|
|
|
|
})
|
|
|
|
|
|
|
|
let children = filterEmpty(this.$slots.default)
|
|
|
|
|
|
|
|
// 如果存在 options, 优先使用
|
|
|
|
if (options && options.length > 0) {
|
|
|
|
children = options.map((option, index) => {
|
|
|
|
if (typeof option === 'string') {
|
|
|
|
return (
|
|
|
|
<Radio
|
|
|
|
key={index}
|
|
|
|
prefixCls={prefixCls}
|
|
|
|
disabled={props.disabled}
|
|
|
|
value={option}
|
|
|
|
onChange={this.onRadioChange}
|
|
|
|
checked={this.stateValue === option}
|
|
|
|
>
|
|
|
|
{option}
|
|
|
|
</Radio>
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
return (
|
|
|
|
<Radio
|
|
|
|
key={index}
|
|
|
|
prefixCls={prefixCls}
|
|
|
|
disabled={option.disabled || props.disabled}
|
|
|
|
value={option.value}
|
|
|
|
onChange={this.onRadioChange}
|
|
|
|
checked={this.stateValue === option.value}
|
|
|
|
>
|
|
|
|
{option.label}
|
|
|
|
</Radio>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-01-15 18:54:26 +08:00
|
|
|
return (
|
2018-06-17 17:22:26 +08:00
|
|
|
<div
|
2018-09-05 21:28:54 +08:00
|
|
|
class={classString}
|
|
|
|
onMouseenter={mouseenter}
|
|
|
|
onMouseleave={mouseleave}
|
2018-06-17 17:22:26 +08:00
|
|
|
>
|
2018-09-05 21:28:54 +08:00
|
|
|
{children}
|
2018-01-15 18:54:26 +08:00
|
|
|
</div>
|
|
|
|
)
|
2017-10-27 14:04:48 +08:00
|
|
|
},
|
|
|
|
}
|
2018-03-19 10:16:27 +08:00
|
|
|
|