use dioxus::prelude::*;
pub const NOTIFICATION: &str = "el-notification";
#[derive(Clone, PartialEq)]
pub enum NotificationType {
Success,
Warning,
Info,
Error,
}
impl NotificationType {
pub fn as_class(&self) -> &'static str {
match self {
NotificationType::Success => "el-notification--success",
NotificationType::Warning => "el-notification--warning",
NotificationType::Info => "el-notification--info",
NotificationType::Error => "el-notification--error",
}
}
}
#[derive(Props, Clone, PartialEq)]
pub struct NotificationProps {
pub title: String,
pub message: String,
#[props(default = NotificationType::Info)]
pub notification_type: NotificationType,
#[props(default = "top-right".to_string())]
pub position: String,
#[props(default = 4500)]
pub duration: u64,
#[props(default = true)]
pub closable: bool,
#[props(default = true)]
pub show_icon: bool,
#[props(default)]
pub class: Option<String>,
#[props(default)]
pub style: Option<String>,
#[props(default)]
pub on_close: Option<EventHandler<()>>,
}
#[component]
pub fn Notification(props: NotificationProps) -> Element {
let mut class_names = vec![NOTIFICATION.to_string()];
class_names.push(props.notification_type.as_class().to_string());
class_names.push(format!("is-{}", props.position));
if let Some(ref custom_class) = props.class {
class_names.push(custom_class.to_string());
}
let class_string = class_names.join(" ");
let style_string = props.style.as_ref().cloned().unwrap_or_default();
let icon_class = match props.notification_type {
NotificationType::Success => "el-icon-check-circle",
NotificationType::Warning => "el-icon-warning-outline",
NotificationType::Info => "el-icon-info-circle",
NotificationType::Error => "el-icon-error-circle",
};
rsx! {
div {
class: "{class_string}",
style: "{style_string}",
role: "alert",
div {
class: "el-notification__content",
if props.show_icon {
i {
class: "{icon_class} el-notification__icon"
}
}
div {
class: "el-notification__group",
h3 {
class: "el-notification__title",
"{props.title}"
}
div {
class: "el-notification__message",
"{props.message}"
}
}
}
if props.closable {
button {
class: "el-notification__close-btn",
r#type: "button",
onclick: move |_| {
if let Some(handler) = props.on_close {
handler.call(());
}
},
"×"
}
}
}
}
}