10
10
"input_selectize" ,
11
11
)
12
12
import json
13
- from typing import Mapping , Optional , Union , cast
13
+ from typing import Literal , Mapping , Optional , Union , cast
14
14
15
15
from htmltools import Tag , TagChild , TagList , css , div , tags
16
16
@@ -58,7 +58,7 @@ def input_selectize(
58
58
selected : Optional [str | list [str ]] = None ,
59
59
multiple : bool = False ,
60
60
width : Optional [str ] = None ,
61
- remove_button : bool = True ,
61
+ remove_button : Optional [ Literal [ True , False , "both" ]] = None ,
62
62
options : Optional [dict [str , Jsonifiable | JSEval ]] = None ,
63
63
) -> Tag :
64
64
"""
@@ -84,10 +84,16 @@ def input_selectize(
84
84
width
85
85
The CSS width, e.g. '400px', or '100%'
86
86
remove_button
87
- Whether to add a remove button. When `True` (the default), both the
88
- 'clear_button' and 'remove_button' plugins are included. If you want just one
89
- plugin, set this to `False` and specify the desired plugin in the `options`
90
- argument (i.e., `options = {"plugins": ["remove_button"]}`).
87
+ Whether to add a remove button. The following values are supported:
88
+
89
+ - `None` (the default): The 'remove_button' selection plugin is included when
90
+ `multiple=True`.
91
+ - `True`: Same as `None` in the `multiple=True` case, but when `multiple=False`,
92
+ the 'clear_button' plugin is included.
93
+ - `False`: No plugins are included.
94
+ - `"both"`: Both 'remove_button' and 'clear_button' plugins are included. This
95
+ is useful for being able to clear each and all selected items when
96
+ `multiple=True`.
91
97
options
92
98
A dictionary of options. See the documentation of selectize.js for possible options.
93
99
If you want to pass a JavaScript function, wrap the string in `ui.JS`.
@@ -245,7 +251,7 @@ def _input_select_impl(
245
251
selectize : bool = False ,
246
252
width : Optional [str ] = None ,
247
253
size : Optional [str ] = None ,
248
- remove_button : bool = True ,
254
+ remove_button : Literal [ True , False , "both" , None ] = None ,
249
255
options : Optional [dict [str , Jsonifiable | JSEval ]] = None ,
250
256
) -> Tag :
251
257
if options is not None and selectize is False :
@@ -262,15 +268,18 @@ def _input_select_impl(
262
268
if options is None :
263
269
options = {}
264
270
265
- # Although 'remove_button' is primarily for multiple=True and 'clear_button' is for
266
- # multiple=False, we include both in either case since:
267
- # 1. When multiple=True, 'clear_button' can be used to clear _all_ selected items.
268
- # 2. When multiple=False, 'remove_button' is effectively a no-op.
269
- # 3. By including both, we can simplify the client-side logic needed to retain
270
- # these plugins across update_selectize(options=...) calls
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)
271
278
default_plugins = None
272
- if remove_button or remove_button is None :
279
+ if remove_button == "both" :
273
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" ])
274
283
275
284
choices_tags = _render_choices (choices_ , selected )
276
285
0 commit comments