2020-06-23 07:45:30 +00:00
|
|
|
import { inject } from 'vue';
|
2020-03-30 16:02:28 +00:00
|
|
|
import LoadingOutlined from '@ant-design/icons-vue/LoadingOutlined';
|
2019-01-12 03:33:27 +00:00
|
|
|
import PropTypes from '../_util/vue-types';
|
2020-06-23 07:45:30 +00:00
|
|
|
import hasProp, { getOptionProps, getComponent } from '../_util/props-util';
|
2019-01-12 03:33:27 +00:00
|
|
|
import VcSwitch from '../vc-switch';
|
|
|
|
import Wave from '../_util/wave';
|
2019-03-18 12:35:24 +00:00
|
|
|
import { ConfigConsumerProps } from '../config-provider';
|
2020-03-07 11:45:13 +00:00
|
|
|
import warning from '../_util/warning';
|
2018-02-27 04:14:29 +00:00
|
|
|
|
2018-09-19 05:21:57 +00:00
|
|
|
const Switch = {
|
2018-04-08 13:17:20 +00:00
|
|
|
name: 'ASwitch',
|
2020-03-07 11:45:13 +00:00
|
|
|
__ANT_SWITCH: true,
|
2020-06-23 07:45:30 +00:00
|
|
|
inheritAttrs: false,
|
2018-02-27 04:14:29 +00:00
|
|
|
props: {
|
2019-03-18 12:35:24 +00:00
|
|
|
prefixCls: PropTypes.string,
|
2018-02-27 04:14:29 +00:00
|
|
|
// size=default and size=large are the same
|
|
|
|
size: PropTypes.oneOf(['small', 'default', 'large']),
|
|
|
|
disabled: PropTypes.bool,
|
|
|
|
checkedChildren: PropTypes.any,
|
|
|
|
unCheckedChildren: PropTypes.any,
|
2020-07-17 09:13:30 +00:00
|
|
|
tabindex: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
|
2018-02-27 04:14:29 +00:00
|
|
|
checked: PropTypes.bool,
|
|
|
|
defaultChecked: PropTypes.bool,
|
2020-07-17 09:36:58 +00:00
|
|
|
autofocus: PropTypes.bool,
|
2018-02-27 04:14:29 +00:00
|
|
|
loading: PropTypes.bool,
|
|
|
|
},
|
2020-06-23 07:45:30 +00:00
|
|
|
setup() {
|
|
|
|
return {
|
|
|
|
configProvider: inject('configProvider', ConfigConsumerProps),
|
|
|
|
};
|
2019-03-18 12:35:24 +00:00
|
|
|
},
|
2018-02-27 04:14:29 +00:00
|
|
|
methods: {
|
2019-01-12 03:33:27 +00:00
|
|
|
focus() {
|
2020-06-23 08:50:02 +00:00
|
|
|
this.refSwitchNode?.focus();
|
2018-02-27 04:14:29 +00:00
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
blur() {
|
2020-06-23 08:50:02 +00:00
|
|
|
this.refSwitchNode?.blur();
|
2020-06-23 07:45:30 +00:00
|
|
|
},
|
|
|
|
saveRef(c) {
|
|
|
|
this.refSwitchNode = c;
|
2018-02-27 04:14:29 +00:00
|
|
|
},
|
|
|
|
},
|
2020-03-07 11:45:13 +00:00
|
|
|
created() {
|
|
|
|
warning(
|
2020-06-23 07:45:30 +00:00
|
|
|
hasProp(this, 'checked') || !('value' in this.$attrs),
|
2020-03-07 11:45:13 +00:00
|
|
|
'Switch',
|
|
|
|
'`value` is not validate prop, do you mean `checked`?',
|
|
|
|
);
|
|
|
|
},
|
2018-02-27 04:14:29 +00:00
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
render() {
|
2019-05-28 03:37:38 +00:00
|
|
|
const { prefixCls: customizePrefixCls, size, loading, disabled, ...restProps } = getOptionProps(
|
|
|
|
this,
|
|
|
|
);
|
2019-09-11 14:35:25 +00:00
|
|
|
const getPrefixCls = this.configProvider.getPrefixCls;
|
2019-03-18 12:35:24 +00:00
|
|
|
const prefixCls = getPrefixCls('switch', customizePrefixCls);
|
2020-06-23 07:45:30 +00:00
|
|
|
const { $attrs } = this;
|
2019-03-18 12:35:24 +00:00
|
|
|
|
2018-02-27 04:14:29 +00:00
|
|
|
const classes = {
|
2020-06-23 07:45:30 +00:00
|
|
|
[$attrs.class]: $attrs.class,
|
2018-02-27 04:14:29 +00:00
|
|
|
[`${prefixCls}-small`]: size === 'small',
|
|
|
|
[`${prefixCls}-loading`]: loading,
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|
2020-06-23 07:45:30 +00:00
|
|
|
const loadingIcon = loading ? <LoadingOutlined class={`${prefixCls}-loading-icon`} /> : null;
|
2018-02-27 04:14:29 +00:00
|
|
|
const switchProps = {
|
2020-06-23 07:45:30 +00:00
|
|
|
...restProps,
|
2020-08-13 09:46:44 +00:00
|
|
|
...$attrs,
|
2020-06-23 07:45:30 +00:00
|
|
|
prefixCls,
|
|
|
|
loadingIcon,
|
|
|
|
checkedChildren: getComponent(this, 'checkedChildren'),
|
|
|
|
unCheckedChildren: getComponent(this, 'unCheckedChildren'),
|
|
|
|
disabled: disabled || loading,
|
2018-02-27 04:14:29 +00:00
|
|
|
class: classes,
|
2020-06-24 15:45:36 +00:00
|
|
|
ref: this.saveRef,
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|
2020-07-07 08:01:12 +00:00
|
|
|
const comp = <VcSwitch {...switchProps} />;
|
|
|
|
return <Wave insertExtraNode>{comp}</Wave>;
|
2018-02-27 04:14:29 +00:00
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|
2018-03-19 02:16:27 +00:00
|
|
|
|
2018-09-19 05:21:57 +00:00
|
|
|
/* istanbul ignore next */
|
2020-06-23 07:45:30 +00:00
|
|
|
Switch.install = function(app) {
|
|
|
|
app.component(Switch.name, Switch);
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|
2018-09-19 05:21:57 +00:00
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
export default Switch;
|