mirror of
https://gitee.com/vant-contrib/vant.git
synced 2025-05-03 22:46:33 +08:00
refactor(Switch): use setup
This commit is contained in:
parent
04290db467
commit
7b4286b9d5
@ -15,57 +15,49 @@ export default createComponent({
|
|||||||
|
|
||||||
props: switchProps,
|
props: switchProps,
|
||||||
|
|
||||||
emits: ['click', 'change', 'update:modelValue'],
|
emits: ['change', 'update:modelValue'],
|
||||||
|
|
||||||
computed: {
|
setup(props, { emit }) {
|
||||||
checked() {
|
const isChecked = () => props.modelValue === props.activeValue;
|
||||||
return this.modelValue === this.activeValue;
|
|
||||||
},
|
|
||||||
|
|
||||||
style() {
|
const onClick = () => {
|
||||||
return {
|
if (!props.disabled && !props.loading) {
|
||||||
fontSize: addUnit(this.size),
|
const newValue = isChecked() ? props.inactiveValue : props.activeValue;
|
||||||
backgroundColor: this.checked ? this.activeColor : this.inactiveColor,
|
emit('update:modelValue', newValue);
|
||||||
};
|
emit('change', newValue);
|
||||||
},
|
|
||||||
},
|
|
||||||
|
|
||||||
methods: {
|
|
||||||
onClick(event) {
|
|
||||||
this.$emit('click', event);
|
|
||||||
|
|
||||||
if (!this.disabled && !this.loading) {
|
|
||||||
const newValue = this.checked ? this.inactiveValue : this.activeValue;
|
|
||||||
this.$emit('update:modelValue', newValue);
|
|
||||||
this.$emit('change', newValue);
|
|
||||||
}
|
}
|
||||||
},
|
};
|
||||||
|
|
||||||
genLoading() {
|
const renderLoading = () => {
|
||||||
if (this.loading) {
|
if (props.loading) {
|
||||||
const color = this.checked ? this.activeColor : this.inactiveColor;
|
const color = isChecked() ? props.activeColor : props.inactiveColor;
|
||||||
return <Loading class={bem('loading')} color={color} />;
|
return <Loading class={bem('loading')} color={color} />;
|
||||||
}
|
}
|
||||||
},
|
};
|
||||||
},
|
|
||||||
|
|
||||||
render() {
|
return () => {
|
||||||
const { checked, loading, disabled } = this;
|
const { size, loading, disabled, activeColor, inactiveColor } = props;
|
||||||
|
const checked = isChecked();
|
||||||
|
const style = {
|
||||||
|
fontSize: addUnit(size),
|
||||||
|
backgroundColor: checked ? activeColor : inactiveColor,
|
||||||
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div
|
<div
|
||||||
|
role="switch"
|
||||||
class={bem({
|
class={bem({
|
||||||
on: checked,
|
on: checked,
|
||||||
loading,
|
loading,
|
||||||
disabled,
|
disabled,
|
||||||
})}
|
})}
|
||||||
role="switch"
|
style={style}
|
||||||
style={this.style}
|
|
||||||
aria-checked={String(checked)}
|
aria-checked={String(checked)}
|
||||||
onClick={this.onClick}
|
onClick={onClick}
|
||||||
>
|
>
|
||||||
<div class={bem('node')}>{this.genLoading()}</div>
|
<div class={bem('node')}>{renderLoading()}</div>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
|
};
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user