modal: Add alert method to create modal with a Ok button. (#829)

- Fix to handle close modal when `on_ok`, `on_cancel` not present.
This commit is contained in:
Jason Lee 2025-05-02 10:28:26 +08:00 committed by GitHub
parent 2c06a032fb
commit f4e8b952e9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 42 additions and 7 deletions

View file

@ -327,6 +327,24 @@ impl Render for ModalStory {
}); });
})), })),
), ),
)
.child(
section("Alert Modal").child(
Button::new("alert-modal")
.label("Alert")
.on_click(cx.listener(|_, _, window, cx| {
window.open_modal(cx, |modal, _, _| {
modal
.confirm()
.child("You are successfully logged in.")
.alert()
.on_close(|_, window, cx| {
window
.push_notification("You have pressed Ok.", cx);
})
});
})),
),
), ),
) )
} }

View file

@ -173,13 +173,24 @@ impl Modal {
self self
} }
/// Set to use confirm modal, with OK and CANCEL buttons. /// Set to use confirm modal, with OK and Cancel buttons.
///
/// See also [`Self::alert`]
pub fn confirm(self) -> Self { pub fn confirm(self) -> Self {
self.footer(|ok, cancel, window, cx| vec![cancel(window, cx), ok(window, cx)]) self.footer(|ok, cancel, window, cx| vec![cancel(window, cx), ok(window, cx)])
.overlay_closable(false) .overlay_closable(false)
.show_close(false) .show_close(false)
} }
/// Set to as a alter modal, with OK button.
///
/// See also [`Self::confirm`]
pub fn alert(self) -> Self {
self.footer(|ok, _, window, cx| vec![ok(window, cx)])
.overlay_closable(false)
.show_close(false)
}
/// Set the button props of the modal. /// Set the button props of the modal.
pub fn button_props(mut self, button_props: ModalButtonProps) -> Self { pub fn button_props(mut self, button_props: ModalButtonProps) -> Self {
self.button_props = button_props; self.button_props = button_props;
@ -187,6 +198,8 @@ impl Modal {
} }
/// Sets the callback for when the modal is closed. /// Sets the callback for when the modal is closed.
///
/// Called after [`Self::on_ok`] or [`Self::on_cancel`] callback.
pub fn on_close( pub fn on_close(
mut self, mut self,
on_close: impl Fn(&ClickEvent, &mut Window, &mut App) + 'static, on_close: impl Fn(&ClickEvent, &mut Window, &mut App) + 'static,
@ -303,11 +316,13 @@ impl RenderOnce for Modal {
move |_, window, cx| { move |_, window, cx| {
if let Some(on_ok) = &on_ok { if let Some(on_ok) = &on_ok {
if on_ok(&ClickEvent::default(), window, cx) { if !on_ok(&ClickEvent::default(), window, cx) {
on_close(&ClickEvent::default(), window, cx); return;
window.close_modal(cx);
} }
} }
on_close(&ClickEvent::default(), window, cx);
window.close_modal(cx);
} }
}) })
.into_any_element() .into_any_element()
@ -329,10 +344,12 @@ impl RenderOnce for Modal {
let on_cancel = on_cancel.clone(); let on_cancel = on_cancel.clone();
let on_close = on_close.clone(); let on_close = on_close.clone();
move |_, window, cx| { move |_, window, cx| {
if on_cancel(&ClickEvent::default(), window, cx) { if !on_cancel(&ClickEvent::default(), window, cx) {
on_close(&ClickEvent::default(), window, cx); return;
window.close_modal(cx);
} }
on_close(&ClickEvent::default(), window, cx);
window.close_modal(cx);
} }
}) })
.into_any_element() .into_any_element()