Skip to content

Commit

Permalink
updated label update method + doc
Browse files Browse the repository at this point in the history
  • Loading branch information
pvictor committed Aug 30, 2023
1 parent db0ea2f commit fa28e94
Show file tree
Hide file tree
Showing 21 changed files with 258 additions and 52 deletions.
11 changes: 10 additions & 1 deletion R/color-pickr.R
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
#' See online documentation for more information: \url{https://github.com/Simonwep/pickr}.
#'
#' @return a color picker input widget that can be added to the UI of a shiny app.
#'
#' @seealso [updateColorPickr()] for updating from server.
#'
#' @export
#'
#'
Expand Down Expand Up @@ -145,16 +148,21 @@ colorPickr <- function(inputId,
#'
#' @param session The session object passed to function given to shinyServer.
#' @param inputId The id of the input object.
#' @param label The label to set for the input object.
#' @param value The value to set for the input object.
#' @param action Action to perform on color-picker: enable, disable, show or hide.
#' @param swatches Optional color swatches.
#'
#' @return No return value.
#'
#' @seealso [colorPickr()] for creating a widget in the UI.
#'
#' @export
#'
#' @rdname colorPickr
#' @example examples/updateColorPickr.R
updateColorPickr <- function(session = getDefaultReactiveDomain(),
inputId,
label = NULL,
value = NULL,
action = NULL,
swatches = NULL) {
Expand All @@ -163,6 +171,7 @@ updateColorPickr <- function(session = getDefaultReactiveDomain(),
}
message <- dropNulls(list(
value = value,
label = label,
action = action,
swatches = list1(swatches)
))
Expand Down
9 changes: 8 additions & 1 deletion R/virtual-select.R
Original file line number Diff line number Diff line change
Expand Up @@ -188,14 +188,21 @@ virtualSelectInput <- function(inputId,



#' Update virtual select from server
#' @title Update virtual select from server
#'
#' @description
#' Update a [virtualSelectInput()] from the server.
#'
#'
#' @inheritParams virtualSelectInput
#' @inheritParams shiny::updateSelectInput
#' @param disable Disable (`TRUE`) or enable (`FALSE`) the select menu.
#' @param disabledChoices List of disabled option's values.
#'
#' @return No value.
#'
#' @seealso [virtualSelectInput()] for creating a widget in the UI.
#'
#' @export
#'
#' @importFrom shiny getDefaultReactiveDomain
Expand Down
77 changes: 77 additions & 0 deletions examples/updateColorPickr.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
library(shiny)
library(shinyWidgets)

ui <- fluidPage(
tags$h2("Update colorPickr example"),
fluidRow(
column(
width = 6,
colorPickr(
inputId = "id1",
label = "Update this label:",
width = "300px"
),
textInput(
inputId = "update_label",
label = "Update label above :"
)
),
column(
width = 6,
colorPickr(
inputId = "id2",
label = "Swatches :",
selected = "#440154",
swatches = c(
scales::viridis_pal()(9)
),
update = "change",
opacity = FALSE,
preview = FALSE,
hue = FALSE,
interaction = list(
hex= FALSE,
rgba = FALSE,
input = FALSE,
save = FALSE,
clear = FALSE
),
pickr_width = "245px",
inline = TRUE
),
verbatimTextOutput("res"),
actionButton("red", "Update red"),
actionButton("green", "Update green"),
actionButton("blue", "Update blue")
)
)
)

server <- function(input, output, session) {

observeEvent(
input$update_label,
updateColorPickr(inputId = "id1", label = input$update_label),
ignoreInit = TRUE
)


output$res <- renderPrint(input$id1)

observeEvent(
input$red,
updateColorPickr(inputId = "id2", swatches = scales::brewer_pal(palette = "Reds")(9))
)
observeEvent(
input$green,
updateColorPickr(inputId = "id2", swatches = scales::brewer_pal(palette = "Greens")(9))
)
observeEvent(
input$blue,
updateColorPickr(inputId = "id2", swatches = scales::brewer_pal(palette = "Blues")(9))
)

}

if (interactive())
shinyApp(ui, server)
2 changes: 1 addition & 1 deletion inst/packer/air-datepicker.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion inst/packer/autonumeric.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion inst/packer/colorpickr.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion inst/packer/multi.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion inst/packer/nouislider.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion inst/packer/tree.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion inst/packer/virtual-select.js

Large diffs are not rendered by default.

24 changes: 5 additions & 19 deletions man/colorPickr.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

116 changes: 116 additions & 0 deletions man/updateColorPickr.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion man/updateVirtualSelect.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 3 additions & 6 deletions srcjs/inputs/air-datepicker.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import $ from "jquery";
import "shiny";
import { updateLabel } from "../modules/utils";
import dayjs from "dayjs";
import dayjsPluginUTC from "dayjs-plugin-utc";
dayjs.extend(dayjsPluginUTC);
Expand Down Expand Up @@ -265,12 +266,8 @@ $.extend(AirDatepickerBindings, {
if (data.hasOwnProperty("value")) AirDatepickerBindings.setValue(el, data.value);

if (data.hasOwnProperty("label")) {
// console.log(el);
$(el)
.parent()
.parent()
.find('label[for="' + data.id + '"]')
.text(data.label);
var label = $("#" + el.id + "-label");
updateLabel(data.label, label);
}

if (data.hasOwnProperty("options")) {
Expand Down
11 changes: 5 additions & 6 deletions srcjs/inputs/autonumeric.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import $ from "jquery";
import "shiny";
import { updateLabel } from "../modules/utils";
import AutoNumeric from "autonumeric";

Shiny.InputBinding.prototype.store = [];
Expand Down Expand Up @@ -61,12 +62,10 @@ $.extend(autonumericInputBinding, {
autonum.update(data.options);
}

if (data.hasOwnProperty("label"))
$(el)
.parent()
.parent()
.find('label[for="' + Shiny.$escape(el.id) + '"]')
.text(data.label);
if (data.hasOwnProperty("label")) {
var label = $("#" + el.id + "-label");
updateLabel(data.label, label);
}

$(el).trigger("change");
},
Expand Down
9 changes: 9 additions & 0 deletions srcjs/inputs/colorpickr.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import $ from "jquery";
import "shiny";
import { updateLabel } from "../modules/utils";
import '@simonwep/pickr/dist/themes/classic.min.css';
import '@simonwep/pickr/dist/themes/monolith.min.css';
import '@simonwep/pickr/dist/themes/nano.min.css';
Expand Down Expand Up @@ -55,6 +58,12 @@ $.extend(pickrColorBinding, {
//$(el).off(".pickrColorBinding");
},
receiveMessage: function(el, data) {

if (data.hasOwnProperty("label")) {
var label = $("#" + el.id + "-label");
updateLabel(data.label, label);
}

var pickr = this.getPickr(el);
if (data.hasOwnProperty("swatches")) {
for (let i = pickr._swatchColors.length - 1; i > -1; i--) {
Expand Down
Loading

0 comments on commit fa28e94

Please sign in to comment.