Prevent .selectize() updates from destroying .data() and event listeners (#3923)

Co-authored-by: cpsievert <cpsievert@users.noreply.github.com>
This commit is contained in:
Carson Sievert
2023-10-26 17:42:06 -05:00
committed by GitHub
parent 97a12ec601
commit a1b9fda809
9 changed files with 989 additions and 993 deletions

View File

@@ -1064,14 +1064,8 @@ $.extend(Selectize.prototype, {
self.ignoreHover = self.settings.ignoreHover;
});
var inputPlaceholder = $('<div></div>');
var inputChildren = $input.children().detach();
$input.replaceWith(inputPlaceholder);
inputPlaceholder.replaceWith($input);
this.revertSettings = {
$children : inputChildren,
$children : $input.children().detach(),
tabindex : $input.attr('tabindex')
};

File diff suppressed because one or more lines are too long

File diff suppressed because it is too large Load Diff

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@@ -2,7 +2,6 @@ import $ from "jquery";
import { InputBinding } from "./inputBinding";
import { $escape, hasDefinedProperty, updateLabel } from "../../utils";
import { indirectEval } from "../../utils/eval";
import { shinyBindAll, shinyUnbindAll } from "../../shiny/initedMethods";
import type { NotUndefined } from "../../utils/extraTypes";
type SelectHTMLElement = HTMLSelectElement & { nonempty: boolean };
@@ -285,35 +284,18 @@ class SelectInputBinding extends InputBinding {
// @ts-expect-error; Need to type `options` keys to know exactly which values are accessed.
options[x] = indirectEval("(" + options[x] + ")");
});
let control = this._newSelectize($el, options);
let control = $el.selectize(options)[0].selectize as SelectizeInfo;
// .selectize() does not really update settings; must destroy and rebuild
if (update) {
const settings = $.extend(control.settings, options);
control.destroy();
control = this._newSelectize($el, settings);
control = $el.selectize(settings)[0].selectize as SelectizeInfo;
}
return control;
}
protected _newSelectize(
$el: JQuery<HTMLSelectElement>,
options: SelectizeOptions
): SelectizeInfo {
// Starting with selectize v0.15.2, $el.selectize() can prune the <select>
// element from the DOM, meaning that if we're already bound to it, we'll
// lose the binding. So if we are bound, unbind first, then rebind after.
// (Note this is quite similar to how Shiny.renderContent() works.)
const binding = $el.data("shiny-input-binding");
if (binding) shinyUnbindAll($el.parent());
const control = $el.selectize(options)[0].selectize as SelectizeInfo;
/* eslint-disable @typescript-eslint/no-floating-promises */
if (binding) shinyBindAll($el.parent());
return control;
}
}
export { SelectInputBinding };

View File

@@ -34,7 +34,6 @@ declare class SelectInputBinding extends InputBinding {
unsubscribe(el: HTMLElement): void;
initialize(el: SelectHTMLElement): void;
protected _selectize(el: SelectHTMLElement, update?: boolean): SelectizeInfo | undefined;
protected _newSelectize($el: JQuery<HTMLSelectElement>, options: SelectizeOptions): SelectizeInfo;
}
export { SelectInputBinding };
export type { SelectInputReceiveMessageData };

View File

@@ -0,0 +1,32 @@
diff --git a/inst/www/shared/selectize/js/selectize.js b/inst/www/shared/selectize/js/selectize.js
index 767f45e1c..a902fae5b 100644
--- a/inst/www/shared/selectize/js/selectize.js
+++ b/inst/www/shared/selectize/js/selectize.js
@@ -1064,14 +1064,8 @@ $.extend(Selectize.prototype, {
self.ignoreHover = self.settings.ignoreHover;
});
- var inputPlaceholder = $('<div></div>');
- var inputChildren = $input.children().detach();
-
- $input.replaceWith(inputPlaceholder);
- inputPlaceholder.replaceWith($input);
-
this.revertSettings = {
- $children : inputChildren,
+ $children : $input.children().detach(),
tabindex : $input.attr('tabindex')
};
diff --git a/inst/www/shared/shiny.js b/inst/www/shared/shiny.js
index aeff68aa3..db9880f26 100644
Binary files a/inst/www/shared/shiny.js and b/inst/www/shared/shiny.js differ
diff --git a/inst/www/shared/shiny.js.map b/inst/www/shared/shiny.js.map
index 34350b024..1abf54644 100644
Binary files a/inst/www/shared/shiny.js.map and b/inst/www/shared/shiny.js.map differ
diff --git a/inst/www/shared/shiny.min.js b/inst/www/shared/shiny.min.js
index 8b8027fd3..eb1994aac 100644
Binary files a/inst/www/shared/shiny.min.js and b/inst/www/shared/shiny.min.js differ
diff --git a/inst/www/shared/shiny.min.js.map b/inst/www/shared/shiny.min.js.map
index 560e1492e..0de12987b 100644
Binary files a/inst/www/shared/shiny.min.js.map and b/inst/www/shared/shiny.min.js.map differ