Skip to content
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

Add icon to loadingButton #64

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 26 additions & 2 deletions R/loadingButton.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#'
#' @param inputId the input id
#' @param label the button text (label)
#' @param icon the button icon (default: \code{NULL}).
#' @param class the class(es) to apply to the button
#' @param style style for button (pre-loading); character string w/ CSS styling format: "color: black; background-color: red;"
#' @param loadingLabel text to show after button is clicked (e.g. during loading)
Expand All @@ -22,6 +23,7 @@
loadingButton <- function(
inputId,
label,
icon = NULL,
class = "btn btn-primary",
style = "width: 150px;",
loadingLabel = "Loading...",
Expand All @@ -36,8 +38,12 @@ loadingButton <- function(
if (is.null(loadingClass)) loadingClass <- class
if (is.null(loadingStyle)) loadingStyle <- style

# Validate the icon
icon <- validateIcon(icon)

rOptions <- list(
"label" = label,
"label" = label,
"icon" = icon$attribs$class,
"class" = class,
"style" = style,
"loadingLabel" = loadingLabel,
Expand All @@ -57,7 +63,7 @@ loadingButton <- function(
id = inputId,
class = class,
style = style,
label
list(icon, label)
),
tags$head(
htmltools::singleton(
Expand Down Expand Up @@ -97,3 +103,21 @@ resetLoadingButton <- function(inputId, session = shiny::getDefaultReactiveDomai
)
)
}


#' validateIcon
#'
#' Validate the icon (from \code{shiny:::validateIcon})
#'
#' @noRd
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")
}
}
11 changes: 9 additions & 2 deletions inst/assets/js/loadingbutton.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,11 @@ LoadingButtons.prototype.create = function (inputId, options) {
var loading_button = $(this);

loading_button.attr('disabled', true);
loading_button.html('<i class="fas fa-' + options.loadingSpinner + ' fa-spin"></i> ' + options.loadingLabel);
loading_button.html(
'<i class="fas fa-' + options.loadingSpinner + ' fa-spin"></i> ' +
'<i class="' + options.icon + '"></i> ' +
options.loadingLabel
);
loading_button.attr('style', options.loadingStyle);
loading_button.removeClass(options["class"]);
loading_button.addClass(options.loadingClass);
Expand All @@ -65,7 +69,10 @@ LoadingButtons.prototype.resetLoading = function (inputId) {

// restore the active (i.e. non loading styles)
loading_button.attr('disabled', false);
loading_button.html(options_.label);
loading_button.html(
'<i class="' + options_.icon + '"></i> ' +
options_.label
);
loading_button.attr('style', options_.style);
loading_button.removeClass(options_.loadingClass);
loading_button.addClass(options_["class"]);
Expand Down
3 changes: 3 additions & 0 deletions man/loadingButton.Rd

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