From 7b4286b9d53180a4e37d7d735073d5874ed5c86e Mon Sep 17 00:00:00 2001 From: chenjiahan Date: Tue, 25 Aug 2020 16:04:39 +0800 Subject: [PATCH] refactor(Switch): use setup --- src/switch/index.js | 80 ++++++++++++++++++++------------------------- 1 file changed, 36 insertions(+), 44 deletions(-) diff --git a/src/switch/index.js b/src/switch/index.js index 334e42fe7..b83faf256 100644 --- a/src/switch/index.js +++ b/src/switch/index.js @@ -15,57 +15,49 @@ export default createComponent({ props: switchProps, - emits: ['click', 'change', 'update:modelValue'], + emits: ['change', 'update:modelValue'], - computed: { - checked() { - return this.modelValue === this.activeValue; - }, + setup(props, { emit }) { + const isChecked = () => props.modelValue === props.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('update:modelValue', newValue); - this.$emit('change', newValue); + const onClick = () => { + if (!props.disabled && !props.loading) { + const newValue = isChecked() ? props.inactiveValue : props.activeValue; + emit('update:modelValue', newValue); + emit('change', newValue); } - }, + }; - genLoading() { - if (this.loading) { - const color = this.checked ? this.activeColor : this.inactiveColor; + const renderLoading = () => { + if (props.loading) { + const color = isChecked() ? props.activeColor : props.inactiveColor; return ; } - }, - }, + }; - render() { - const { checked, loading, disabled } = this; + return () => { + const { size, loading, disabled, activeColor, inactiveColor } = props; + const checked = isChecked(); + const style = { + fontSize: addUnit(size), + backgroundColor: checked ? activeColor : inactiveColor, + }; - return ( -
-
{this.genLoading()}
-
- ); + return ( +
+
{renderLoading()}
+
+ ); + }; }, });