diff --git a/R/input-search.R b/R/input-search.R index 39cd3359..2045966d 100644 --- a/R/input-search.R +++ b/R/input-search.R @@ -9,75 +9,51 @@ #' @param placeholder A character string giving the user a hint as to what can be entered into the control. #' @param btnSearch An icon for the button which validate the search. #' @param btnReset An icon for the button which reset the search. -#' @param resetValue Value used when reset button is clicked, default to \code{""}, -#' if \code{NULL} value is not reset. +#' @param btnClass Class to add to buttons, if a vector of length 2 the first value is used for search button and second one for reset button. +#' @param resetValue Value used when reset button is clicked, default to `""` (empty string), +#' if `NULL` value is not reset. #' @param width The width of the input, e.g. `400px`, or `100%`. #' -#' @note The two buttons ('search' and 'reset') act like \code{actionButton}, you can -#' retrieve their value server-side with \code{input$_search} and \code{input$_reset}. +#' @note The two buttons ('search' and 'reset') act like [shiny::actionButton()], you can +#' retrieve their value server-side with `input$_search` and `input$_reset`. #' -#' @seealso \link{updateSearchInput} to update value server-side. +#' @seealso [updateSearchInput()] to update value server-side. #' -#' @examples -#' if (interactive()) { -#' ui <- fluidPage( -#' tags$h1("Search Input"), -#' br(), -#' searchInput( -#' inputId = "search", label = "Enter your text", -#' placeholder = "A placeholder", -#' btnSearch = icon("magnifying-glass"), -#' btnReset = icon("xmark"), -#' width = "450px" -#' ), -#' br(), -#' verbatimTextOutput(outputId = "res") -#' ) -#' -#' server <- function(input, output, session) { -#' output$res <- renderPrint({ -#' input$search -#' }) -#' } -#' -#' shinyApp(ui = ui, server = server) -#' } #' #' @importFrom shiny restoreInput #' @importFrom htmltools tags css validateCssUnit singleton #' #' @export +#' +#' @example examples/searchInput.R searchInput <- function(inputId, label = NULL, value = "", placeholder = NULL, btnSearch = NULL, btnReset = NULL, + btnClass = "btn-default btn-outline-secondary", resetValue = "", width = NULL) { value <- shiny::restoreInput(id = inputId, default = value) - + btnClass <- rep_len(btnClass, length.out = 2) tagSearch <- htmltools::tags$button( - class = "btn btn-default btn-addon action-button", + class = "btn btn-addon action-button", + class = btnClass[1], id = paste0(inputId, "_search"), type = "button", btnSearch, style = if (is.null(btnSearch)) css(display = "none") ) tagReset <- htmltools::tags$button( - class = "btn btn-default btn-addon action-button", + class = "btn btn-addon action-button", + class = btnClass[2], id = paste0(inputId, "_reset"), type = "button", btnReset, style = if (is.null(btnReset)) css(display = "none") ) - css_btn_addon <- paste0( - ".btn-addon{", "font-size:14.5px;", - "margin:0 0 0 0 !important;", - "display: inline-block !important;", "}" - ) - htmltools::tags$div( class = "form-group shiny-input-container", style = css(width = validateCssUnit(width)), @@ -100,7 +76,6 @@ searchInput <- function(inputId, theme_func = shiny::getCurrentTheme ) ), - singleton(tags$head(tags$style(css_btn_addon))), html_dependency_input_icons() ) } diff --git a/examples/searchInput.R b/examples/searchInput.R new file mode 100644 index 00000000..d8d576c7 --- /dev/null +++ b/examples/searchInput.R @@ -0,0 +1,24 @@ +library(shiny) +library(shinyWidgets) + +ui <- fluidPage( + # theme = bslib::bs_theme(version = 5L, preset = "bootstrap"), + tags$h1("Search Input"), + br(), + searchInput( + inputId = "search", label = "Enter your text", + placeholder = "A placeholder", + btnSearch = icon("magnifying-glass"), + btnReset = icon("xmark"), + width = "450px" + ), + br(), + verbatimTextOutput(outputId = "res") +) + +server <- function(input, output, session) { + output$res <- renderPrint(input$search) +} + +if (interactive()) + shinyApp(ui = ui, server = server) diff --git a/man/searchInput.Rd b/man/searchInput.Rd index 905992c8..eb4b5df1 100644 --- a/man/searchInput.Rd +++ b/man/searchInput.Rd @@ -11,6 +11,7 @@ searchInput( placeholder = NULL, btnSearch = NULL, btnReset = NULL, + btnClass = "btn-default btn-outline-secondary", resetValue = "", width = NULL ) @@ -28,7 +29,9 @@ searchInput( \item{btnReset}{An icon for the button which reset the search.} -\item{resetValue}{Value used when reset button is clicked, default to \code{""}, +\item{btnClass}{Class to add to buttons, if a vector of length 2 the first value is used for search button and second one for reset button.} + +\item{resetValue}{Value used when reset button is clicked, default to \code{""} (empty string), if \code{NULL} value is not reset.} \item{width}{The width of the input, e.g. \verb{400px}, or \verb{100\%}.} @@ -37,35 +40,35 @@ if \code{NULL} value is not reset.} A text input only triggered when Enter key is pressed or search button clicked } \note{ -The two buttons ('search' and 'reset') act like \code{actionButton}, you can -retrieve their value server-side with \code{input$_search} and \code{input$_reset}. +The two buttons ('search' and 'reset') act like \code{\link[shiny:actionButton]{shiny::actionButton()}}, you can +retrieve their value server-side with \verb{input$_search} and \verb{input$_reset}. } \examples{ -if (interactive()) { - ui <- fluidPage( - tags$h1("Search Input"), - br(), - searchInput( - inputId = "search", label = "Enter your text", - placeholder = "A placeholder", - btnSearch = icon("magnifying-glass"), - btnReset = icon("xmark"), - width = "450px" - ), - br(), - verbatimTextOutput(outputId = "res") - ) +library(shiny) +library(shinyWidgets) - server <- function(input, output, session) { - output$res <- renderPrint({ - input$search - }) - } +ui <- fluidPage( + # theme = bslib::bs_theme(version = 5L, preset = "bootstrap"), + tags$h1("Search Input"), + br(), + searchInput( + inputId = "search", label = "Enter your text", + placeholder = "A placeholder", + btnSearch = icon("magnifying-glass"), + btnReset = icon("xmark"), + width = "450px" + ), + br(), + verbatimTextOutput(outputId = "res") +) - shinyApp(ui = ui, server = server) +server <- function(input, output, session) { + output$res <- renderPrint(input$search) } +if (interactive()) + shinyApp(ui = ui, server = server) } \seealso{ -\link{updateSearchInput} to update value server-side. +\code{\link[=updateSearchInput]{updateSearchInput()}} to update value server-side. }