primevue-mirror/components/lib/toast/ToastMessage.vue

120 lines
3.8 KiB
Vue
Raw Normal View History

2022-09-06 12:03:37 +00:00
<template>
<div :class="containerClass" role="alert" aria-live="assertive" aria-atomic="true">
<div class="p-toast-message-content" :class="message.contentStyleClass">
<template v-if="!template">
<slot name="icon">
<component :is="icon ? 'span' : iconComponent" :class="['p-toast-message-icon', icon]"></component>
</slot>
2022-09-06 12:03:37 +00:00
<div class="p-toast-message-text">
2022-09-14 11:26:01 +00:00
<span class="p-toast-summary">{{ message.summary }}</span>
<div class="p-toast-detail">{{ message.detail }}</div>
2022-09-06 12:03:37 +00:00
</div>
</template>
<component v-else :is="template" :message="message"></component>
2022-12-08 11:04:25 +00:00
<div v-if="message.closable !== false">
<button v-ripple class="p-toast-icon-close p-link" type="button" :aria-label="closeAriaLabel" @click="onCloseClick" autofocus v-bind="closeButtonProps">
<slot name="closeIcon">
<component :is="closeIcon ? 'span' : 'TimesIcon'" :class="['p-toast-icon-close-icon', closeIcon]"></component>
</slot>
2022-12-08 11:04:25 +00:00
</button>
</div>
2022-09-06 12:03:37 +00:00
</div>
</div>
</template>
<script>
import Ripple from 'primevue/ripple';
import TimesIcon from 'primevue/icon/times';
import InfoCircleIcon from 'primevue/icon/infocircle';
import CheckIcon from 'primevue/icon/check';
import ExclamationTriangleIcon from 'primevue/icon/exclamationtriangle';
import TimesCircleIcon from 'primevue/icon/timescircle';
2022-09-06 12:03:37 +00:00
export default {
name: 'ToastMessage',
emits: ['close'],
props: {
2022-12-08 11:04:25 +00:00
message: {
type: null,
default: null
},
template: {
type: null,
default: null
},
closeIcon: {
type: String,
default: undefined
2022-12-08 11:04:25 +00:00
},
icon: {
2022-12-08 11:04:25 +00:00
type: String,
default: undefined
2022-12-08 11:04:25 +00:00
},
closeButtonProps: {
type: null,
default: null
}
2022-09-06 12:03:37 +00:00
},
closeTimeout: null,
mounted() {
if (this.message.life) {
this.closeTimeout = setTimeout(() => {
this.close({ message: this.message, type: 'life-end' });
2022-09-14 11:26:01 +00:00
}, this.message.life);
2022-09-06 12:03:37 +00:00
}
},
beforeUnmount() {
this.clearCloseTimeout();
},
methods: {
close(params) {
this.$emit('close', params);
2022-09-06 12:03:37 +00:00
},
onCloseClick() {
this.clearCloseTimeout();
this.close({ message: this.message, type: 'close' });
2022-09-06 12:03:37 +00:00
},
clearCloseTimeout() {
if (this.closeTimeout) {
clearTimeout(this.closeTimeout);
this.closeTimeout = null;
}
}
},
computed: {
containerClass() {
2022-09-14 11:26:01 +00:00
return [
'p-toast-message',
this.message.styleClass,
{
'p-toast-message-info': this.message.severity === 'info',
'p-toast-message-warn': this.message.severity === 'warn',
'p-toast-message-error': this.message.severity === 'error',
'p-toast-message-success': this.message.severity === 'success'
}
];
2022-09-06 12:03:37 +00:00
},
iconComponent() {
return {
info: InfoCircleIcon,
success: CheckIcon,
warn: ExclamationTriangleIcon,
error: TimesCircleIcon
}[this.message.severity];
2022-12-08 11:04:25 +00:00
},
closeAriaLabel() {
return this.$primevue.config.locale.aria ? this.$primevue.config.locale.aria.close : undefined;
2022-09-06 12:03:37 +00:00
}
},
components: {
TimesIcon,
InfoCircleIcon,
CheckIcon,
ExclamationTriangleIcon,
TimesCircleIcon
},
2022-09-06 12:03:37 +00:00
directives: {
2022-09-14 11:26:01 +00:00
ripple: Ripple
2022-09-06 12:03:37 +00:00
}
2022-09-14 11:26:01 +00:00
};
2022-09-06 12:03:37 +00:00
</script>