feat(button): Support customize loading icon (#281)

This commit is contained in:
Floyd Wang 2024-09-27 13:56:04 +08:00 committed by GitHub
parent 76be7a8ed8
commit 2c1119a7bd
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 14 additions and 1 deletions

View file

@ -549,6 +549,7 @@ impl Render for ButtonStory {
.child( .child(
Button::new("icon-button-primary", cx) Button::new("icon-button-primary", cx)
.icon(IconName::Search) .icon(IconName::Search)
.loading_icon(IconName::LoaderCircle)
.primary() .primary()
.disabled(disabled) .disabled(disabled)
.selected(selected) .selected(selected)
@ -595,6 +596,7 @@ impl Render for ButtonStory {
.child( .child(
Button::new("icon-button-ghost", cx) Button::new("icon-button-ghost", cx)
.icon(IconName::ArrowLeft) .icon(IconName::ArrowLeft)
.loading_icon(IconName::LoaderCircle)
.ghost() .ghost()
.disabled(disabled) .disabled(disabled)
.selected(selected) .selected(selected)

View file

@ -163,6 +163,7 @@ pub struct Button {
on_click: Option<Box<dyn Fn(&ClickEvent, &mut WindowContext) + 'static>>, on_click: Option<Box<dyn Fn(&ClickEvent, &mut WindowContext) + 'static>>,
pub(crate) stop_propagation: bool, pub(crate) stop_propagation: bool,
loading: bool, loading: bool,
loading_icon: Option<Icon>,
} }
impl From<Button> for AnyElement { impl From<Button> for AnyElement {
@ -192,6 +193,7 @@ impl Button {
loading: false, loading: false,
compact: false, compact: false,
children: Vec::new(), children: Vec::new(),
loading_icon: None,
} }
} }
@ -258,6 +260,11 @@ impl Button {
self.stop_propagation = val; self.stop_propagation = val;
self self
} }
pub fn loading_icon(mut self, icon: impl Into<Icon>) -> Self {
self.loading_icon = Some(icon.into());
self
}
} }
impl Disableable for Button { impl Disableable for Button {
@ -435,7 +442,11 @@ impl RenderOnce for Button {
}) })
}) })
.when(self.loading, |this| { .when(self.loading, |this| {
this.child(Indicator::new().with_size(self.size)) this.child(
Indicator::new()
.with_size(self.size)
.when_some(self.loading_icon, |this, icon| this.icon(icon)),
)
}) })
.when_some(self.label, |this, label| { .when_some(self.label, |this, label| {
this.child( this.child(