2019-01-12 11:33:27 +08:00
|
|
|
import classNames from 'classnames';
|
|
|
|
import PropTypes from '../_util/vue-types';
|
|
|
|
import Radio from './Radio';
|
2020-01-19 16:58:38 +08:00
|
|
|
import { getOptionProps, filterEmpty, hasProp, getListeners } from '../_util/props-util';
|
2019-03-18 20:00:00 +08:00
|
|
|
import { ConfigConsumerProps } from '../config-provider';
|
2019-01-12 11:33:27 +08:00
|
|
|
function noop() {}
|
2018-09-05 21:28:54 +08:00
|
|
|
|
2017-10-27 14:04:48 +08:00
|
|
|
export default {
|
2018-04-08 21:17:20 +08:00
|
|
|
name: 'ARadioGroup',
|
2019-02-01 17:23:00 +08:00
|
|
|
model: {
|
|
|
|
prop: 'value',
|
|
|
|
},
|
2017-10-27 14:04:48 +08:00
|
|
|
props: {
|
2019-09-18 20:03:13 +08:00
|
|
|
prefixCls: PropTypes.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',
|
2019-01-12 11:33:27 +08:00
|
|
|
validator(value) {
|
|
|
|
return ['large', 'default', 'small'].includes(value);
|
2017-10-27 14:04:48 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
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
|
|
|
},
|
2019-01-12 11:33:27 +08:00
|
|
|
data() {
|
|
|
|
const { value, defaultValue } = this;
|
2019-10-18 13:05:35 +08:00
|
|
|
this.updatingValue = false;
|
2017-10-27 14:04:48 +08:00
|
|
|
return {
|
2018-10-24 12:51:46 +08:00
|
|
|
stateValue: value === undefined ? defaultValue : value,
|
2019-01-12 11:33:27 +08:00
|
|
|
};
|
2017-10-27 14:04:48 +08:00
|
|
|
},
|
2019-01-12 11:33:27 +08:00
|
|
|
provide() {
|
2017-11-07 11:58:47 +08:00
|
|
|
return {
|
|
|
|
radioGroupContext: this,
|
2019-01-12 11:33:27 +08:00
|
|
|
};
|
2017-10-27 14:04:48 +08:00
|
|
|
},
|
2019-03-18 20:00:00 +08:00
|
|
|
inject: {
|
2019-09-11 22:35:25 +08:00
|
|
|
configProvider: { default: () => ConfigConsumerProps },
|
2019-03-18 20:00:00 +08:00
|
|
|
},
|
2017-11-07 11:58:47 +08:00
|
|
|
computed: {
|
2019-01-12 11:33:27 +08:00
|
|
|
radioOptions() {
|
|
|
|
const { disabled } = this;
|
2017-11-07 11:58:47 +08:00
|
|
|
return this.options.map(option => {
|
|
|
|
return typeof option === 'string'
|
|
|
|
? { label: option, value: option }
|
2019-01-12 11:33:27 +08:00
|
|
|
: { ...option, disabled: option.disabled === undefined ? disabled : option.disabled };
|
|
|
|
});
|
2017-11-07 11:58:47 +08:00
|
|
|
},
|
2019-01-12 11:33:27 +08:00
|
|
|
classes() {
|
|
|
|
const { prefixCls, size } = this;
|
2017-11-07 11:58:47 +08:00
|
|
|
return {
|
|
|
|
[`${prefixCls}`]: true,
|
|
|
|
[`${prefixCls}-${size}`]: size,
|
2019-01-12 11:33:27 +08:00
|
|
|
};
|
2017-11-07 11:58:47 +08:00
|
|
|
},
|
2017-10-27 14:04:48 +08:00
|
|
|
},
|
2019-02-01 17:23:00 +08:00
|
|
|
watch: {
|
|
|
|
value(val) {
|
2019-10-18 13:05:35 +08:00
|
|
|
this.updatingValue = false;
|
2019-02-01 17:23:00 +08:00
|
|
|
this.stateValue = val;
|
|
|
|
},
|
|
|
|
},
|
2017-10-27 14:04:48 +08:00
|
|
|
methods: {
|
2019-01-12 11:33:27 +08:00
|
|
|
onRadioChange(ev) {
|
|
|
|
const lastValue = this.stateValue;
|
|
|
|
const { value } = ev.target;
|
2018-09-05 21:28:54 +08:00
|
|
|
if (!hasProp(this, 'value')) {
|
2019-01-12 11:33:27 +08:00
|
|
|
this.stateValue = value;
|
2018-09-05 21:28:54 +08:00
|
|
|
}
|
2019-10-16 14:47:49 +08:00
|
|
|
// nextTick for https://github.com/vueComponent/ant-design-vue/issues/1280
|
2019-10-18 13:05:35 +08:00
|
|
|
if (!this.updatingValue && value !== lastValue) {
|
|
|
|
this.updatingValue = true;
|
|
|
|
this.$emit('input', value);
|
|
|
|
this.$emit('change', ev);
|
|
|
|
}
|
2019-10-16 14:47:49 +08:00
|
|
|
this.$nextTick(() => {
|
2019-10-18 13:05:35 +08:00
|
|
|
this.updatingValue = false;
|
2019-10-16 14:47:49 +08:00
|
|
|
});
|
2018-06-17 17:22:26 +08:00
|
|
|
},
|
2017-10-27 14:04:48 +08:00
|
|
|
},
|
2019-01-12 11:33:27 +08:00
|
|
|
render() {
|
2020-01-19 16:58:38 +08:00
|
|
|
const { mouseenter = noop, mouseleave = noop } = getListeners(this);
|
2019-01-12 11:33:27 +08:00
|
|
|
const props = getOptionProps(this);
|
2019-03-18 20:00:00 +08:00
|
|
|
const { prefixCls: customizePrefixCls, options, buttonStyle } = props;
|
2019-09-11 22:35:25 +08:00
|
|
|
const getPrefixCls = this.configProvider.getPrefixCls;
|
2019-03-18 20:00:00 +08:00
|
|
|
const prefixCls = getPrefixCls('radio', customizePrefixCls);
|
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
const groupPrefixCls = `${prefixCls}-group`;
|
2018-09-05 21:28:54 +08:00
|
|
|
const classString = classNames(groupPrefixCls, `${groupPrefixCls}-${buttonStyle}`, {
|
|
|
|
[`${groupPrefixCls}-${props.size}`]: props.size,
|
2019-01-12 11:33:27 +08:00
|
|
|
});
|
2018-09-05 21:28:54 +08:00
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
let children = filterEmpty(this.$slots.default);
|
2018-09-05 21:28:54 +08:00
|
|
|
|
|
|
|
// 如果存在 options, 优先使用
|
|
|
|
if (options && options.length > 0) {
|
2020-03-18 14:52:02 +08:00
|
|
|
children = options.map(option => {
|
2018-09-05 21:28:54 +08:00
|
|
|
if (typeof option === 'string') {
|
|
|
|
return (
|
|
|
|
<Radio
|
2020-03-07 19:45:13 +08:00
|
|
|
key={option}
|
2018-09-05 21:28:54 +08:00
|
|
|
prefixCls={prefixCls}
|
|
|
|
disabled={props.disabled}
|
|
|
|
value={option}
|
|
|
|
checked={this.stateValue === option}
|
|
|
|
>
|
|
|
|
{option}
|
|
|
|
</Radio>
|
2019-01-12 11:33:27 +08:00
|
|
|
);
|
2018-09-05 21:28:54 +08:00
|
|
|
} else {
|
|
|
|
return (
|
|
|
|
<Radio
|
2020-03-07 19:45:13 +08:00
|
|
|
key={`radio-group-value-options-${option.value}`}
|
2018-09-05 21:28:54 +08:00
|
|
|
prefixCls={prefixCls}
|
|
|
|
disabled={option.disabled || props.disabled}
|
|
|
|
value={option.value}
|
|
|
|
checked={this.stateValue === option.value}
|
|
|
|
>
|
|
|
|
{option.label}
|
|
|
|
</Radio>
|
2019-01-12 11:33:27 +08:00
|
|
|
);
|
2018-09-05 21:28:54 +08:00
|
|
|
}
|
2019-01-12 11:33:27 +08:00
|
|
|
});
|
2018-09-05 21:28:54 +08:00
|
|
|
}
|
|
|
|
|
2018-01-15 18:54:26 +08:00
|
|
|
return (
|
2019-01-12 11:33:27 +08:00
|
|
|
<div class={classString} onMouseenter={mouseenter} onMouseleave={mouseleave}>
|
2018-09-05 21:28:54 +08:00
|
|
|
{children}
|
2018-01-15 18:54:26 +08:00
|
|
|
</div>
|
2019-01-12 11:33:27 +08:00
|
|
|
);
|
2017-10-27 14:04:48 +08:00
|
|
|
},
|
2019-01-12 11:33:27 +08:00
|
|
|
};
|