Skip to content

Commit 9080b48

Browse files
committed
support many {shinyWidgets} inputs in reset(); fixes #255
1 parent 2e7f53d commit 9080b48

3 files changed

Lines changed: 132 additions & 3 deletions

File tree

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package: shinyjs
22
Title: Easily Improve the User Experience of Your Shiny Apps in Seconds
3-
Version: 2.1.0.9007
3+
Version: 2.1.0.9008
44
Authors@R: person("Dean", "Attali",
55
email = "daattali@gmail.com",
66
role = c("aut", "cre"),

R/reset.R

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,30 @@ reset <- function(id = "", asis = FALSE) {
137137
funcParams[['start']] <- dates[1]
138138
funcParams[['end']] <- dates[2]
139139
}
140+
141+
# {shinyWidgets} inputs
142+
else if (type == "CalendarPro") {
143+
funcParams[['value']] <- strToVec(value, " ")
144+
} else if (type == "NoUiSlider") {
145+
funcParams[['value']] <- strToVec(value)
146+
} else if (type == "NumericRange") {
147+
funcParams[['value']] <- strToVec(value)
148+
} else if (type == "RadioGroupButtons") {
149+
funcParams[['selected']] <- value
150+
} else if (type == "SliderText") {
151+
funcParams[['selected']] <- strToVec(value)
152+
} else if (type == "SlimSelect") {
153+
funcParams[['selected']] <- strToVec(value)
154+
} else if (type == "Spectrum") {
155+
funcParams[['selected']] <- value
156+
} else if (type == "VirtualSelect") {
157+
if (is.null(value)) {
158+
funcParams[['selected']] <- ""
159+
} else {
160+
funcParams[['selected']] <- strToVec(value)
161+
}
162+
}
163+
140164
else {
141165
funcParams[['value']] <- value
142166
}
@@ -159,7 +183,7 @@ strToVec <- function(str, sep = ",") {
159183
getUpdateFunc <- function(type) {
160184

161185
# get the name of the function
162-
inputsShortUpdateName <- c("RadioButtons")
186+
inputsShortUpdateName <- c("RadioButtons", "CalendarPro", "ColorPickr", "RadioGroupButtons", "SlimSelect", "VirtualSelect")
163187
if (type %in% inputsShortUpdateName) {
164188
updateFunc <- sprintf("update%s", type)
165189
} else {
@@ -168,8 +192,11 @@ getUpdateFunc <- function(type) {
168192

169193
# get the package it's from
170194
pkg <- ""
195+
shinyWidgetsInputs <- c("CalendarPro", "ColorPickr", "Knob", "NoUiSlider", "NumericRange", "RadioGroupButtons", "SliderText", "SlimSelect", "Spectrum", "Time", "VirtualSelect")
171196
if (type == "Colour") {
172197
pkg <- "colourpicker"
198+
} else if (type %in% shinyWidgetsInputs) {
199+
pkg <- "shinyWidgets"
173200
}
174201
if (pkg != "") {
175202
updateFunc <- utils::getFromNamespace(updateFunc, pkg)

inst/srcjs/shinyjs-default-funcs.js

Lines changed: 103 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -299,8 +299,109 @@ shinyjs = function() {
299299
inputValue = null,
300300
inputId = null;
301301

302+
// --- {shinyWidgets} inputs ---
303+
304+
// calendarProInput
305+
if (input.children(".calendar-pro-element").length > 0) {
306+
input = input.children("input");
307+
inputType = "CalendarPro";
308+
inputId = inputContainer.attr('id');
309+
}
310+
// colorPickr
311+
else if (input.children(".pickr-color-container").length > 0) {
312+
input = input.children(".pickr-color-container");
313+
inputType = "ColorPickr";
314+
inputValue = JSON.parse(input.children("script")[0].textContent).options.default;
315+
}
316+
// knobInput
317+
else if (input.children(".knob-input").length > 0) {
318+
input = input.children(".knob-input");
319+
inputType = "Knob";
320+
inputValue = input.data('value');
321+
}
322+
// noUiSliderInput
323+
else if (input.find(".sw-no-ui-slider").length > 0) {
324+
input = input.find(".sw-no-ui-slider");
325+
inputType = "NoUiSlider";
326+
inputValue = JSON.parse(inputContainer.find("script")[0].textContent).start;
327+
}
328+
// numericInputIcon
329+
else if (input.find('.numeric-input-icon').length > 0) {
330+
input = input.find('.numeric-input-icon');
331+
inputType = "Numeric";
332+
}
333+
// numericRangeInput
334+
else if (input.hasClass("shiny-numeric-range-input")) {
335+
inputType = "NumericRange";
336+
inputValue = Array.from(input.find("input[type=number")).map(
337+
input => parseFloat(input.value));
338+
}
339+
// radioGroupButtons
340+
else if (input.hasClass("shiny-input-radiogroup") &&
341+
input.find('.btn-group-container-sw').length > 0 &&
342+
input.find('.radio-group-buttons').length > 0) {
343+
input = input.find('.radio-group-buttons');
344+
inputType = "RadioGroupButtons";
345+
inputValue = input.find("input[type='radio']:checked").val();
346+
}
347+
// searchInput
348+
else if (input.find(".search-text").length > 0) {
349+
input = input.find(".search-text input[type='text']");
350+
inputType = "Text";
351+
}
352+
// sliderTextInput
353+
else if (input.children(".js-range-slider.sw-slider-text").length > 0) {
354+
input = input.children(".js-range-slider.sw-slider-text");
355+
inputType = "SliderText";
356+
let allVals = input.data('swvalues');
357+
inputValue = allVals[input.data('from')];
358+
if (typeof input.data('to') !== "undefined") {
359+
inputValue = inputValue + "," + allVals[input.data('to')];
360+
}
361+
}
362+
// slimSelectInput
363+
else if (input.children(".slim-select").length > 0) {
364+
input = input.children(".slim-select");
365+
inputType = "SlimSelect";
366+
let values = JSON.parse(input.children("script")[0].textContent).selected;
367+
if (values.length <= 1) {
368+
inputValue = values;
369+
} else {
370+
inputValue = values.join(",");
371+
}
372+
}
373+
// spectrumInput
374+
else if (input.children(".sw-spectrum").length > 0) {
375+
input = input.children(".sw-spectrum");
376+
inputType = "Spectrum"
377+
inputValue = input.data('color');
378+
}
379+
// textInputIcon
380+
else if (input.find(".text-input-icon").length > 0) {
381+
input = input.find("input[type='text'].text-input-icon");
382+
inputType = "Text";
383+
}
384+
// timeInput
385+
else if (input.children(".sw-time-input").length > 0) {
386+
input = input.children(".sw-time-input");
387+
inputType = "Time";
388+
}
389+
// virtualSelectInput
390+
else if (input.children(".virtual-select").length > 0) {
391+
input = input.children(".virtual-select");
392+
inputType = "VirtualSelect";
393+
let initParams = JSON.parse(inputContainer.find("script")[0].textContent);
394+
if (typeof initParams.config.selectedValue === "undefined") {
395+
inputValue = initParams.options.choices[0];
396+
} else {
397+
inputValue = initParams.config.selectedValue;
398+
}
399+
}
400+
401+
// --- Native shiny inputs ---
402+
302403
// dateInput
303-
if (input.hasClass("shiny-date-input")) {
404+
else if (input.hasClass("shiny-date-input")) {
304405
input = input.children("input");
305406
inputType = "Date";
306407
inputValue = _getInputDate(input);
@@ -390,6 +491,7 @@ shinyjs = function() {
390491
input = input.find("input[type='file']");
391492
inputType = "File"
392493
}
494+
393495
// if none of the above, no supported Shiny input was found
394496
else {
395497
foundInput = false;

0 commit comments

Comments
 (0)