From 109a386c227093e41dff017f42277526926850c1 Mon Sep 17 00:00:00 2001
From: Amour1688 <31695475+Amour1688@users.noreply.github.com>
Date: Sat, 13 Jun 2020 22:32:10 +0800
Subject: [PATCH] feat: update radio (#2374)
* feat: update radio
* chore: add @babel/plugin-proposal-optional-chaining
---
components/radio/Group.jsx | 75 +++++++++++++------------
components/radio/Radio.jsx | 56 +++++++++++-------
components/radio/RadioButton.jsx | 29 +++++-----
components/radio/index.js | 11 ++--
components/vc-checkbox/src/Checkbox.jsx | 33 +++++------
examples/index.js | 2 +
package.json | 1 +
webpack.config.js | 5 +-
8 files changed, 112 insertions(+), 100 deletions(-)
diff --git a/components/radio/Group.jsx b/components/radio/Group.jsx
index 0b8a6e2d5..18d9cefe4 100644
--- a/components/radio/Group.jsx
+++ b/components/radio/Group.jsx
@@ -1,15 +1,14 @@
+import { provide, inject, nextTick } from 'vue';
import classNames from 'classnames';
import PropTypes from '../_util/vue-types';
import Radio from './Radio';
-import { getOptionProps, filterEmpty, hasProp, getListeners } from '../_util/props-util';
+import { getOptionProps, filterEmpty, hasProp, getSlot } from '../_util/props-util';
import { ConfigConsumerProps } from '../config-provider';
function noop() {}
export default {
name: 'ARadioGroup',
- model: {
- prop: 'value',
- },
+ inheritAttrs: false,
props: {
prefixCls: PropTypes.string,
defaultValue: PropTypes.any,
@@ -35,14 +34,11 @@ export default {
stateValue: value === undefined ? defaultValue : value,
};
},
- provide() {
+ setup() {
return {
- radioGroupContext: this,
+ configProvider: inject('configProvider', ConfigConsumerProps),
};
},
- inject: {
- configProvider: { default: () => ConfigConsumerProps },
- },
computed: {
radioOptions() {
const { disabled } = this;
@@ -52,13 +48,6 @@ export default {
: { ...option, disabled: option.disabled === undefined ? disabled : option.disabled };
});
},
- classes() {
- const { prefixCls, size } = this;
- return {
- [`${prefixCls}`]: true,
- [`${prefixCls}-${size}`]: size,
- };
- },
},
watch: {
value(val) {
@@ -66,6 +55,10 @@ export default {
this.stateValue = val;
},
},
+ created() {
+ this.configProvider = inject('configProvider', ConfigConsumerProps);
+ this.radioGroupContext = provide('radioGroupContext', this);
+ },
methods: {
onRadioChange(ev) {
const lastValue = this.stateValue;
@@ -76,27 +69,32 @@ export default {
// nextTick for https://github.com/vueComponent/ant-design-vue/issues/1280
if (!this.updatingValue && value !== lastValue) {
this.updatingValue = true;
- this.$emit('input', value);
+ this.$emit('update:modelValue', value);
this.$emit('change', ev);
}
- this.$nextTick(() => {
+ nextTick(() => {
this.updatingValue = false;
});
},
},
render() {
- const { mouseenter = noop, mouseleave = noop } = getListeners(this);
+ const { onMouseenter = noop, onMouseleave = noop, class: className, style, id } = this.$attrs;
const props = getOptionProps(this);
const { prefixCls: customizePrefixCls, options, buttonStyle } = props;
const getPrefixCls = this.configProvider.getPrefixCls;
const prefixCls = getPrefixCls('radio', customizePrefixCls);
const groupPrefixCls = `${prefixCls}-group`;
- const classString = classNames(groupPrefixCls, `${groupPrefixCls}-${buttonStyle}`, {
- [`${groupPrefixCls}-${props.size}`]: props.size,
- });
+ const classString = classNames(
+ groupPrefixCls,
+ `${groupPrefixCls}-${buttonStyle}`,
+ {
+ [`${groupPrefixCls}-${props.size}`]: props.size,
+ },
+ className,
+ );
- let children = filterEmpty(this.$slots.default);
+ let children = filterEmpty(getSlot(this));
// 如果存在 options, 优先使用
if (options && options.length > 0) {
@@ -113,24 +111,29 @@ export default {
{option}
);
- } else {
- return (
-
- {option.label}
-
- );
}
+ return (
+
+ {option.label}
+
+ );
});
}
return (
-
+
{children}
);
diff --git a/components/radio/Radio.jsx b/components/radio/Radio.jsx
index e24b6924e..3c563bc09 100644
--- a/components/radio/Radio.jsx
+++ b/components/radio/Radio.jsx
@@ -1,13 +1,15 @@
+import { inject } from 'vue';
import PropTypes from '../_util/vue-types';
import VcCheckbox from '../vc-checkbox';
import classNames from 'classnames';
-import { getOptionProps, getAttrs, getListeners } from '../_util/props-util';
+import { getOptionProps } from '../_util/props-util';
import { ConfigConsumerProps } from '../config-provider';
function noop() {}
export default {
name: 'ARadio',
+ inheritAttrs: false,
model: {
prop: 'checked',
},
@@ -23,9 +25,11 @@ export default {
autoFocus: Boolean,
type: PropTypes.string.def('radio'),
},
- inject: {
- radioGroupContext: { default: undefined },
- configProvider: { default: () => ConfigConsumerProps },
+ setup() {
+ return {
+ configProvider: inject('configProvider', ConfigConsumerProps),
+ radioGroupContext: inject('radioGroupContext'),
+ };
},
methods: {
focus() {
@@ -36,7 +40,7 @@ export default {
},
handleChange(event) {
const targetChecked = event.target.checked;
- this.$emit('input', targetChecked);
+ this.$emit('update:value', targetChecked);
this.$emit('change', event);
},
onChange(e) {
@@ -48,38 +52,48 @@ export default {
},
render() {
- const { $slots, radioGroupContext: radioGroup } = this;
+ const { $slots, radioGroupContext: radioGroup, $attrs } = this;
const props = getOptionProps(this);
- const children = $slots.default;
- const { mouseenter = noop, mouseleave = noop, ...restListeners } = getListeners(this);
+ const {
+ onMouseenter = noop,
+ onMouseleave = noop,
+ class: className,
+ style,
+ ...restAttrs
+ } = $attrs;
const { prefixCls: customizePrefixCls, ...restProps } = props;
const getPrefixCls = this.configProvider.getPrefixCls;
const prefixCls = getPrefixCls('radio', customizePrefixCls);
const radioProps = {
- props: { ...restProps, prefixCls },
- on: restListeners,
- attrs: getAttrs(this),
+ prefixCls,
+ ...restProps,
+ ...restAttrs,
};
if (radioGroup) {
- radioProps.props.name = radioGroup.name;
- radioProps.on.change = this.onChange;
- radioProps.props.checked = props.value === radioGroup.stateValue;
- radioProps.props.disabled = props.disabled || radioGroup.disabled;
+ radioProps.name = radioGroup.name;
+ radioProps.onChange = this.onChange;
+ radioProps.checked = props.value === radioGroup.stateValue;
+ radioProps.disabled = props.disabled || radioGroup.disabled;
} else {
- radioProps.on.change = this.handleChange;
+ radioProps.onChange = this.handleChange;
}
- const wrapperClassString = classNames({
+ const wrapperClassString = classNames(className, {
[`${prefixCls}-wrapper`]: true,
- [`${prefixCls}-wrapper-checked`]: radioProps.props.checked,
- [`${prefixCls}-wrapper-disabled`]: radioProps.props.disabled,
+ [`${prefixCls}-wrapper-checked`]: radioProps.checked,
+ [`${prefixCls}-wrapper-disabled`]: radioProps.disabled,
});
return (
-