feat: Tag adds closable and on_close prop (#137)

This commit is contained in:
luoxiaozero 2024-03-14 22:43:00 +08:00 committed by GitHub
parent c945363168
commit f996a3b4ea
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 72 additions and 3 deletions

View file

@ -11,10 +11,34 @@ view! {
} }
``` ```
### Closable
```rust demo
let message = use_message();
let success = move |_| {
message.create(
"tag close".into(),
MessageVariant::Success,
Default::default(),
);
};
view! {
<Space>
<Tag closable=true on_close=success>"Default"</Tag>
<Tag closable=true on_close=success variant=TagVariant::Success>"Success"</Tag>
<Tag closable=true on_close=success variant=TagVariant::Warning>"Warning"</Tag>
<Tag closable=true on_close=success variant=TagVariant::Error>"Error"</Tag>
</Space>
}
```
### Tag Props ### Tag Props
| Name | Type | Default | Description | | Name | Type | Default | Description |
| -------- | ----------------------------------- | --------------------- | -------------------------------------- | | -------- | ----------------------------------- | --------------------- | -------------------------------------- |
| class | `OptionalProp<MaybeSignal<String>>` | `Default::default()` | Addtional classes for the tag element. | | class | `OptionalProp<MaybeSignal<String>>` | `Default::default()` | Addtional classes for the tag element. |
| variant | `MaybeSignal<TagVariant>` | `TagVariant::Default` | Tag's variant. | | variant | `MaybeSignal<TagVariant>` | `TagVariant::Default` | Tag's variant. |
| closable | `MaybeSignal<bool>` | `false` | Whether the tag shows a close button. |
| on_close | `Option<Callback<ev::MouseEvent>>` | `None` | Close clicked callback. |
| children | `Children` | | Tag's content. | | children | `Children` | | Tag's content. |

View file

@ -1,12 +1,13 @@
mod theme; mod theme;
pub use theme::TagTheme;
use crate::{ use crate::{
theme::use_theme, theme::use_theme,
utils::{class_list::class_list, mount_style, OptionalProp}, utils::{class_list::class_list, mount_style, OptionalProp},
Theme, Icon, Theme,
}; };
use leptos::*; use leptos::*;
pub use theme::TagTheme;
#[derive(Clone, Default)] #[derive(Clone, Default)]
pub enum TagVariant { pub enum TagVariant {
@ -48,6 +49,8 @@ impl TagVariant {
pub fn Tag( pub fn Tag(
#[prop(optional, into)] variant: MaybeSignal<TagVariant>, #[prop(optional, into)] variant: MaybeSignal<TagVariant>,
#[prop(optional, into)] class: OptionalProp<MaybeSignal<String>>, #[prop(optional, into)] class: OptionalProp<MaybeSignal<String>>,
#[prop(optional, into)] closable: MaybeSignal<bool>,
#[prop(optional, into)] on_close: Option<Callback<ev::MouseEvent>>,
children: Children, children: Children,
) -> impl IntoView { ) -> impl IntoView {
mount_style("tag", include_str!("./tag.css")); mount_style("tag", include_str!("./tag.css"));
@ -72,12 +75,32 @@ pub fn Tag(
css_vars css_vars
}); });
let on_close = move |event| {
let Some(callback) = on_close.as_ref() else {
return;
};
callback.call(event);
};
view! { view! {
<div <div
class=class_list!["thaw-tag", class.map(| c | move || c.get())] class=class_list!["thaw-tag", class.map(| c | move || c.get())]
style=move || css_vars.get() style=move || css_vars.get()
> >
<span class="thaw-tag__content">{children()}</span> <span class="thaw-tag__content">{children()}</span>
{
move || {
if closable.get() {
view! {
<button class="thaw-tag__close" on:click=on_close>
<Icon icon=icondata_ai::AiCloseOutlined style="font-size: 14px"/>
</button>
}.into()
} else {
None
}
}
}
</div> </div>
} }
} }

View file

@ -3,9 +3,31 @@
height: 28px; height: 28px;
align-items: center; align-items: center;
font-size: 14px; font-size: 14px;
padding: 0 10px; line-height: 1;
padding: 0 8px;
background-color: var(--thaw-background-color); background-color: var(--thaw-background-color);
color: var(--thaw-font-color); color: var(--thaw-font-color);
border: 1px solid var(--thaw-border-color); border: 1px solid var(--thaw-border-color);
border-radius: 3px; border-radius: 3px;
} }
.thaw-tag__close {
position: relative;
right: -3px;
display: flex;
align-items: center;
justify-content: center;
background-color: transparent;
padding: 1px;
width: 16px;
height: 16px;
color: var(--thaw-font-color);
border: none;
cursor: pointer;
transition: background-color 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}
.thaw-tag__close:hover {
background-color: var(--thaw-border-color);
border-radius: 3px;
}