2019-01-12 11:33:27 +08: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 20:00:00 +08:00
|
|
|
import { ConfigConsumerProps } from '../config-provider';
|
2018-09-05 21:28:54 +08:00
|
|
|
|
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: 'ARadio',
|
2019-02-01 17:23:00 +08:00
|
|
|
model: {
|
|
|
|
prop: 'checked',
|
|
|
|
},
|
2017-10-27 14:04:48 +08:00
|
|
|
props: {
|
|
|
|
prefixCls: {
|
|
|
|
type: String,
|
|
|
|
},
|
2017-11-02 12:07:20 +08:00
|
|
|
defaultChecked: Boolean,
|
|
|
|
checked: { type: Boolean, default: undefined },
|
2017-10-27 14:04:48 +08:00
|
|
|
disabled: Boolean,
|
|
|
|
isGroup: Boolean,
|
2018-03-20 19:06:04 +08:00
|
|
|
value: PropTypes.any,
|
2017-10-27 14:04:48 +08:00
|
|
|
name: String,
|
2018-06-01 21:25:16 +08:00
|
|
|
id: String,
|
2018-06-17 16:14:49 +08:00
|
|
|
autoFocus: Boolean,
|
2018-09-05 21:28:54 +08:00
|
|
|
type: PropTypes.string.def('radio'),
|
2017-10-27 14:04:48 +08:00
|
|
|
},
|
2017-11-07 11:58:47 +08:00
|
|
|
inject: {
|
|
|
|
radioGroupContext: { default: undefined },
|
2019-03-18 20:00:00 +08:00
|
|
|
configProvider: { default: () => ({}) },
|
2017-11-07 11:58:47 +08:00
|
|
|
},
|
2017-10-27 14:04:48 +08:00
|
|
|
methods: {
|
2019-01-12 11:33:27 +08:00
|
|
|
handleChange(event) {
|
|
|
|
const targetChecked = event.target.checked;
|
|
|
|
this.$emit('input', targetChecked);
|
|
|
|
this.$emit('change', event);
|
2017-10-27 14:04:48 +08:00
|
|
|
},
|
2019-01-12 11:33:27 +08:00
|
|
|
focus() {
|
|
|
|
this.$refs.vcCheckbox.focus();
|
2018-06-17 16:14:49 +08:00
|
|
|
},
|
2019-01-12 11:33:27 +08:00
|
|
|
blur() {
|
|
|
|
this.$refs.vcCheckbox.blur();
|
2017-11-07 11:58:47 +08:00
|
|
|
},
|
2017-11-02 12:07:20 +08:00
|
|
|
},
|
2018-09-05 21:28:54 +08:00
|
|
|
|
2019-01-12 11:33:27 +08: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 20:00:00 +08:00
|
|
|
const { prefixCls: customizePrefixCls, ...restProps } = props;
|
|
|
|
const getPrefixCls = this.configProvider.getPrefixCls || ConfigConsumerProps.getPrefixCls;
|
|
|
|
const prefixCls = getPrefixCls('radio', customizePrefixCls);
|
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
const radioProps = {
|
|
|
|
props: { ...restProps, prefixCls },
|
|
|
|
on: restListeners,
|
|
|
|
attrs: getAttrs(this),
|
|
|
|
};
|
2018-09-05 21:28:54 +08:00
|
|
|
|
|
|
|
if (radioGroup) {
|
2019-01-12 11:33:27 +08:00
|
|
|
radioProps.props.name = radioGroup.name;
|
|
|
|
radioProps.on.change = radioGroup.onRadioChange;
|
|
|
|
radioProps.props.checked = props.value === radioGroup.stateValue;
|
|
|
|
radioProps.props.disabled = props.disabled || radioGroup.disabled;
|
2018-09-05 21:28:54 +08:00
|
|
|
} else {
|
2019-01-12 11:33:27 +08:00
|
|
|
radioProps.on.change = this.handleChange;
|
2018-06-17 17:22:26 +08:00
|
|
|
}
|
2018-09-05 21:28:54 +08:00
|
|
|
const wrapperClassString = classNames({
|
2018-06-17 17:46:09 +08:00
|
|
|
[`${prefixCls}-wrapper`]: true,
|
2018-09-05 21:28:54 +08:00
|
|
|
[`${prefixCls}-wrapper-checked`]: radioProps.props.checked,
|
|
|
|
[`${prefixCls}-wrapper-disabled`]: radioProps.props.disabled,
|
2019-01-12 11:33:27 +08:00
|
|
|
});
|
2018-06-17 17:46:09 +08:00
|
|
|
|
2018-03-19 09:43:31 +08:00
|
|
|
return (
|
2019-01-12 11:33:27 +08:00
|
|
|
<label class={wrapperClassString} onMouseenter={mouseenter} onMouseleave={mouseleave}>
|
|
|
|
<VcCheckbox {...radioProps} ref="vcCheckbox" />
|
2018-09-05 21:28:54 +08:00
|
|
|
{children !== undefined ? <span>{children}</span> : null}
|
2018-03-19 09:43:31 +08:00
|
|
|
</label>
|
2019-01-12 11:33:27 +08:00
|
|
|
);
|
2018-03-19 09:43:31 +08:00
|
|
|
},
|
2019-01-12 11:33:27 +08:00
|
|
|
};
|