Skip to content

Commit

Permalink
make pretty
Browse files Browse the repository at this point in the history
  • Loading branch information
maartenbreddels committed Sep 28, 2023
1 parent f106d11 commit f99d5f2
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 30 deletions.
41 changes: 21 additions & 20 deletions solara/lab/components/confirmation_dialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def delete_user():
@solara.component
def Page():
solara.Button(label="Delete user", on_click=lambda: open_delete_confirmation.set(True))
solara.lab.ConfirmationDialog(open_delete_confirmation, on_ok=delete_user, content="Are you sure you want to delete this user?")
solara.lab.ConfirmationDialog(open_delete_confirmation, ok="Ok, Delete", on_ok=delete_user, content="Are you sure you want to delete this user?")
```
## Arguments
Expand Down Expand Up @@ -92,28 +92,29 @@ def on_v_model(value):
persistent=persistent,
max_width=max_width,
):
with solara.Card(title=title):
if isinstance(content, str):
solara.Markdown(content)
else:
solara.display(content)
if children:
solara.display(*children)
with solara.CardActions():

if isinstance(ok, str):
solara.Button(label=ok, on_click=perform_ok)
with solara.v.Card():
solara.v.Toolbar(color="primary", dark=True, children=[title])
with solara.v.CardText(class_="pa-4"):
if isinstance(content, str):
solara.Text(content)
else:
# the user may have passed in on_click already
user_on_click_ok = ok.kwargs.get("on_click")
# override or add our own on_click handler
ok.kwargs = {**ok.kwargs, "on_click": perform_ok}
solara.display(ok)

# similar as ok
solara.display(content)
if children:
solara.display(*children)
with solara.v.CardActions(class_="justify-end"):
if isinstance(cancel, str):
solara.Button(label=cancel, on_click=perform_cancel)
solara.Button(label=cancel, on_click=perform_cancel, outlined=True, color="primary")
else:
# the user may have passed in on_click already
user_on_click_cancel = cancel.kwargs.get("on_click")
# override or add our own on_click handler
cancel.kwargs = {**cancel.kwargs, "on_click": perform_cancel}
solara.display(cancel)

# similar as cancel
if isinstance(ok, str):
solara.Button(label=ok, on_click=perform_ok, color="primary")
else:
user_on_click_ok = ok.kwargs.get("on_click")
ok.kwargs = {**ok.kwargs, "on_click": perform_ok}
solara.display(ok)
8 changes: 7 additions & 1 deletion solara/website/pages/api/confirmation_dialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,13 @@ def Page():
style="max-width: 400px;",
disabled=not users.value,
)
ConfirmationDialog(is_open, on_ok=delete_user, title="Delete user", content=f"Are you sure you want to delete user **{user_to_be_deleted.value}**?")
with ConfirmationDialog(
is_open,
on_ok=delete_user,
ok="Ok, Delete",
title="Delete user",
):
solara.Markdown(f"Are you sure you want to delete user **{user_to_be_deleted.value}**?")


__doc__ += apidoc(solara.lab.components.confirmation_dialog.ConfirmationDialog.f) # type: ignore
18 changes: 9 additions & 9 deletions tests/unit/confirmation_dialog_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def test_confirmation_dialog_ok():
_, rc = solara.render(el, handle_error=False)
buttons = rc.find(vw.Btn)
assert len(buttons) == 2
buttons[0].widget.click()
buttons[1].widget.click()
assert on_ok.call_count == 1 # was OK button clicked?
assert on_open.call_count == 1 # always triggered
assert not is_open.value # is dialog closed?
Expand All @@ -28,7 +28,7 @@ def test_confirmation_dialog_cancel():
_, rc = solara.render(el, handle_error=False)
buttons = rc.find(vw.Btn)
assert len(buttons) == 2
buttons[1].widget.click()
buttons[0].widget.click()
assert on_ok.call_count == 0 # on_ok action should not have been executed
assert on_open.call_count == 1 # always triggered
assert not is_open.value # is dialog closed?
Expand Down Expand Up @@ -59,9 +59,9 @@ def test_confirmation_dialog_custom_button_no_onclick():
_, rc = solara.render(el, handle_error=False)
buttons = rc.find(vw.Btn)
assert len(buttons) == 2
assert buttons[0].widget.children == ["Not OK"]
assert buttons[1].widget.children == ["Cancel"]
buttons[0].widget.click()
assert buttons[0].widget.children == ["Cancel"]
assert buttons[1].widget.children == ["Not OK"]
buttons[1].widget.click()
assert on_ok.call_count == 1 # should still be called


Expand All @@ -87,11 +87,11 @@ def on_click_cancel():
_, rc = solara.render(el, handle_error=False)
buttons = rc.find(vw.Btn)
assert len(buttons) == 2
assert buttons[0].widget.children == ["Not OK"]
assert buttons[1].widget.children == ["Not Cancel"]
buttons[0].widget.click()
assert buttons[1].widget.children == ["Not OK"]
assert buttons[0].widget.children == ["Not Cancel"]
buttons[1].widget.click()
assert values == ["on_click_ok", "on_ok"] # assert on_ok and on_click were both called, in that order
values.clear()
# now the same for cancel
buttons[1].widget.click()
buttons[0].widget.click()
assert values == ["on_click_cancel", "on_cancel"]

0 comments on commit f99d5f2

Please sign in to comment.