Skip to content
Open
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
83 changes: 31 additions & 52 deletions R/shiny-material-text-box.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,77 +3,56 @@
#' Build a shinymaterial text box.
#' @param input_id String. The input identifier used to access the value.
#' @param label String. The text box label.
#' @param value String. Initial value.
#' @param color String. The accent color of the text box. Leave empty for the default color. Visit \url{http://materializecss.com/color.html} for a list of available colors. \emph{This input requires using color hex codes, rather than the word form. E.g., "#ef5350", rather than "red lighten-1".}
#' @param icon String. The name of the icon. Leave empty for no icon. Visit \url{http://materializecss.com/icons.html} for a list of available icons.
#' @param disabled FALSE by default, if TRUE the text box is disable.
#' @seealso \code{\link{update_material_text_box}}
#' @examples
#' material_text_box(
#' input_id = "example_text_box",
#' label = "text box",
#' icon = "search",
#' color = "#ef5350"
#' color = "#ef5350",
#' disabled = FALSE
#' )
material_text_box <- function(input_id, label, color = NULL, icon = NULL){
if (!is.null(color)) {

if (!is.null(icon)) {
icon_style_color <- paste0(
"#", input_id, "_text_box.input-field .prefix.active {
color: ", color, ";
}"
)
} else {
icon_style_color <- ""
}

text_box_style <-
shiny::tagList(
shiny::tags$head(
shiny::tags$style(
paste0(
"
#", input_id, "_text_box.input-field input[type=text]:focus + label {
color: ", color, ";
}
#", input_id, "_text_box.input-field input[type=text]:focus {
border-bottom: 1px solid ", color, ";
box-shadow: 0 1px 0 0 ", color, ";
}
",
icon_style_color
)
material_text_box <- function(input_id, label, value = "", color = NULL, icon = NULL, disable = FALSE){

css_id <- paste0("#", input_id, "_text_box.input-field")
text_box_style <- if (!is.null(color))
shiny::tagList(
shiny::tags$head(
shiny::tags$style(
paste0(
css_id, " input[type=text]:focus + label {color: ", color, ";}",
css_id, " input[type=text]:focus {border-bottom: 1px solid ", color, ";box-shadow: 0 1px 0 0 ", color, ";}",
if (!is.null(icon))
paste0(css_id, " .prefix.active {color: ", color, ";}")
)
)
)

} else {
text_box_style <- shiny::tags$div()
}

if (!is.null(icon)) {
text_box_icon <- shiny::tags$i(class = "material-icons prefix", icon)
} else {
text_box_icon <- shiny::tags$div()
}

)

text_box_icon <- if(!is.null(icon))
shiny::tags$i(class = "material-icons prefix", icon)

text_box_input <- shiny::tags$input(
id = input_id, type = "text",
class = "validate", value = value
)

if (disable) text_box_input <- shiny::HTML(sub("<input", "<input disabled", text_box_input))

create_material_object(
js_file =
"shiny-material-text-box.js",
js_file = "shiny-material-text-box.js",
material_tag_list =
shiny::tagList(
shiny::tags$div(
class = "input-field",
id = paste0(input_id, "_text_box"),
text_box_icon,
shiny::tags$input(
id = input_id,
type = "text",
class = "validate"
),
shiny::tags$label(
`for` = input_id,
label
)
text_box_input,
shiny::tags$label(`for` = input_id, label)
),
text_box_style
)
Expand Down
24 changes: 15 additions & 9 deletions R/update-shiny-material-text-box.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,29 @@
#' @param session The session object passed to function given to shinyServer.
#' @param input_id The input_id of the material_text_box.
#' @param value The value to set for the material_text_box.
#' @param disabled NULL by default (do nothing), if TRUE the text box is disable and if FALSE, enable.
#' @seealso \code{\link{material_text_box}}
#' @examples
#' \dontrun{
#' update_material_text_box(
#' session,
#' input_id = "example_text_box",
#' value = "New Text"
#' value = "New Text",
#' disabled = FALSE
#' )
#' }
update_material_text_box <- function(session, input_id, value = NULL){
if(is.null(value)) {
return(NULL)
update_material_text_box <- function(session, input_id, value = NULL, disabled = NULL){

if(!is.null(disabled)){
js_code <- paste0("$('#", input_id, "').prop( 'disabled', ", tolower(disabled), " )")
session$sendCustomMessage(type = "shinymaterialJS", js_code)
}
session$sendCustomMessage(
type = "shinymaterialJS",
paste0(
"$('#", input_id, "').val('", value, "');Shiny.onInputChange('", input_id, "', '", value, "');M.updateTextFields()"

if(!is.null(value)){
js_code <- paste0(
"$('#", input_id, "').val('", value, "');Shiny.onInputChange('", input_id, "', '", value, "');M.updateTextFields()"
)
)
session$sendCustomMessage(type = "shinymaterialJS", js_code)
}

}
16 changes: 14 additions & 2 deletions man/material_text_box.Rd

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

7 changes: 5 additions & 2 deletions man/update_material_text_box.Rd

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