R/bulma-form-file.R

Defines functions bulma_form_file

Documented in bulma_form_file

#' @title
#' Bulma Form: File
#'
#' @description
#' A custom file upload input, without JavaScript.
#'
#' [File](https://bulma.io/documentation/form/file/)
#'
#' @param name      (str) name identifier, defaults to random string.
#' @param icon      (str) icon of the upload button, defaults to FontAwesome.
#' @param label     (str) label of the upload button
#' @param file_name (str) the file name on the side when uploaded
#' @param ...       (tag) content
#' @param fullwidth (flg) whether this is expanded
#' @param align,color,size,state,boxed styling
#' @param outer_tag,inner_tag (fun) default html tag
#'
#' @family Bulma Form Components
#' @export
bulma_form_file <- function(...,
                            name = stri_rand_strings(1L, 16L),
                            icon = "fas fa-upload",
                            label = NULL,
                            file_name = NULL,
                            align = c("left", "centered", "right"),
                            color = c("primary", "link", "info",
                                      "success", "warning", "danger",
                                      "white", "black", "light", "dark"),
                            size  = c("small", "normal", "medium", "large"),
                            state = c("hovered", "focused", "active",
                                      "loading", "static"),
                            boxed = FALSE,
                            fullwidth = FALSE,
                            outer_tag = tags$div,
                            inner_tag = tags$label) {

  assert_string(name)
  assert_function(outer_tag)
  assert_function(inner_tag)
  assert_flag(fullwidth)

  outer_tag(
    class = "file",
    inner_tag(
      class = "file-label",
      tags$input(class = "file-input", type = "file", name = name),
      tags$span(
        class = "file-cta",
        tags$span(class = "file-icon", tags$i(class = icon)),
        tags$span(class = "file-label", label)
      ),
      if (!is.null(file_name)) {
        tags$span(class = "file-name", file_name)
      }
    )
  ) %>%
    bulma_color(color) %>%
    bulma_size(size) %>%
    bulma_state(state) %>%
    bulma_align(align) %>%
    when(fullwidth, bulma_fullwidth(.)) %>%
    when(boxed, bulma_is(., "boxed")) %>%
    when(!is.null(file_name), bulma_has(., "name")) %>%
    add_class("bulma_form_file")

}
tjpalanca/bulma.R documentation built on Dec. 23, 2021, 10:58 a.m.