2020-06-13 22:32:10 +08:00
|
|
|
import { inject } from 'vue';
|
2019-01-12 11:33:27 +08:00
|
|
|
import PropTypes from '../_util/vue-types';
|
|
|
|
import VcCheckbox from '../vc-checkbox';
|
|
|
|
import classNames from 'classnames';
|
2020-06-13 22:32:10 +08:00
|
|
|
import { getOptionProps } 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',
|
2020-06-13 22:32:10 +08:00
|
|
|
inheritAttrs: false,
|
2019-02-01 17:23:00 +08:00
|
|
|
model: {
|
|
|
|
prop: 'checked',
|
|
|
|
},
|
2017-10-27 14:04:48 +08:00
|
|
|
props: {
|
2019-09-18 20:03:13 +08:00
|
|
|
prefixCls: PropTypes.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
|
|
|
},
|
2020-06-13 22:32:10 +08:00
|
|
|
setup() {
|
|
|
|
return {
|
|
|
|
configProvider: inject('configProvider', ConfigConsumerProps),
|
|
|
|
radioGroupContext: inject('radioGroupContext'),
|
|
|
|
};
|
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
|
|
|
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
|
|
|
},
|
2020-03-07 19:45:13 +08:00
|
|
|
handleChange(event) {
|
|
|
|
const targetChecked = event.target.checked;
|
2020-06-13 22:32:10 +08:00
|
|
|
this.$emit('update:value', targetChecked);
|
2020-03-07 19:45:13 +08:00
|
|
|
this.$emit('change', event);
|
|
|
|
},
|
2019-09-18 20:03:13 +08:00
|
|
|
onChange(e) {
|
|
|
|
this.$emit('change', e);
|
|
|
|
if (this.radioGroupContext && this.radioGroupContext.onRadioChange) {
|
|
|
|
this.radioGroupContext.onRadioChange(e);
|
|
|
|
}
|
|
|
|
},
|
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() {
|
2020-06-13 22:32:10 +08:00
|
|
|
const { $slots, radioGroupContext: radioGroup, $attrs } = this;
|
2019-01-12 11:33:27 +08:00
|
|
|
const props = getOptionProps(this);
|
2020-06-13 22:32:10 +08:00
|
|
|
const {
|
|
|
|
onMouseenter = noop,
|
|
|
|
onMouseleave = noop,
|
|
|
|
class: className,
|
|
|
|
style,
|
|
|
|
...restAttrs
|
|
|
|
} = $attrs;
|
2019-03-18 20:00:00 +08:00
|
|
|
const { prefixCls: customizePrefixCls, ...restProps } = 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 radioProps = {
|
2020-06-13 22:32:10 +08:00
|
|
|
prefixCls,
|
|
|
|
...restProps,
|
|
|
|
...restAttrs,
|
2019-01-12 11:33:27 +08:00
|
|
|
};
|
2018-09-05 21:28:54 +08:00
|
|
|
|
|
|
|
if (radioGroup) {
|
2020-06-13 22:32:10 +08:00
|
|
|
radioProps.name = radioGroup.name;
|
|
|
|
radioProps.onChange = this.onChange;
|
|
|
|
radioProps.checked = props.value === radioGroup.stateValue;
|
|
|
|
radioProps.disabled = props.disabled || radioGroup.disabled;
|
2018-09-05 21:28:54 +08:00
|
|
|
} else {
|
2020-06-13 22:32:10 +08:00
|
|
|
radioProps.onChange = this.handleChange;
|
2018-06-17 17:22:26 +08:00
|
|
|
}
|
2020-06-13 22:32:10 +08:00
|
|
|
const wrapperClassString = classNames(className, {
|
2018-06-17 17:46:09 +08:00
|
|
|
[`${prefixCls}-wrapper`]: true,
|
2020-06-13 22:32:10 +08:00
|
|
|
[`${prefixCls}-wrapper-checked`]: radioProps.checked,
|
|
|
|
[`${prefixCls}-wrapper-disabled`]: radioProps.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 (
|
2020-06-13 22:32:10 +08:00
|
|
|
<label
|
|
|
|
class={wrapperClassString}
|
|
|
|
style={style}
|
|
|
|
onMouseenter={onMouseenter}
|
|
|
|
onMouseleave={onMouseleave}
|
|
|
|
>
|
2019-01-12 11:33:27 +08:00
|
|
|
<VcCheckbox {...radioProps} ref="vcCheckbox" />
|
2020-06-13 22:32:10 +08:00
|
|
|
{$slots.default !== undefined ? <span>{$slots.default()}</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
|
|
|
};
|