Skip to content

Commit

Permalink
Add disabled argument to ui.input_action_button() and `ui.update_…
Browse files Browse the repository at this point in the history
…action_button()`

Fixes #1465
  • Loading branch information
gadenbuie committed Jul 16, 2024
1 parent 00ff063 commit 0f924d7
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 3 deletions.
25 changes: 25 additions & 0 deletions shiny/api-examples/input_action_button/app-disabled-core.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from shiny import App, Inputs, reactive, render, ui

app_ui = ui.page_fluid(
ui.input_text("name", "Your Name"),
ui.input_action_button("greet", "Say Hello", disabled=True),
ui.output_ui("hello"),
)


def server(input: Inputs):
@reactive.effect
@reactive.event(input.name)
def set_button_state():
if input.name():
ui.update_action_button("greet", disabled=False)
else:
ui.update_action_button("greet", disabled=True)

@render.ui
@reactive.event(input.greet)
def hello():
return ui.p(f"Hello, {input.name()}!", class_="fs-1 text-primary mt-3")


app = App(app_ui, server)
20 changes: 20 additions & 0 deletions shiny/api-examples/input_action_button/app-disabled-express.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from shiny import reactive
from shiny.express import input, render, ui

ui.input_text("name", "Your Name")
ui.input_action_button("greet", "Say Hello", disabled=True)


@reactive.effect
@reactive.event(input.name)
def set_button_state():
if input.name():
ui.update_action_button("greet", disabled=False)
else:
ui.update_action_button("greet", disabled=True)


@render.ui
@reactive.event(input.greet)
def hello():
return ui.p(f"Hello, {input.name()}!", class_="fs-1 text-primary mt-3")
8 changes: 8 additions & 0 deletions shiny/ui/_input_action_button.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@


@add_example()
@add_example("app-disabled-core.py")
def input_action_button(
id: str,
label: TagChild,
*,
icon: TagChild = None,
width: Optional[str] = None,
disabled: bool = False,
**kwargs: TagAttrValue,
) -> Tag:
"""
Expand All @@ -31,6 +33,9 @@ def input_action_button(
An icon to appear inline with the button/link.
width
The CSS width, e.g. '400px', or '100%'
disabled
If `True`, the button will not be clickable. Use
:func:`~shiny.ui.update_action_button` to dynamically enable/disable the button.
**kwargs
Attributes to be applied to the button.
Expand All @@ -47,6 +52,7 @@ def input_action_button(
See Also
--------
* :func:`~shiny.ui.update_action_button`
* :func:`~shiny.ui.input_action_link`
* :func:`~shiny.reactive.event`
"""
Expand All @@ -61,6 +67,7 @@ def input_action_button(
label,
id=resolve_id(id),
type="button",
disabled="" if disabled else None,
**kwargs,
)

Expand Down Expand Up @@ -101,6 +108,7 @@ def input_action_link(
See Also
--------
* :func:`~shiny.ui.update_action_link`
* :func:`~shiny.ui.input_action_button`
* :func:`~shiny.reactive.event`
"""
Expand Down
53 changes: 50 additions & 3 deletions shiny/ui/_input_update.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ def update_action_button(
*,
label: Optional[str] = None,
icon: TagChild = None,
disabled: Optional[bool] = None,
session: Optional[Session] = None,
) -> None:
"""
Expand All @@ -86,6 +87,9 @@ def update_action_button(
An input label.
icon
An icon to appear inline with the button/link.
disabled
If `True`, disable the button making it unclickable; if `False`, the button will
become enabled and clickable.
session
A :class:`~shiny.Session` instance. If not provided, it is inferred via
:func:`~shiny.session.get_current_session`.
Expand All @@ -102,12 +106,55 @@ def update_action_button(
session = require_active_session(session)
# TODO: supporting a TagChild for label would require changes to shiny.js
# https://github.com/rstudio/shiny/issues/1140
msg = {"label": label, "icon": session._process_ui(icon)["html"] if icon else None}
msg = {
"label": label,
"icon": session._process_ui(icon)["html"] if icon else None,
"disabled": disabled,
}
session.send_input_message(id, drop_none(msg))


update_action_link = update_action_button
update_action_link.__doc__ = update_action_button.__doc__
@add_example(ex_dir="../api-examples/update_action_button")
@doc_format(note=_note)
def update_action_link(
id: str,
*,
label: Optional[str] = None,
icon: TagChild = None,
session: Optional[Session] = None,
) -> None:
"""
Change the label and/or icon of an action link on the client.
Parameters
----------
id
An input id.
label
An input label.
icon
An icon to appear inline with the button/link.
session
A :class:`~shiny.Session` instance. If not provided, it is inferred via
:func:`~shiny.session.get_current_session`.
Note
----
{note}
See Also
--------
* :func:`~shiny.input_action_link`
"""

session = require_active_session(session)
# TODO: supporting a TagChild for label would require changes to shiny.js
# https://github.com/rstudio/shiny/issues/1140
msg = {
"label": label,
"icon": session._process_ui(icon)["html"] if icon else None,
}
session.send_input_message(id, drop_none(msg))


# -----------------------------------------------------------------------------
Expand Down

0 comments on commit 0f924d7

Please sign in to comment.