Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disable dropdown on encondings if no option is available #332

Merged
merged 7 commits into from
Sep 12, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
28 changes: 21 additions & 7 deletions R/adtteSpec.R
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,14 @@ adtteSpecInput <- function(inputId, # nolint

ns <- NS(inputId)

selectInput(
inputId = ns("paramcd"),
label = label_paramcd,
choices = ""
tagList(
selectizeInput(
inputId = ns("paramcd"),
label = label_paramcd,
choices = "",
options = list(placeholder = "- Nothing selected -")
),
include_js_files("dropdown.js")
)
}

Expand Down Expand Up @@ -295,6 +299,12 @@ adtteSpecServer <- function(id, # nolint
sort(unique(adtte_joined[[adtte_vars$paramcd]])) # Order should not matter.
})

# Start by disabling selection, will be overriden if there are valid choices.
session$sendCustomMessage(
"toggle_dropdown",
list(input_id = session$ns("paramcd"), disabled = TRUE)
)

# Once available endpoints change, we update choices (and also the selection
# if nothing was selected earlier) and warn the user if previous endpoint is
# not available.
Expand All @@ -310,11 +320,15 @@ adtteSpecServer <- function(id, # nolint
))
""
}
updateSelectInput(
session,
updateSelectizeInput(
"paramcd",
choices = paramcd_choices,
selected = new_selected
selected = new_selected,
session = session
)
session$sendCustomMessage(
"toggle_dropdown",
list(input_id = session$ns("paramcd"), disabled = rlang::is_empty(paramcd_choices))
)
})

Expand Down
25 changes: 17 additions & 8 deletions R/assaySpec.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,16 @@ assaySpecInput <- function(inputId, # nolint
assert_string(label_assays, min.chars = 1L)

ns <- NS(inputId)
selectInput(
inputId = ns("name"),
label = label_assays,
choices = ""
tagList(
selectizeInput(
inputId = ns("name"),
label = label_assays,
choices = character(0),
options = list(
placeholder = "- Nothing selected -"
)
),
include_js_files("dropdown.js")
)
}

Expand Down Expand Up @@ -119,15 +125,18 @@ assaySpecServer <- function(id, # nolint
hermes::h_short_list(removed_assays), "as per app specifications"
))
}
if (length(remaining_assays) == 0) {
remaining_assays <- character(0)
}
remaining_assays
})

observeEvent(choices(), {
choices <- choices()
updateSelectInput(
session,
"name",
choices = choices
updateSelectizeInput(session, "name", choices = choices)
session$sendCustomMessage(
"toggle_dropdown",
list(input_id = session$ns("name"), disabled = rlang::is_empty(choices))
)
})

Expand Down
1 change: 1 addition & 0 deletions R/geneSpec.R
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ geneSpecInput <- function(inputId, # nolint
multiple = TRUE,
selected = 1,
options = list(
placeholder = "- Nothing selected -",
render = I("{
option: function(item, escape) {
return '<div> <span style=\"font-size: inherit;\">' + item.label + '</div>' +
Expand Down
26 changes: 26 additions & 0 deletions inst/js/dropdown.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Toggle enable/disable of a dropdown
//
// This can be done by `{shinyjs::enable/disable}` R package if that package is
// added to the dependecies.
// Parameters
// `message` should have `input_id` string and `disabled` logical properties.
Shiny.addCustomMessageHandler('toggle_dropdown', function(message) {
const input_id = message.input_id;
const disabled = message.disabled;

let el = document.getElementById(input_id)

if (el.selectize !== undefined) {
el = el.selectize;
if (disabled) {
el.lock();
el.disable();
} else {
el.unlock();
el.enable();
}
} else {
// Fallback in case selectize is not enabled
el.disabled = disabled ? 'disabled' : '';
}
});
Loading