diff --git a/NEWS.md b/NEWS.md index af9e1cfc..59b18a6e 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,10 @@ +shinyWidgets 0.5.5 +====================== + +* `show_alert()`, `sendSweetAlert()` and `inputSweetAlert()` now accept parameters directly passed to JavaScript method. + + + shinyWidgets 0.5.4 ====================== diff --git a/R/sweetalert.R b/R/sweetalert.R index faa4b3d8..8e9619a4 100644 --- a/R/sweetalert.R +++ b/R/sweetalert.R @@ -60,7 +60,9 @@ useSweetAlert <- function(theme = c("sweetalert2", #' the modal by clicking outside of it, or not. #' @param showCloseButton Show close button in top right corner of the modal. #' @param width Width of the modal (in pixel). +#' @param ... Other arguments passed to JavaScript method. #' +#' @note This function use the JavaScript sweetalert2 library, see the official documentation for more \url{https://sweetalert2.github.io/}. #' #' @importFrom jsonlite toJSON #' @importFrom htmltools tags @@ -83,7 +85,8 @@ sendSweetAlert <- function(session, html = FALSE, closeOnClickOutside = TRUE, showCloseButton = FALSE, - width = NULL) { + width = NULL, + ...) { insertUI( selector = "body", where = "afterBegin", @@ -116,7 +119,8 @@ sendSweetAlert <- function(session, showCancelButton = !is.na(btn_labels[2]), allowOutsideClick = closeOnClickOutside, showCloseButton = showCloseButton, - width = width + width = width, + ... )) ) ) @@ -161,6 +165,7 @@ show_alert <- function(title = "Title", closeOnClickOutside = TRUE, showCloseButton = FALSE, width = NULL, + ..., session = shiny::getDefaultReactiveDomain()) { sendSweetAlert( session = session, @@ -172,7 +177,8 @@ show_alert <- function(title = "Title", html = html, closeOnClickOutside = closeOnClickOutside, showCloseButton = showCloseButton, - width = width + width = width, + ... ) } @@ -338,8 +344,9 @@ ask_confirmation <- function(inputId, #' @param btn_labels Label(s) for button(s). #' @param btn_colors Color(s) for button(s). #' @param reset_input Set the input value to \code{NULL} when alert is displayed. -#' @param ... Additional arguments (not used). +#' @param ... Other arguments passed to JavaScript method. #' +#' @note This function use the JavaScript sweetalert2 library, see the official documentation for more \url{https://sweetalert2.github.io/}. #' #' @importFrom jsonlite toJSON #' @importFrom htmltools tags @@ -418,11 +425,16 @@ ask_confirmation <- function(inputId, #' #' shinyApp(ui = ui, server = server) #' } -inputSweetAlert <- function(session, inputId, title = NULL, - text = NULL, type = NULL, +inputSweetAlert <- function(session, + inputId, + title = NULL, + text = NULL, + type = NULL, input = c("text", "password", "textarea", "radio", "checkbox", "select"), - inputOptions = NULL, inputPlaceholder = NULL, - btn_labels = "Ok", btn_colors = NULL, + inputOptions = NULL, + inputPlaceholder = NULL, + btn_labels = "Ok", + btn_colors = NULL, reset_input = TRUE, ...) { input <- match.arg(input) @@ -461,7 +473,8 @@ inputSweetAlert <- function(session, inputId, title = NULL, confirmButtonColor = btn_colors[1], cancelButtonText = btn_labels[2], cancelButtonColor = btn_colors[2], - showCancelButton = !is.na(btn_labels[2]) + showCancelButton = !is.na(btn_labels[2]), + ... )) ) ) diff --git a/examples/show_alert-ouput.R b/examples/show_alert-ouput.R index 32649f4f..ac76d912 100644 --- a/examples/show_alert-ouput.R +++ b/examples/show_alert-ouput.R @@ -1,5 +1,8 @@ -library("shiny") -library("shinyWidgets") + +# Ouptut in alert ---- + +library(shiny) +library(shinyWidgets) ui <- fluidPage( tags$h1("Click the button to open the alert"), diff --git a/examples/sweetalert-input.R b/examples/sweetalert-input.R new file mode 100644 index 00000000..d4bd9d25 --- /dev/null +++ b/examples/sweetalert-input.R @@ -0,0 +1,81 @@ + +# Input in alert ---- + +library(shiny) +library(shinyWidgets) + + +ui <- fluidPage( + tags$h1("Input sweet alert"), + actionButton(inputId = "text", label = "Text Input"), + verbatimTextOutput(outputId = "text"), + actionButton(inputId = "password", label = "Password Input"), + verbatimTextOutput(outputId = "password"), + actionButton(inputId = "radio", label = "Radio Input"), + verbatimTextOutput(outputId = "radio"), + actionButton(inputId = "checkbox", label = "Checkbox Input"), + verbatimTextOutput(outputId = "checkbox"), + actionButton(inputId = "select", label = "Select Input"), + verbatimTextOutput(outputId = "select") +) +server <- function(input, output, session) { + + observeEvent(input$text, { + inputSweetAlert( + session = session, + inputId = "mytext", + input = "text", + title = "What's your name ?", + inputPlaceholder = "e.g.: Victor", + allowOutsideClick = FALSE, + showCloseButton = TRUE + ) + }) + output$text <- renderPrint(input$mytext) + + observeEvent(input$password, { + inputSweetAlert( + session = session, + inputId = "mypassword", + input = "password", + title = "What's your password ?" + ) + }) + output$password <- renderPrint(input$mypassword) + + observeEvent(input$radio, { + inputSweetAlert( + session = session, + inputId = "myradio", + input = "radio", + inputOptions = c("Banana" , "Orange", "Apple"), + title = "What's your favorite fruit ?" + ) + }) + output$radio <- renderPrint(input$myradio) + + observeEvent(input$checkbox, { + inputSweetAlert( + session = session, + inputId = "mycheckbox", + input = "checkbox", + inputPlaceholder = "Yes I agree", + title = "Do you agree ?" + ) + }) + output$checkbox <- renderPrint(input$mycheckbox) + + observeEvent(input$select, { + inputSweetAlert( + session = session, + inputId = "myselect", + input = "select", + inputOptions = c("Banana" , "Orange", "Apple"), + title = "What's your favorite fruit ?" + ) + }) + output$select <- renderPrint(input$myselect) + +} + +shinyApp(ui = ui, server = server) diff --git a/man/inputSweetAlert.Rd b/man/inputSweetAlert.Rd index 39ce6dcc..4a0d993a 100644 --- a/man/inputSweetAlert.Rd +++ b/man/inputSweetAlert.Rd @@ -45,11 +45,14 @@ If in a Shiny module, it use same logic than inputs : use namespace in UI, not i \item{reset_input}{Set the input value to \code{NULL} when alert is displayed.} -\item{...}{Additional arguments (not used).} +\item{...}{Other arguments passed to JavaScript method.} } \description{ Launch a popup with a text input } +\note{ +This function use the JavaScript sweetalert2 library, see the official documentation for more \url{https://sweetalert2.github.io/}. +} \examples{ if (interactive()) { library("shiny") diff --git a/man/sweetalert.Rd b/man/sweetalert.Rd index 187ff55a..14bbfaa8 100644 --- a/man/sweetalert.Rd +++ b/man/sweetalert.Rd @@ -16,7 +16,8 @@ sendSweetAlert( html = FALSE, closeOnClickOutside = TRUE, showCloseButton = FALSE, - width = NULL + width = NULL, + ... ) show_alert( @@ -29,6 +30,7 @@ show_alert( closeOnClickOutside = TRUE, showCloseButton = FALSE, width = NULL, + ..., session = shiny::getDefaultReactiveDomain() ) } @@ -54,10 +56,15 @@ the modal by clicking outside of it, or not.} \item{showCloseButton}{Show close button in top right corner of the modal.} \item{width}{Width of the modal (in pixel).} + +\item{...}{Other arguments passed to JavaScript method.} } \description{ Show an alert message to the user to provide some feedback. } +\note{ +This function use the JavaScript sweetalert2 library, see the official documentation for more \url{https://sweetalert2.github.io/}. +} \examples{ library(shiny) library(shinyWidgets) @@ -123,8 +130,11 @@ server <- function(input, output, session) { if (interactive()) shinyApp(ui, server) -library("shiny") -library("shinyWidgets") + +# Ouptut in alert ---- + +library(shiny) +library(shinyWidgets) ui <- fluidPage( tags$h1("Click the button to open the alert"),