chore(Switch): use tsx

This commit is contained in:
chenjiahan
2020-09-21 17:25:35 +08:00
parent bf1f0f57eb
commit 7d0340a35b
2 changed files with 17 additions and 39 deletions
+72
View File
@@ -0,0 +1,72 @@
import { createNamespace, addUnit } from '../utils';
import { useParentField } from '../composition/use-parent-field';
import Loading from '../loading';
const [createComponent, bem] = createNamespace('switch');
export default createComponent({
props: {
size: [Number, String],
loading: Boolean,
disabled: Boolean,
modelValue: null as any,
activeColor: String,
inactiveColor: String,
activeValue: {
type: null as any,
default: true,
},
inactiveValue: {
type: null as any,
default: false,
},
},
emits: ['change', 'update:modelValue'],
setup(props, { emit }) {
const isChecked = () => props.modelValue === props.activeValue;
const onClick = () => {
if (!props.disabled && !props.loading) {
const newValue = isChecked() ? props.inactiveValue : props.activeValue;
emit('update:modelValue', newValue);
emit('change', newValue);
}
};
const renderLoading = () => {
if (props.loading) {
const color = isChecked() ? props.activeColor : props.inactiveColor;
return <Loading class={bem('loading')} color={color} />;
}
};
useParentField(() => props.modelValue);
return () => {
const { size, loading, disabled, activeColor, inactiveColor } = props;
const checked = isChecked();
const style = {
fontSize: addUnit(size),
backgroundColor: checked ? activeColor : inactiveColor,
};
return (
<div
role="switch"
class={bem({
on: checked,
loading,
disabled,
})}
style={style}
aria-checked={checked}
onClick={onClick}
>
<div class={bem('node')}>{renderLoading()}</div>
</div>
);
};
},
});