[new feature] Toast: add icon prop (#3485)

This commit is contained in:
neverland
2019-06-12 17:21:59 +08:00
committed by GitHub
parent 2d18f67a7d
commit 153fb9d69f
15 changed files with 185 additions and 48 deletions

View File

@@ -4,12 +4,12 @@ import Icon from '../icon';
import Loading from '../loading';
const [sfc, bem] = use('toast');
const STYLE = ['success', 'fail', 'loading'];
export default sfc({
mixins: [PopupMixin],
props: {
icon: String,
className: null,
loadingType: String,
forbidClick: Boolean,
@@ -62,6 +62,7 @@ export default sfc({
}
},
/* istanbul ignore next */
onAfterEnter() {
this.$emit('opened');
@@ -72,31 +73,43 @@ export default sfc({
},
render(h) {
const { type, message, loadingType } = this;
const style = STYLE.indexOf(type) !== -1 ? 'default' : type;
const { type, icon, message, loadingType } = this;
function Content() {
switch (style) {
case 'text':
return <div>{message}</div>;
case 'html':
return <div domPropsInnerHTML={message} />;
const hasIcon = icon || (type === 'success' || type === 'fail');
function ToastIcon() {
if (hasIcon) {
return <Icon class={bem('icon')} name={icon || type} />;
}
return [
type === 'loading' ? (
<Loading color="white" type={loadingType} />
) : (
<Icon class={bem('icon')} name={type} />
),
isDef(message) && <div class={bem('text')}>{message}</div>
];
if (type === 'loading') {
return <Loading class={bem('loading')} color="white" type={loadingType} />;
}
}
function Message() {
if (!isDef(message) || message === '') {
return;
}
if (type === 'html') {
return <div class={bem('text')} domPropsInnerHTML={message} />;
}
return <div class={bem('text')}>{message}</div>;
}
return (
<transition name="van-fade" onAfterEnter={this.onAfterEnter}>
<div vShow={this.value} class={[bem([style, this.position]), this.className]}>
{Content()}
<div
vShow={this.value}
class={[
bem([this.position, { text: !hasIcon && type !== 'loading' }]),
this.className
]}
>
{ToastIcon()}
{Message()}
</div>
</transition>
);