@@ -686,6 +686,7 @@ choicesWithNames <- function(choices) {
686
686
# ' \code{multiple = TRUE}). If not specified then defaults to the first value
687
687
# ' for single-select lists and no values for multiple select lists.
688
688
# ' @param multiple Is selection of multiple items allowed?
689
+ # ' @param selectize Whether to use \pkg{selectize.js} or not.
689
690
# ' @return A select list control that can be added to a UI definition.
690
691
# '
691
692
# ' @family input elements
@@ -697,11 +698,8 @@ choicesWithNames <- function(choices) {
697
698
# ' "Transmission" = "am",
698
699
# ' "Gears" = "gear"))
699
700
# ' @export
700
- selectInput <- function (inputId ,
701
- label ,
702
- choices ,
703
- selected = NULL ,
704
- multiple = FALSE ) {
701
+ selectInput <- function (inputId , label , choices , selected = NULL ,
702
+ multiple = FALSE , selectize = TRUE ) {
705
703
# resolve names
706
704
choices <- choicesWithNames(choices )
707
705
@@ -731,21 +729,36 @@ selectInput <- function(inputId,
731
729
selectTag <- tagSetChildren(selectTag , list = optionTags )
732
730
733
731
# return label and select tag
734
- tagList(controlLabel(inputId , label ), selectTag )
732
+ res <- tagList(controlLabel(inputId , label ), selectTag )
733
+ if (! selectize ) return (res )
734
+ selectizeIt(inputId , res , NULL , nonempty = ! multiple && ! (" " %in% choices ))
735
735
}
736
736
737
737
# ' @rdname selectInput
738
- # ' @param ... arguments passed to \code{selectInput()}
739
- # ' @param options a list of options; see the documentation of \pkg{selectize.js}
738
+ # ' @param ... Arguments passed to \code{selectInput()}.
739
+ # ' @param options A list of options. See the documentation of \pkg{selectize.js}
740
740
# ' for possible options (character option values inside \code{\link{I}()} will
741
741
# ' be treated as literal JavaScript code; see \code{\link{renderDataTable}()}
742
- # ' for details)
742
+ # ' for details).
743
+ # ' @note The selectize input created from \code{selectizeInput()} allows
744
+ # ' deletion of the selected option even in a single select input, which will
745
+ # ' return an empty string as its value. This is the default behavior of
746
+ # ' \pkg{selectize.js}. However, the selectize input created from
747
+ # ' \code{selectInput(..., selectize = TRUE)} will ignore the empty string
748
+ # ' value when it is a single choice input and the empty string is not in the
749
+ # ' \code{choices} argument. This is to keep compatibility with
750
+ # ' \code{selectInput(..., selectize = FALSE)}.
743
751
# ' @export
744
752
selectizeInput <- function (inputId , ... , options = NULL ) {
753
+ selectizeIt(inputId , selectInput(inputId , ... , selectize = FALSE ), options )
754
+ }
755
+
756
+ # given a select input and its id, selectize it
757
+ selectizeIt <- function (inputId , select , options , nonempty = FALSE ) {
745
758
res <- checkAsIs(options )
746
759
747
760
tagList(
748
- selectInput( inputId , ... ) ,
761
+ select ,
749
762
singleton(tags $ head(
750
763
tags $ link(rel = ' stylesheet' , type = ' text/css' ,
751
764
href = ' shared/selectize/css/selectize.bootstrap2.css' ),
@@ -755,7 +768,8 @@ selectizeInput <- function(inputId, ..., options = NULL) {
755
768
tags $ script(src = ' shared/selectize/js/selectize.min.js' )
756
769
)),
757
770
tags $ script(
758
- type = ' application/json' , `data-for` = inputId ,
771
+ type = ' application/json' ,
772
+ `data-for` = inputId , `data-nonempty` = if (nonempty ) ' ' ,
759
773
`data-eval` = if (length(res $ eval )) HTML(toJSON(res $ eval )),
760
774
if (length(res $ options )) HTML(toJSON(res $ options )) else ' {}'
761
775
)
0 commit comments