From 44aaa471879ac79b7baee0e07c92d7a71ff7f530 Mon Sep 17 00:00:00 2001 From: chenjiahan Date: Mon, 31 Aug 2020 11:41:48 +0800 Subject: [PATCH] refactor(Toast): refactor with composition api --- src/toast/Toast.js | 113 ++++++++++++++++----------------------------- 1 file changed, 39 insertions(+), 74 deletions(-) diff --git a/src/toast/Toast.js b/src/toast/Toast.js index 950c88464..672529a14 100644 --- a/src/toast/Toast.js +++ b/src/toast/Toast.js @@ -6,6 +6,7 @@ import { lockClick } from './lock-click'; import Icon from '../icon'; import Popup from '../popup'; import Loading from '../loading'; +import { watch, onMounted, onUnmounted } from 'vue'; const [createComponent, bem] = createNamespace('toast'); @@ -13,13 +14,13 @@ export default createComponent({ props: { icon: String, show: Boolean, - clear: Function, + message: [Number, String], className: null, iconPrefix: String, + lockScroll: Boolean, loadingType: String, forbidClick: Boolean, closeOnClick: Boolean, - message: [Number, String], type: { type: String, default: 'text', @@ -32,72 +33,37 @@ export default createComponent({ type: String, default: 'van-fade', }, - lockScroll: { - type: Boolean, - default: false, - }, }, - emits: ['opened', 'closed', 'update:show'], + emits: ['update:show'], - data() { - return { - clickable: false, - }; - }, + setup(props, { emit }) { + let clickable; - mounted() { - this.toggleClickable(); - }, - - unmounted() { - this.toggleClickable(); - }, - - watch: { - show: 'toggleClickable', - forbidClick: 'toggleClickable', - }, - - methods: { - onClick() { - if (this.closeOnClick) { - this.close(); - } - }, - - toggleClickable() { - const clickable = this.show && this.forbidClick; - - if (this.clickable !== clickable) { - this.clickable = clickable; + const toggleClickable = () => { + const newValue = props.show && props.forbidClick; + if (clickable !== newValue) { + clickable = newValue; lockClick(clickable); } - }, + }; - /* istanbul ignore next */ - onAfterEnter() { - this.$emit('opened'); - - if (this.onOpened) { - this.onOpened(); + const onClick = () => { + if (props.closeOnClick) { + emit('update:show', false); } - }, + }; - onAfterLeave() { - this.$emit('closed'); - }, - - genIcon() { - const { icon, type, iconPrefix, loadingType } = this; + const renderIcon = () => { + const { icon, type, iconPrefix, loadingType } = props; const hasIcon = icon || type === 'success' || type === 'fail'; if (hasIcon) { return ( ); } @@ -105,38 +71,37 @@ export default createComponent({ if (type === 'loading') { return ; } - }, + }; - genMessage() { - const { type, message } = this; + const renderMessage = () => { + const { type, message } = props; - if (!isDef(message) || message === '') { - return; + if (isDef(message) && message !== '') { + return type === 'html' ? ( +
+ ) : ( +
{message}
+ ); } + }; - if (type === 'html') { - return
; - } + watch([() => props.show, () => props.forbidClick], toggleClickable); - return
{message}
; - }, - }, + onMounted(toggleClickable); + onUnmounted(toggleClickable); - render() { - return ( + return () => ( - {this.genIcon()} - {this.genMessage()} + {renderIcon()} + {renderMessage()} ); },