diff --git a/solara/lab/components/confirmation_dialog.py b/solara/lab/components/confirmation_dialog.py index 1a24a66fd..6ba944535 100644 --- a/solara/lab/components/confirmation_dialog.py +++ b/solara/lab/components/confirmation_dialog.py @@ -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 @@ -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) diff --git a/solara/website/pages/api/confirmation_dialog.py b/solara/website/pages/api/confirmation_dialog.py index 20bd4b81f..18e03ef24 100644 --- a/solara/website/pages/api/confirmation_dialog.py +++ b/solara/website/pages/api/confirmation_dialog.py @@ -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 diff --git a/tests/unit/confirmation_dialog_test.py b/tests/unit/confirmation_dialog_test.py index 708510b95..895dd4446 100644 --- a/tests/unit/confirmation_dialog_test.py +++ b/tests/unit/confirmation_dialog_test.py @@ -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? @@ -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? @@ -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 @@ -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"]