2020-06-13 14:32:10 +00:00
|
|
|
import { inject } from 'vue';
|
2019-01-12 03:33:27 +00:00
|
|
|
import PropTypes from '../_util/vue-types';
|
|
|
|
import VcCheckbox from '../vc-checkbox';
|
2020-08-31 08:53:19 +00:00
|
|
|
import classNames from '../_util/classNames';
|
2020-06-13 14:32:10 +00:00
|
|
|
import { getOptionProps } from '../_util/props-util';
|
2019-03-18 12:00:00 +00:00
|
|
|
import { ConfigConsumerProps } from '../config-provider';
|
2018-09-05 13:28:54 +00:00
|
|
|
|
2017-10-27 06:04:48 +00:00
|
|
|
export default {
|
2018-04-08 13:17:20 +00:00
|
|
|
name: 'ARadio',
|
2019-02-01 09:23:00 +00:00
|
|
|
model: {
|
|
|
|
prop: 'checked',
|
|
|
|
},
|
2017-10-27 06:04:48 +00:00
|
|
|
props: {
|
2019-09-18 12:03:13 +00:00
|
|
|
prefixCls: PropTypes.string,
|
2017-11-02 04:07:20 +00:00
|
|
|
defaultChecked: Boolean,
|
|
|
|
checked: { type: Boolean, default: undefined },
|
2017-10-27 06:04:48 +00:00
|
|
|
disabled: Boolean,
|
|
|
|
isGroup: Boolean,
|
2018-03-20 11:06:04 +00:00
|
|
|
value: PropTypes.any,
|
2017-10-27 06:04:48 +00:00
|
|
|
name: String,
|
2018-06-01 13:25:16 +00:00
|
|
|
id: String,
|
2020-07-17 09:36:58 +00:00
|
|
|
autofocus: Boolean,
|
2018-09-05 13:28:54 +00:00
|
|
|
type: PropTypes.string.def('radio'),
|
2020-06-13 14:49:32 +00:00
|
|
|
onChange: PropTypes.func,
|
2020-08-14 09:04:32 +00:00
|
|
|
onFocus: PropTypes.func,
|
|
|
|
onBlur: PropTypes.func,
|
2020-08-03 08:47:12 +00:00
|
|
|
'onUpdate:checked': PropTypes.func,
|
|
|
|
'onUpdate:value': PropTypes.func,
|
2017-10-27 06:04:48 +00:00
|
|
|
},
|
2020-06-13 14:32:10 +00:00
|
|
|
setup() {
|
|
|
|
return {
|
|
|
|
configProvider: inject('configProvider', ConfigConsumerProps),
|
2020-08-03 08:47:12 +00:00
|
|
|
radioGroupContext: inject('radioGroupContext', null),
|
2020-06-13 14:32:10 +00:00
|
|
|
};
|
2017-11-07 03:58:47 +00:00
|
|
|
},
|
2017-10-27 06:04:48 +00:00
|
|
|
methods: {
|
2019-01-12 03:33:27 +00:00
|
|
|
focus() {
|
|
|
|
this.$refs.vcCheckbox.focus();
|
2018-06-17 08:14:49 +00:00
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
blur() {
|
|
|
|
this.$refs.vcCheckbox.blur();
|
2017-11-07 03:58:47 +00:00
|
|
|
},
|
2020-03-07 11:45:13 +00:00
|
|
|
handleChange(event) {
|
|
|
|
const targetChecked = event.target.checked;
|
2020-08-03 08:47:12 +00:00
|
|
|
this.$emit('update:checked', targetChecked);
|
2020-06-13 14:32:10 +00:00
|
|
|
this.$emit('update:value', targetChecked);
|
2020-03-07 11:45:13 +00:00
|
|
|
this.$emit('change', event);
|
|
|
|
},
|
2020-06-13 14:49:32 +00:00
|
|
|
onChange2(e) {
|
2019-09-18 12:03:13 +00:00
|
|
|
this.$emit('change', e);
|
|
|
|
if (this.radioGroupContext && this.radioGroupContext.onRadioChange) {
|
|
|
|
this.radioGroupContext.onRadioChange(e);
|
|
|
|
}
|
|
|
|
},
|
2017-11-02 04:07:20 +00:00
|
|
|
},
|
2018-09-05 13:28:54 +00:00
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
render() {
|
2020-06-13 14:49:32 +00:00
|
|
|
const { $slots, radioGroupContext: radioGroup } = this;
|
2019-01-12 03:33:27 +00:00
|
|
|
const props = getOptionProps(this);
|
2019-03-18 12:00:00 +00:00
|
|
|
const { prefixCls: customizePrefixCls, ...restProps } = props;
|
2019-09-11 14:35:25 +00:00
|
|
|
const getPrefixCls = this.configProvider.getPrefixCls;
|
2019-03-18 12:00:00 +00:00
|
|
|
const prefixCls = getPrefixCls('radio', customizePrefixCls);
|
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
const radioProps = {
|
2020-06-13 14:32:10 +00:00
|
|
|
prefixCls,
|
|
|
|
...restProps,
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|
2018-09-05 13:28:54 +00:00
|
|
|
|
|
|
|
if (radioGroup) {
|
2020-06-13 14:32:10 +00:00
|
|
|
radioProps.name = radioGroup.name;
|
2020-06-13 14:49:32 +00:00
|
|
|
radioProps.onChange = this.onChange2;
|
2020-06-13 14:32:10 +00:00
|
|
|
radioProps.checked = props.value === radioGroup.stateValue;
|
|
|
|
radioProps.disabled = props.disabled || radioGroup.disabled;
|
2018-09-05 13:28:54 +00:00
|
|
|
} else {
|
2020-06-13 14:32:10 +00:00
|
|
|
radioProps.onChange = this.handleChange;
|
2018-06-17 09:22:26 +00:00
|
|
|
}
|
2020-06-20 14:22:00 +00:00
|
|
|
const wrapperClassString = classNames({
|
2018-06-17 09:46:09 +00:00
|
|
|
[`${prefixCls}-wrapper`]: true,
|
2020-06-13 14:32:10 +00:00
|
|
|
[`${prefixCls}-wrapper-checked`]: radioProps.checked,
|
|
|
|
[`${prefixCls}-wrapper-disabled`]: radioProps.disabled,
|
2019-01-12 03:33:27 +00:00
|
|
|
});
|
2018-06-17 09:46:09 +00:00
|
|
|
|
2018-03-19 01:43:31 +00:00
|
|
|
return (
|
2020-06-20 14:22:00 +00:00
|
|
|
<label class={wrapperClassString}>
|
2019-01-12 03:33:27 +00:00
|
|
|
<VcCheckbox {...radioProps} ref="vcCheckbox" />
|
2020-06-13 14:49:32 +00:00
|
|
|
{$slots.default && <span>{$slots.default()}</span>}
|
2018-03-19 01:43:31 +00:00
|
|
|
</label>
|
2019-01-12 03:33:27 +00:00
|
|
|
);
|
2018-03-19 01:43:31 +00:00
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|