refactor(Toast): refactor with composition api

This commit is contained in:
chenjiahan 2020-08-31 11:41:48 +08:00
parent 9a84468e79
commit 44aaa47187

View File

@ -6,6 +6,7 @@ import { lockClick } from './lock-click';
import Icon from '../icon'; import Icon from '../icon';
import Popup from '../popup'; import Popup from '../popup';
import Loading from '../loading'; import Loading from '../loading';
import { watch, onMounted, onUnmounted } from 'vue';
const [createComponent, bem] = createNamespace('toast'); const [createComponent, bem] = createNamespace('toast');
@ -13,13 +14,13 @@ export default createComponent({
props: { props: {
icon: String, icon: String,
show: Boolean, show: Boolean,
clear: Function, message: [Number, String],
className: null, className: null,
iconPrefix: String, iconPrefix: String,
lockScroll: Boolean,
loadingType: String, loadingType: String,
forbidClick: Boolean, forbidClick: Boolean,
closeOnClick: Boolean, closeOnClick: Boolean,
message: [Number, String],
type: { type: {
type: String, type: String,
default: 'text', default: 'text',
@ -32,72 +33,37 @@ export default createComponent({
type: String, type: String,
default: 'van-fade', default: 'van-fade',
}, },
lockScroll: {
type: Boolean,
default: false,
},
}, },
emits: ['opened', 'closed', 'update:show'], emits: ['update:show'],
data() { setup(props, { emit }) {
return { let clickable;
clickable: false,
};
},
mounted() { const toggleClickable = () => {
this.toggleClickable(); const newValue = props.show && props.forbidClick;
}, if (clickable !== newValue) {
clickable = newValue;
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;
lockClick(clickable); lockClick(clickable);
} }
}, };
/* istanbul ignore next */ const onClick = () => {
onAfterEnter() { if (props.closeOnClick) {
this.$emit('opened'); emit('update:show', false);
if (this.onOpened) {
this.onOpened();
} }
}, };
onAfterLeave() { const renderIcon = () => {
this.$emit('closed'); const { icon, type, iconPrefix, loadingType } = props;
},
genIcon() {
const { icon, type, iconPrefix, loadingType } = this;
const hasIcon = icon || type === 'success' || type === 'fail'; const hasIcon = icon || type === 'success' || type === 'fail';
if (hasIcon) { if (hasIcon) {
return ( return (
<Icon <Icon
name={icon || type}
class={bem('icon')} class={bem('icon')}
classPrefix={iconPrefix} classPrefix={iconPrefix}
name={icon || type}
/> />
); );
} }
@ -105,38 +71,37 @@ export default createComponent({
if (type === 'loading') { if (type === 'loading') {
return <Loading class={bem('loading')} type={loadingType} />; return <Loading class={bem('loading')} type={loadingType} />;
} }
}, };
genMessage() { const renderMessage = () => {
const { type, message } = this; const { type, message } = props;
if (!isDef(message) || message === '') { if (isDef(message) && message !== '') {
return; return type === 'html' ? (
<div class={bem('text')} innerHTML={message} />
) : (
<div class={bem('text')}>{message}</div>
);
} }
};
if (type === 'html') { watch([() => props.show, () => props.forbidClick], toggleClickable);
return <div class={bem('text')} innerHTML={message} />;
}
return <div class={bem('text')}>{message}</div>; onMounted(toggleClickable);
}, onUnmounted(toggleClickable);
},
render() { return () => (
return (
<Popup <Popup
show={this.show} show={props.show}
class={[ class={[
bem([this.position, { [this.type]: !this.icon }]), bem([props.position, { [props.type]: !props.icon }]),
this.className, props.className,
]} ]}
transition={this.transition} transition={props.transition}
onOpened={this.onAfterEnter} onClick={onClick}
onClosed={this.onAfterLeave}
onClick={this.onClick}
> >
{this.genIcon()} {renderIcon()}
{this.genMessage()} {renderMessage()}
</Popup> </Popup>
); );
}, },