Skip to content

Commit 24f0668

Browse files
committed
Don't mutate options; better typing pattern
1 parent 980296d commit 24f0668

File tree

5 files changed

+50
-48
lines changed

5 files changed

+50
-48
lines changed

inst/www/shared/shiny.js

Lines changed: 12 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

inst/www/shared/shiny.js.map

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

inst/www/shared/shiny.min.js

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

inst/www/shared/shiny.min.js.map

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

srcts/src/bindings/input/selectInput.ts

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,21 @@ type SelectInputReceiveMessageData = {
1313
value?: string;
1414
};
1515

16-
type SelectizeOptions = Selectize.IOptions<string, unknown>;
1716
type SelectizeInfo = Selectize.IApi<string, unknown> & {
18-
settings: SelectizeOptions;
17+
settings: Selectize.IOptions<string, unknown>;
1918
};
2019

21-
// Extend SelectizeOptions to include shinyRemoveButton option
22-
// Currently, py-shiny leverages this option to power the
23-
// remove_button parameter.
20+
type SelectizeOptions = Selectize.IOptions<string, unknown> & {
21+
// Provide some stronger typing for the Selectize options
22+
labelField: "label";
23+
valueField: "value";
24+
searchField: ["label"];
25+
onItemRemove?: (value: string) => void;
26+
onDropdownClose?: () => void;
27+
};
28+
29+
// Adds a py-shiny specific "option" that makes the
30+
// input_selectize(remove_button) parameter possible
2431
type SelectizeShinyOptions = SelectizeOptions & {
2532
shinyRemoveButton?: "none" | "true" | "false" | "both";
2633
};
@@ -251,13 +258,7 @@ class SelectInputBinding extends InputBinding {
251258

252259
if (config.length === 0) return undefined;
253260

254-
let options: SelectizeOptions & {
255-
labelField: "label";
256-
valueField: "value";
257-
searchField: ["label"];
258-
onItemRemove?: (value: string) => void;
259-
onDropdownClose?: () => void;
260-
} = $.extend(
261+
let options: SelectizeShinyOptions = $.extend(
261262
{
262263
labelField: "label",
263264
valueField: "value",
@@ -266,7 +267,7 @@ class SelectInputBinding extends InputBinding {
266267
JSON.parse(config.html()),
267268
);
268269

269-
this._addShinyRemoveButton(options, el.hasAttribute("multiple"));
270+
options = this._addShinyRemoveButton(options, el.hasAttribute("multiple"));
270271

271272
// selectize created from selectInput()
272273
if (typeof config.data("nonempty") !== "undefined") {
@@ -319,10 +320,10 @@ class SelectInputBinding extends InputBinding {
319320
private _addShinyRemoveButton(
320321
options: SelectizeShinyOptions,
321322
multiple: boolean,
322-
): void {
323+
): SelectizeOptions {
323324
let removeButton = options.shinyRemoveButton;
324325
if (removeButton === undefined) {
325-
return;
326+
return options;
326327
}
327328

328329
// None really means 'smart default'
@@ -331,7 +332,7 @@ class SelectInputBinding extends InputBinding {
331332
}
332333

333334
if (removeButton === "false") {
334-
return;
335+
return options;
335336
}
336337

337338
const plugins = [];
@@ -341,16 +342,16 @@ class SelectInputBinding extends InputBinding {
341342
plugins.push(multiple ? "remove_button" : "clear_button");
342343
}
343344

344-
const optionPlugins = options.plugins || [];
345-
346-
plugins.forEach((plugin) => {
347-
if (!optionPlugins.includes(plugin)) {
348-
optionPlugins.push(plugin);
349-
}
350-
});
351-
352-
options.plugins = optionPlugins;
353-
delete options.shinyRemoveButton;
345+
// Add plugins to existing plugins if not already present
346+
return {
347+
...options,
348+
plugins: Array.from(
349+
new Set([
350+
...(Array.isArray(options.plugins) ? options.plugins : []),
351+
...plugins,
352+
]),
353+
),
354+
};
354355
}
355356
}
356357

0 commit comments

Comments
 (0)