2019-01-12 03:33:27 +00:00
|
|
|
import PropTypes from '../_util/vue-types';
|
|
|
|
import VcCheckbox from '../vc-checkbox';
|
|
|
|
import classNames from 'classnames';
|
|
|
|
import { getOptionProps, getAttrs } 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
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
function noop() {}
|
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,
|
2018-06-17 08:14:49 +00:00
|
|
|
autoFocus: Boolean,
|
2018-09-05 13:28:54 +00:00
|
|
|
type: PropTypes.string.def('radio'),
|
2017-10-27 06:04:48 +00:00
|
|
|
},
|
2017-11-07 03:58:47 +00:00
|
|
|
inject: {
|
|
|
|
radioGroupContext: { default: undefined },
|
2019-09-11 14:35:25 +00:00
|
|
|
configProvider: { default: () => ConfigConsumerProps },
|
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
|
|
|
handleChange(event) {
|
|
|
|
const targetChecked = event.target.checked;
|
|
|
|
this.$emit('input', targetChecked);
|
|
|
|
this.$emit('change', event);
|
2017-10-27 06:04:48 +00:00
|
|
|
},
|
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
|
|
|
},
|
2019-09-18 12:03:13 +00:00
|
|
|
onChange(e) {
|
|
|
|
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() {
|
|
|
|
const { $slots, $listeners, radioGroupContext: radioGroup } = this;
|
|
|
|
const props = getOptionProps(this);
|
|
|
|
const children = $slots.default;
|
|
|
|
const { mouseenter = noop, mouseleave = noop, ...restListeners } = $listeners;
|
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 = {
|
|
|
|
props: { ...restProps, prefixCls },
|
|
|
|
on: restListeners,
|
|
|
|
attrs: getAttrs(this),
|
|
|
|
};
|
2018-09-05 13:28:54 +00:00
|
|
|
|
|
|
|
if (radioGroup) {
|
2019-01-12 03:33:27 +00:00
|
|
|
radioProps.props.name = radioGroup.name;
|
2019-09-18 12:03:13 +00:00
|
|
|
radioProps.on.change = this.onChange;
|
2019-01-12 03:33:27 +00:00
|
|
|
radioProps.props.checked = props.value === radioGroup.stateValue;
|
|
|
|
radioProps.props.disabled = props.disabled || radioGroup.disabled;
|
2018-09-05 13:28:54 +00:00
|
|
|
} else {
|
2019-01-12 03:33:27 +00:00
|
|
|
radioProps.on.change = this.handleChange;
|
2018-06-17 09:22:26 +00:00
|
|
|
}
|
2018-09-05 13:28:54 +00:00
|
|
|
const wrapperClassString = classNames({
|
2018-06-17 09:46:09 +00:00
|
|
|
[`${prefixCls}-wrapper`]: true,
|
2018-09-05 13:28:54 +00:00
|
|
|
[`${prefixCls}-wrapper-checked`]: radioProps.props.checked,
|
|
|
|
[`${prefixCls}-wrapper-disabled`]: radioProps.props.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 (
|
2019-01-12 03:33:27 +00:00
|
|
|
<label class={wrapperClassString} onMouseenter={mouseenter} onMouseleave={mouseleave}>
|
|
|
|
<VcCheckbox {...radioProps} ref="vcCheckbox" />
|
2018-09-05 13:28:54 +00:00
|
|
|
{children !== undefined ? <span>{children}</span> : null}
|
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
|
|
|
};
|