2020-10-13 11:02:03 +00:00
|
|
|
import { provide, inject, nextTick, defineComponent } from 'vue';
|
2020-08-31 08:53:19 +00:00
|
|
|
import classNames from '../_util/classNames';
|
2019-01-12 03:33:27 +00:00
|
|
|
import PropTypes from '../_util/vue-types';
|
|
|
|
import Radio from './Radio';
|
2020-06-13 14:32:10 +00:00
|
|
|
import { getOptionProps, filterEmpty, hasProp, getSlot } from '../_util/props-util';
|
2020-09-30 02:47:18 +00:00
|
|
|
import { defaultConfigProvider } from '../config-provider';
|
2020-10-20 08:58:08 +00:00
|
|
|
import { tuple } from '../_util/type';
|
2021-06-26 01:35:40 +00:00
|
|
|
import type { RadioChangeEvent } from './interface';
|
2018-09-05 13:28:54 +00:00
|
|
|
|
2020-10-13 11:02:03 +00:00
|
|
|
export default defineComponent({
|
2018-04-08 13:17:20 +00:00
|
|
|
name: 'ARadioGroup',
|
2017-10-27 06:04:48 +00:00
|
|
|
props: {
|
2019-09-18 12:03:13 +00:00
|
|
|
prefixCls: PropTypes.string,
|
2018-03-20 11:06:04 +00:00
|
|
|
defaultValue: PropTypes.any,
|
|
|
|
value: PropTypes.any,
|
2020-10-20 08:58:08 +00:00
|
|
|
size: PropTypes.oneOf(tuple('large', 'default', 'small')).def('default'),
|
|
|
|
options: PropTypes.array,
|
2020-10-11 13:31:57 +00:00
|
|
|
disabled: PropTypes.looseBool,
|
2020-10-20 08:58:08 +00:00
|
|
|
name: PropTypes.string,
|
2018-09-05 13:28:54 +00:00
|
|
|
buttonStyle: PropTypes.string.def('outline'),
|
2020-06-13 14:49:32 +00:00
|
|
|
onChange: PropTypes.func,
|
2017-10-27 06:04:48 +00:00
|
|
|
},
|
2020-10-20 08:58:08 +00:00
|
|
|
emits: ['update:value', 'change'],
|
2020-06-13 14:32:10 +00:00
|
|
|
setup() {
|
2017-11-07 03:58:47 +00:00
|
|
|
return {
|
2020-10-20 08:58:08 +00:00
|
|
|
updatingValue: false,
|
2020-09-30 02:47:18 +00:00
|
|
|
configProvider: inject('configProvider', defaultConfigProvider),
|
2020-10-20 08:58:08 +00:00
|
|
|
radioGroupContext: null,
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|
2017-10-27 06:04:48 +00:00
|
|
|
},
|
2020-11-07 07:37:17 +00:00
|
|
|
data() {
|
|
|
|
const { value, defaultValue } = this;
|
|
|
|
return {
|
|
|
|
stateValue: value === undefined ? defaultValue : value,
|
|
|
|
};
|
|
|
|
},
|
|
|
|
watch: {
|
|
|
|
value(val) {
|
|
|
|
this.updatingValue = false;
|
|
|
|
this.stateValue = val;
|
|
|
|
},
|
|
|
|
},
|
2020-10-20 08:58:08 +00: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 };
|
|
|
|
// });
|
|
|
|
// },
|
|
|
|
// },
|
|
|
|
created() {
|
|
|
|
this.radioGroupContext = provide('radioGroupContext', this);
|
2017-10-27 06:04:48 +00:00
|
|
|
},
|
|
|
|
methods: {
|
2020-10-20 08:58:08 +00:00
|
|
|
onRadioChange(ev: RadioChangeEvent) {
|
2019-01-12 03:33:27 +00:00
|
|
|
const lastValue = this.stateValue;
|
|
|
|
const { value } = ev.target;
|
2018-09-05 13:28:54 +00:00
|
|
|
if (!hasProp(this, 'value')) {
|
2019-01-12 03:33:27 +00:00
|
|
|
this.stateValue = value;
|
2018-09-05 13:28:54 +00:00
|
|
|
}
|
2019-10-16 06:47:49 +00:00
|
|
|
// nextTick for https://github.com/vueComponent/ant-design-vue/issues/1280
|
2019-10-18 05:05:35 +00:00
|
|
|
if (!this.updatingValue && value !== lastValue) {
|
|
|
|
this.updatingValue = true;
|
2020-06-13 14:49:32 +00:00
|
|
|
this.$emit('update:value', value);
|
2019-10-18 05:05:35 +00:00
|
|
|
this.$emit('change', ev);
|
|
|
|
}
|
2020-06-13 14:32:10 +00:00
|
|
|
nextTick(() => {
|
2019-10-18 05:05:35 +00:00
|
|
|
this.updatingValue = false;
|
2019-10-16 06:47:49 +00:00
|
|
|
});
|
2018-06-17 09:22:26 +00:00
|
|
|
},
|
2017-10-27 06:04:48 +00:00
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
render() {
|
|
|
|
const props = getOptionProps(this);
|
2019-03-18 12:00:00 +00:00
|
|
|
const { prefixCls: customizePrefixCls, options, buttonStyle } = props;
|
2020-10-20 08:58:08 +00:00
|
|
|
const { getPrefixCls } = this.configProvider;
|
2019-03-18 12:00:00 +00:00
|
|
|
const prefixCls = getPrefixCls('radio', customizePrefixCls);
|
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
const groupPrefixCls = `${prefixCls}-group`;
|
2020-06-20 14:22:00 +00:00
|
|
|
const classString = classNames(groupPrefixCls, `${groupPrefixCls}-${buttonStyle}`, {
|
|
|
|
[`${groupPrefixCls}-${props.size}`]: props.size,
|
|
|
|
});
|
2018-09-05 13:28:54 +00:00
|
|
|
|
2020-06-13 14:32:10 +00:00
|
|
|
let children = filterEmpty(getSlot(this));
|
2018-09-05 13:28:54 +00:00
|
|
|
|
|
|
|
// 如果存在 options, 优先使用
|
|
|
|
if (options && options.length > 0) {
|
2020-03-18 06:52:02 +00:00
|
|
|
children = options.map(option => {
|
2018-09-05 13:28:54 +00:00
|
|
|
if (typeof option === 'string') {
|
|
|
|
return (
|
|
|
|
<Radio
|
2020-03-07 11:45:13 +00:00
|
|
|
key={option}
|
2018-09-05 13:28:54 +00:00
|
|
|
prefixCls={prefixCls}
|
|
|
|
disabled={props.disabled}
|
|
|
|
value={option}
|
|
|
|
checked={this.stateValue === option}
|
|
|
|
>
|
|
|
|
{option}
|
|
|
|
</Radio>
|
2019-01-12 03:33:27 +00:00
|
|
|
);
|
2018-09-05 13:28:54 +00:00
|
|
|
}
|
2020-06-13 14:32:10 +00:00
|
|
|
return (
|
|
|
|
<Radio
|
|
|
|
key={`radio-group-value-options-${option.value}`}
|
|
|
|
prefixCls={prefixCls}
|
|
|
|
disabled={option.disabled || props.disabled}
|
|
|
|
value={option.value}
|
|
|
|
checked={this.stateValue === option.value}
|
|
|
|
>
|
|
|
|
{option.label}
|
|
|
|
</Radio>
|
|
|
|
);
|
2019-01-12 03:33:27 +00:00
|
|
|
});
|
2018-09-05 13:28:54 +00:00
|
|
|
}
|
|
|
|
|
2020-06-20 14:22:00 +00:00
|
|
|
return <div class={classString}>{children}</div>;
|
2017-10-27 06:04:48 +00:00
|
|
|
},
|
2020-10-13 11:02:03 +00:00
|
|
|
});
|