Skip to content

Commit 9f5d86e

Browse files
committed
Callout the problem with this approach
1 parent e350db0 commit 9f5d86e

File tree

2 files changed

+31
-33
lines changed

2 files changed

+31
-33
lines changed

shiny/ui/_input_select.py

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -268,19 +268,6 @@ def _input_select_impl(
268268
if options is None:
269269
options = {}
270270

271-
if remove_button is None:
272-
remove_button = multiple
273-
274-
# Translate remove_button into default selectize plugins
275-
# N.B. remove_button is primarily for multiple=True and clear_button is for
276-
# multiple=False, but both can also be useful in the multiple=True case (i.e., clear
277-
# _all_ selected items)
278-
default_plugins = None
279-
if remove_button == "both":
280-
default_plugins = json.dumps(["remove_button", "clear_button"])
281-
elif remove_button:
282-
default_plugins = json.dumps(["remove_button" if multiple else "clear_button"])
283-
284271
choices_tags = _render_choices(choices_, selected)
285272

286273
selectize_config = None
@@ -293,7 +280,7 @@ def _input_select_impl(
293280
# Which option values should be interpreted as JS?
294281
data_eval=json.dumps(extract_js_keys(options)),
295282
# Supply and retain these plugins across updates (on the client)
296-
data_default_plugins=default_plugins,
283+
data_default_plugins=_get_default_plugins(remove_button, multiple),
297284
)
298285

299286
return div(
@@ -323,17 +310,27 @@ def _normalize_choices(x: SelectChoicesArg) -> _SelectChoices:
323310
return x
324311

325312

326-
def _contains_html(x: _SelectChoices) -> bool:
327-
for v in x.values():
328-
if isinstance(v, Mapping):
329-
# Check the `_Choices` values of `_OptGrpChoices`
330-
for vv in v.values():
331-
if not isinstance(vv, str):
332-
return True
333-
else:
334-
if not isinstance(v, str):
335-
return True
336-
return False
313+
# Translate remove_button into default selectize plugins
314+
# N.B. remove_button is primarily for multiple=True and clear_button is for
315+
# multiple=False, but both can also be useful in the multiple=True case (i.e., clear
316+
# _all_ selected items)
317+
def _get_default_plugins(
318+
remove_button: Optional[Literal[True, False, "both"]],
319+
multiple: bool,
320+
) -> Optional[str]:
321+
if remove_button is None:
322+
remove_button = multiple
323+
324+
if remove_button is False:
325+
return None
326+
327+
if remove_button is True:
328+
return json.dumps(["remove_button" if multiple else "clear_button"])
329+
330+
if remove_button == "both":
331+
return json.dumps(["remove_button", "clear_button"])
332+
333+
raise ValueError(f"Invalid value for `remove_button`: {remove_button}")
337334

338335

339336
def _render_choices(

shiny/ui/_input_update.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,12 @@
3838
from ..types import ActionButtonValue
3939
from ._input_check_radio import ChoicesArg, _generate_options
4040
from ._input_date import _as_date_attr
41-
from ._input_select import SelectChoicesArg, _normalize_choices, _render_choices
41+
from ._input_select import (
42+
SelectChoicesArg,
43+
_normalize_choices,
44+
_render_choices,
45+
_get_default_plugins,
46+
)
4247
from ._input_slider import SliderStepArg, SliderValueArg, _as_numeric, _slider_type
4348
from ._utils import JSEval, _session_on_flush_send_msg, extract_js_keys
4449

@@ -721,7 +726,7 @@ def update_selectize(
721726
label: Optional[TagChild] = None,
722727
choices: Optional[SelectChoicesArg] = None,
723728
selected: Optional[str | list[str]] = None,
724-
remove_button: Optional[bool] = None,
729+
remove_button: Optional[Literal[True, False, "both"]] = None,
725730
options: Optional[dict[str, str | float | JSEval]] = None,
726731
server: bool = False,
727732
session: Optional[Session] = None,
@@ -766,12 +771,8 @@ def update_selectize(
766771

767772
session = require_active_session(session)
768773

769-
# Don't change default plugins unless explicitly specified
770-
default_plugins = None
771-
if remove_button is True:
772-
default_plugins = json.dumps(["remove_button", "clear_button"])
773-
if remove_button is False:
774-
default_plugins = json.dumps([])
774+
# TODO: we don't know if multiple is True or False!
775+
default_plugins = _get_default_plugins(remove_button, multiple=True)
775776

776777
if options is not None or default_plugins is not None:
777778
options = options or {}

0 commit comments

Comments
 (0)