R/mod_select_all_dropdown.R

Defines functions mod_select_all_dropdown_server mod_select_all_dropdown_ui

#' select_all_dropdown UI Function
#'
#' @description A shiny Module.
#'
#' @param id,input,output,session Internal parameters for {shiny}.
#'
#' @noRd
#'
#' @importFrom shiny NS tagList
mod_select_all_dropdown_ui <-
  function(id, .label, .choices, .multiple = FALSE, ...) {
    ns <- NS(id)

    if (.multiple) {
      .choices <- c("Select all", .choices)
    }
    tagList(
      shiny::selectizeInput(
        inputId = ns("drop"),
        label = .label,
        choices = .choices,
        options = list(placeholder = .label, plugins = list('remove_button')),
        multiple = .multiple,
        ...
      )
    )
  }

#' select_all_dropdown Server Functions
#'
#' @noRd
mod_select_all_dropdown_server <- function(id, .choices){
  moduleServer( id, function(input, output, session){
    ns <- session$ns

    shiny::observe({
      shiny::req(input$drop)
      if ("Select all" %in% input$drop) {
        selected_choices <- setdiff(.choices, c("Select all"))
        shiny::updateSelectizeInput(
          inputId = "drop",
          selected = selected_choices,
          options = list(plugins = list('remove_button'))
        )
      }
    })

    return(list(
      get = reactive({input$drop})
    ))

  })
}
teofiln/gene.editing.dash documentation built on Feb. 21, 2022, 12:59 a.m.