Skip to content

Commit dfb09ca

Browse files
committed
Let the client handle supplying and retaining default plugins
1 parent 89647ef commit dfb09ca

File tree

2 files changed

+22
-42
lines changed

2 files changed

+22
-42
lines changed

shiny/ui/_input_select.py

Lines changed: 11 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,8 @@
99
"input_select",
1010
"input_selectize",
1111
)
12-
import copy
13-
from json import dumps
14-
from typing import Any, Mapping, Optional, Union, cast
12+
import json
13+
from typing import Mapping, Optional, Union, cast
1514

1615
from htmltools import Tag, TagChild, TagList, css, div, tags
1716

@@ -263,7 +262,6 @@ def _input_select_impl(
263262
options = {}
264263

265264
plugins = _get_default_plugins(remove_button, multiple, choices_)
266-
options = _add_default_plugins(options, plugins)
267265

268266
choices_tags = _render_choices(choices_, selected)
269267

@@ -278,19 +276,16 @@ def _input_select_impl(
278276
multiple=multiple,
279277
size=size,
280278
),
281-
(
282-
TagList(
283-
tags.script(
284-
dumps(options),
285-
type="application/json",
286-
data_for=resolved_id,
287-
data_eval=dumps(extract_js_keys(options)),
288-
),
289-
selectize_deps(),
290-
)
291-
if selectize
292-
else None
279+
tags.script(
280+
json.dumps(options),
281+
type="application/json",
282+
data_for=resolved_id,
283+
# Which option values should be interpreted as JS?
284+
data_eval=json.dumps(extract_js_keys(options)),
285+
# Supply and retain these plugins across updates (on the client)
286+
data_default_plugins=json.dumps(plugins),
293287
),
288+
selectize_deps() if selectize else None,
294289
),
295290
class_="form-group shiny-input-container",
296291
style=css(width=width),
@@ -317,18 +312,6 @@ def _get_default_plugins(
317312
return ()
318313

319314

320-
def _add_default_plugins(
321-
options: dict[str, Any], default_plugins: tuple[str, ...]
322-
) -> dict[str, Any]:
323-
opts = copy.deepcopy(options)
324-
p: list[str] = opts.get("plugins", [])
325-
if not isinstance(p, list):
326-
raise TypeError("`options['plugins']` must be a list.")
327-
p.extend(default_plugins)
328-
opts["plugins"] = p
329-
return opts
330-
331-
332315
def _normalize_choices(x: SelectChoicesArg) -> _SelectChoices:
333316
if x is None:
334317
raise TypeError("`choices` must be a list, tuple, or dict.")

shiny/ui/_input_update.py

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,7 @@
3636
from ..types import ActionButtonValue
3737
from ._input_check_radio import ChoicesArg, _generate_options
3838
from ._input_date import _as_date_attr
39-
from ._input_select import (
40-
SelectChoicesArg,
41-
_add_default_plugins,
42-
_normalize_choices,
43-
_render_choices,
44-
)
39+
from ._input_select import SelectChoicesArg, _normalize_choices, _render_choices
4540
from ._input_slider import SliderStepArg, SliderValueArg, _as_numeric, _slider_type
4641
from ._utils import JSEval, _session_on_flush_send_msg, extract_js_keys
4742

@@ -769,20 +764,22 @@ def update_selectize(
769764

770765
session = require_active_session(session)
771766

772-
if remove_button:
773-
options = _add_default_plugins(options or {}, ("remove_button", "clear_button"))
774-
775-
if options is not None:
776-
if remove_button is None:
777-
# clear_button is problematic when multiple=False
778-
# and there is no empty choice
779-
options = _add_default_plugins(options, ("remove_button",))
767+
# Unless specified otherwise, the default plugins are retained
768+
# across updates (by the client)
769+
default_plugins = None
770+
if remove_button is True:
771+
default_plugins = json.dumps(["remove_button", "clear_button"])
772+
if remove_button is False:
773+
default_plugins = json.dumps([])
780774

775+
if options is not None or default_plugins is not None:
776+
options = options or {}
781777
cfg = tags.script(
782778
json.dumps(options),
783779
type="application/json",
784780
data_for=resolve_id(id),
785781
data_eval=json.dumps(extract_js_keys(options)),
782+
data_default_plugins=default_plugins,
786783
)
787784
session.send_input_message(id, {"config": cfg.get_html_string()})
788785

0 commit comments

Comments
 (0)