Skip to content

Commit

Permalink
show_alert, sendSweetAlert and inputSweetAlert now accept parameters …
Browse files Browse the repository at this point in the history
…directly passed to JavaScript method.
  • Loading branch information
pvictor committed Oct 19, 2020
1 parent afb956a commit 726a547
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 15 deletions.
7 changes: 7 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -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
======================

Expand Down
31 changes: 22 additions & 9 deletions R/sweetalert.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -83,7 +85,8 @@ sendSweetAlert <- function(session,
html = FALSE,
closeOnClickOutside = TRUE,
showCloseButton = FALSE,
width = NULL) {
width = NULL,
...) {
insertUI(
selector = "body",
where = "afterBegin",
Expand Down Expand Up @@ -116,7 +119,8 @@ sendSweetAlert <- function(session,
showCancelButton = !is.na(btn_labels[2]),
allowOutsideClick = closeOnClickOutside,
showCloseButton = showCloseButton,
width = width
width = width,
...
))
)
)
Expand Down Expand Up @@ -161,6 +165,7 @@ show_alert <- function(title = "Title",
closeOnClickOutside = TRUE,
showCloseButton = FALSE,
width = NULL,
...,
session = shiny::getDefaultReactiveDomain()) {
sendSweetAlert(
session = session,
Expand All @@ -172,7 +177,8 @@ show_alert <- function(title = "Title",
html = html,
closeOnClickOutside = closeOnClickOutside,
showCloseButton = showCloseButton,
width = width
width = width,
...
)
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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]),
...
))
)
)
Expand Down
7 changes: 5 additions & 2 deletions examples/show_alert-ouput.R
Original file line number Diff line number Diff line change
@@ -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"),
Expand Down
81 changes: 81 additions & 0 deletions examples/sweetalert-input.R
Original file line number Diff line number Diff line change
@@ -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)
5 changes: 4 additions & 1 deletion man/inputSweetAlert.Rd

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

16 changes: 13 additions & 3 deletions man/sweetalert.Rd

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

0 comments on commit 726a547

Please sign in to comment.