Skip to content

Commit

Permalink
Working to integrate ThomasSiegmund's shinyTypeahead package to retur…
Browse files Browse the repository at this point in the history
…n typeahead functionality.
  • Loading branch information
ebailey78 committed Mar 31, 2015
1 parent fccf2c3 commit f2e2fc8
Show file tree
Hide file tree
Showing 9 changed files with 674 additions and 2 deletions.
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Package: shinyBS
Type: Package
Title: Twitter Bootstrap Components for Shiny
Version: 0.61
Date: 2015-03-30
Version: 0.62
Date: 2015-03-31
Author: Eric Bailey
Maintainer: Eric Bailey <[email protected]>
Description: Adds additional Twitter Bootstrap components to Shiny.
Expand Down
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export(bsExample)
export(bsModal)
export(bsPopover)
export(bsTooltip)
export(bsTypeahead)
export(closeAlert)
export(createAlert)
export(popify)
Expand All @@ -19,3 +20,4 @@ export(tipify)
export(toggleModal)
export(updateButton)
export(updateCollapse)
export(updateTypeahead)
41 changes: 41 additions & 0 deletions R/bsTypeahead.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#'bsTypeahead
#'
#'\code{typeaheadInput} creates a textinput with type ahead function buttons.
#'
#'@param inputId Input variable to assign the control's value to
#'@param label Display label for the control
#'@param value Initial value
#'@param choices Array of strings to match against. Can also be JavaScript
#' function. Use htmlwidgets::JS() to indicate JavaScript. The length of \code{choices} must no exceed 131370.
#'@param items The max number of items to display in the dropdown. Can also be
#' set to 'all'
#'@param minLength The minimum character length needed before triggering
#' autocomplete suggestions. You can set it to 0 so suggestion are shown even
#' when there is no text when lookup function is called.
#'@seealso \code{\link{updateTypeaheadInput}}
#'@export
bsTypeahead <- function(inputId, label, value = "", choices, items = 8, minLength = 1) {
if(!is.null(choices)) {
if(length(choices) > 131370) {
warning("Due to a limitation of the Bootstrap2 Typeahead JavaScript library the length of 'choices' must not exceed 2 ^ 17 - 2!");
}
}
if(!'JS_EVAL' %in% class(choices)) {
choices <- jsonlite::toJSON(choices);
}
typeahead <- shiny::tagList(
shiny::div(class = 'form-group shiny-input-container',
shiny::tags$label(label, `for` = inputId),
shiny::tags$input(id = inputId, type="text",
class="form-control shiny-bound-input typeahead",
"data-provide" = "typeahead", autocomplete="off",
value = value),
shiny::tags$script(paste0("$('#", inputId, "').typeahead({source: ", choices, ",
items: ", items, ",
minLength: ", minLength, "})"))
)
)

htmltools::attachDependencies(typeahead, typeaheadDep)

}
1 change: 1 addition & 0 deletions R/misc.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
}

shinyBSDep <- htmltools::htmlDependency("shinyBS", packageVersion("shinyBS"), src = c("href" = "sbs"), script = "shinyBS.js", stylesheet = "shinyBS.css")
typeaheadDep <- htmltools::htmlDependency("shinyBS", packageVersion("shinyBS"), src = c("href" = "sbs"), script = c("bootstrap3-typeahead.js", "typeahead_inputbinding.js"));

# Copy of dropNulls function for shiny to avoid using shiny:::dropNulls
dropNulls <- function(x) {
Expand Down
23 changes: 23 additions & 0 deletions R/updateTypeahead.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#'updateTypeahead
#'
#'\code{updateTypeahead} Update a typeaheadInput buttons.
#'@param session The session object passed to function given to shinyServer.
#'@param inputId Input variable to assign the control's value to
#'@param label Display label for the control
#'@param value Initial value
#'@param choices Array of strings to match against. Can also be JavaScript
#' function. Use htmlwidgets::JS() to indicate JavaScript.
#'@seealso \code{\link{typeaheadInput}}
#'@export
updateTypeahead<- function(session, inputId, label=NULL, value=NULL, choices=NULL) {

if(!is.null(choices)) {
if(length(choices) > 131370) {
warning("Due to a limitation of the Bootstrap2 Typeahead JavaScript library the length of 'choices' must not exceed 2 ^ 17 - 2!");
}
}

data <- dropNulls(list(id = inputId, label=label, value=value, choices=choices))
session$sendCustomMessage("typeaheadUpdate", data)

}
Loading

0 comments on commit f2e2fc8

Please sign in to comment.