ant-design-vue/components/vc-switch/Switch.jsx

124 lines
2.8 KiB
Vue
Raw Normal View History

2019-01-12 11:33:27 +08:00
import { switchPropTypes } from './PropTypes';
import BaseMixin from '../_util/BaseMixin';
import { hasProp, getOptionProps, getComponent } from '../_util/props-util';
2018-02-27 12:14:29 +08:00
// function noop () {
// }
export default {
2019-02-01 17:23:00 +08:00
name: 'VcSwitch',
2018-02-27 12:14:29 +08:00
mixins: [BaseMixin],
inheritAttrs: false,
2018-02-27 12:14:29 +08:00
props: {
...switchPropTypes,
prefixCls: switchPropTypes.prefixCls.def('rc-switch'),
// onChange: switchPropTypes.onChange.def(noop),
// onClick: switchPropTypes.onClick.def(noop),
},
2019-01-12 11:33:27 +08:00
data() {
let checked = false;
2018-02-27 12:14:29 +08:00
if (hasProp(this, 'checked')) {
2019-01-12 11:33:27 +08:00
checked = !!this.checked;
2018-02-27 12:14:29 +08:00
} else {
2019-01-12 11:33:27 +08:00
checked = !!this.defaultChecked;
2018-02-27 12:14:29 +08:00
}
return {
stateChecked: checked,
2019-01-12 11:33:27 +08:00
};
2018-02-27 12:14:29 +08:00
},
2019-02-01 17:23:00 +08:00
watch: {
checked(val) {
this.stateChecked = val;
},
},
2019-01-12 11:33:27 +08:00
mounted() {
2018-02-27 12:14:29 +08:00
this.$nextTick(() => {
2019-01-12 11:33:27 +08:00
const { autoFocus, disabled } = this;
2018-02-27 12:14:29 +08:00
if (autoFocus && !disabled) {
2019-01-12 11:33:27 +08:00
this.focus();
2018-02-27 12:14:29 +08:00
}
2019-01-12 11:33:27 +08:00
});
2018-02-27 12:14:29 +08:00
},
methods: {
saveRef(c) {
this.refSwitchNode = c;
},
2019-06-30 20:43:46 +08:00
setChecked(checked, e) {
2018-02-27 12:14:29 +08:00
if (this.disabled) {
2019-01-12 11:33:27 +08:00
return;
2018-02-27 12:14:29 +08:00
}
if (!hasProp(this, 'checked')) {
2019-01-12 11:33:27 +08:00
this.stateChecked = checked;
2018-02-27 12:14:29 +08:00
}
2019-06-30 20:43:46 +08:00
this.$emit('change', checked, e);
this.$emit('update:checked', checked);
2018-02-27 12:14:29 +08:00
},
2019-03-02 11:21:00 +08:00
handleClick(e) {
2019-01-12 11:33:27 +08:00
const checked = !this.stateChecked;
2019-03-02 11:21:00 +08:00
this.setChecked(checked, e);
this.$emit('click', checked, e);
2018-02-27 12:14:29 +08:00
},
2019-01-12 11:33:27 +08:00
handleKeyDown(e) {
if (e.keyCode === 37) {
// Left
2019-06-30 20:43:46 +08:00
this.setChecked(false, e);
2019-01-12 11:33:27 +08:00
} else if (e.keyCode === 39) {
// Right
2019-06-30 20:43:46 +08:00
this.setChecked(true, e);
2018-02-27 12:14:29 +08:00
}
},
2019-01-12 11:33:27 +08:00
handleMouseUp(e) {
this.refSwitchNode?.blur();
2019-01-12 11:33:27 +08:00
this.$emit('mouseup', e);
2018-02-27 12:14:29 +08:00
},
2019-01-12 11:33:27 +08:00
focus() {
this.refSwitchNode?.focus();
2018-02-27 12:14:29 +08:00
},
2019-01-12 11:33:27 +08:00
blur() {
this.refSwitchNode?.blur();
2018-02-27 12:14:29 +08:00
},
},
2019-01-12 11:33:27 +08:00
render() {
const {
prefixCls,
disabled,
loadingIcon,
defaultChecked,
autoFocus,
...restProps
} = getOptionProps(this);
2019-01-12 11:33:27 +08:00
const checked = this.stateChecked;
const { $attrs } = this;
2018-02-27 12:14:29 +08:00
const switchClassName = {
[$attrs.class]: $attrs.class,
2018-02-27 12:14:29 +08:00
[prefixCls]: true,
[`${prefixCls}-checked`]: checked,
[`${prefixCls}-disabled`]: disabled,
2019-01-12 11:33:27 +08:00
};
2018-02-27 15:28:30 +08:00
const spanProps = {
...restProps,
...$attrs,
onKeydown: this.handleKeyDown,
onClick: this.handleClick,
onMouseup: this.handleMouseUp,
type: 'button',
role: 'switch',
'aria-checked': checked,
disabled,
2018-02-27 15:28:30 +08:00
class: switchClassName,
2020-06-24 23:45:36 +08:00
ref: this.saveRef,
2019-01-12 11:33:27 +08:00
};
2018-02-27 12:14:29 +08:00
return (
2018-10-31 19:28:53 +08:00
<button {...spanProps}>
{loadingIcon}
2018-02-27 12:14:29 +08:00
<span class={`${prefixCls}-inner`}>
2019-01-12 11:33:27 +08:00
{checked
? getComponent(this, 'checkedChildren')
: getComponent(this, 'unCheckedChildren')}
2018-02-27 12:14:29 +08:00
</span>
2018-10-31 19:28:53 +08:00
</button>
2019-01-12 11:33:27 +08:00
);
2018-02-27 12:14:29 +08:00
},
2019-01-12 11:33:27 +08:00
};