-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adding button with "reload" functionality. #1
Changes from all commits
74102fa
9987002
c3c97e9
dd8f9e1
a71e16b
d7a867e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,5 @@ | |
|
||
export(hover_action_button) | ||
export(hover_download_button) | ||
export(hover_reload_button) | ||
export(use_hover) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
#' Reload button with button and icon animations | ||
#' | ||
#' Animate a reload button and it's icon using | ||
#' [Hover.css](https://github.com/IanLunn/Hover) | ||
#' Note that a reload button is just a shiny::actionButton | ||
#' with onClick behavior to reload or refresh a web browser. | ||
#' | ||
#' @source <https://github.com/IanLunn/Hover> | ||
#' | ||
#' @param inputId The input slot that will be used to access the value. | ||
#' @param label The contents of the button or link–usually a text label, but you could also use any other HTML, like an image. | ||
#' @param icon An optional icon() to appear on the button. | ||
#' @param button_animation The name of the button animation. | ||
#' @param icon_animation The name of the icon animation. | ||
#' @param width The width of the input, e.g. '400px', or '100 percent'; see validateCssUnit(). | ||
#' @param ... Named attributes to be applied to the button or link. | ||
#' @md | ||
#' @examples | ||
#' if (interactive()) { | ||
#' library(shiny) | ||
#' library(hover) | ||
#' | ||
#' ui <- fluidPage( | ||
#' use_hover(), | ||
#' hover_reload_button( | ||
#' inputId = "btn", | ||
#' label = "hello hover!", | ||
#' icon = icon("refresh"), | ||
#' button_animation = "rotate", | ||
#' icon_animation = "spin" | ||
#' ) | ||
#' ) | ||
#' | ||
#' server <- function(input, output, session) { | ||
#' | ||
#' } | ||
#' | ||
#' shinyApp(ui, server) | ||
#' } | ||
#' @export | ||
hover_reload_button <- function(inputId, label, icon = NULL, button_animation = NULL, icon_animation = NULL, width = NULL, ...) { | ||
value <- shiny::restoreInput(id = inputId, default = NULL) | ||
|
||
if (!is.null(icon)) { | ||
icon <- validateIcon(htmltools::tagAppendAttributes(icon, class = "hvr-icon")) | ||
} | ||
|
||
shiny::tags$button( | ||
id = inputId, | ||
style = if (!is.null(width)) paste0("width: ", shiny::validateCssUnit(width), ";"), | ||
type = "button", | ||
class = "btn btn-default reload-button", | ||
class = if (!is.null(button_animation)) paste0("hvr-", button_animation), | ||
class = if (!is.null(icon_animation)) paste0("hvr-icon-", icon_animation), | ||
`data-val` = value, | ||
list(icon, label), | ||
onClick = "location.reload();", | ||
... | ||
) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
validateIcon <- function (icon) { | ||
if (is.null(icon) || identical(icon, character(0))) { | ||
return(icon) | ||
} | ||
else if (inherits(icon, "shiny.tag") && icon$name == "i") { | ||
return(icon) | ||
} | ||
else { | ||
stop("Invalid icon. Use Shiny's 'icon()' function to generate a valid icon") | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
test_that("hover_reload_button has expected structure", { | ||
|
||
# Create testing input | ||
test_me <- hover_reload_button("id", "label") | ||
|
||
# Test 1: attribs type is a button | ||
expect_equal( | ||
## actual | ||
test_me$attribs$type, | ||
## expect | ||
"button" | ||
) | ||
|
||
# Test 2: names on attribs list contains onClick | ||
expect_true(any("onClick" %in% names(test_me$attribs))) | ||
|
||
# Test 3: Look for javascript command | ||
expect_equal( | ||
## actual | ||
test_me$attribs$onClick, | ||
## expect | ||
"location.reload();" | ||
) | ||
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since
reloadButton
isn't an actual button type fromshiny
, let's reword: