feat(Form): support using switch

This commit is contained in:
陈嘉涵
2020-02-10 16:30:17 +08:00
parent 0e23124c3a
commit 1db9536182
6 changed files with 125 additions and 110 deletions
+73
View File
@@ -0,0 +1,73 @@
// Utils
import { createNamespace, addUnit } from '../utils';
import { BLUE } from '../utils/constant';
import { switchProps } from './shared';
// Mixins
import { FieldMixin } from '../mixins/field';
// Components
import Loading from '../loading';
const [createComponent, bem] = createNamespace('switch');
export default createComponent({
mixins: [FieldMixin],
props: switchProps,
computed: {
checked() {
return this.value === this.activeValue;
},
style() {
return {
fontSize: addUnit(this.size),
backgroundColor: this.checked ? this.activeColor : this.inactiveColor,
};
},
},
methods: {
onClick(event) {
this.$emit('click', event);
if (!this.disabled && !this.loading) {
const newValue = this.checked ? this.inactiveValue : this.activeValue;
this.$emit('input', newValue);
this.$emit('change', newValue);
}
},
genLoading() {
if (this.loading) {
const color = this.checked
? this.activeColor || BLUE
: this.inactiveColor || '';
return <Loading class={bem('loading')} color={color} />;
}
},
},
render() {
const { checked, loading, disabled } = this;
return (
<div
class={bem({
on: checked,
loading,
disabled,
})}
role="switch"
style={this.style}
aria-checked={String(checked)}
onClick={this.onClick}
>
<div class={bem('node')}>{this.genLoading()}</div>
</div>
);
},
});