From 178f91faf61a02de5aa94affe022842093eca199 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Ver=C3=ADssimo?= <211358+averissimo@users.noreply.github.com> Date: Mon, 7 Aug 2023 16:30:28 +0100 Subject: [PATCH 1/7] feat: placeholder for select encodings and disables it when no choices available --- R/adtteSpec.R | 28 +++++++++++++++++++++------- R/assaySpec.R | 26 ++++++++++++++++++-------- R/geneSpec.R | 1 + inst/js/dropdown.js | 26 ++++++++++++++++++++++++++ 4 files changed, 66 insertions(+), 15 deletions(-) create mode 100644 inst/js/dropdown.js diff --git a/R/adtteSpec.R b/R/adtteSpec.R index 9e3d33c5..d929c640 100644 --- a/R/adtteSpec.R +++ b/R/adtteSpec.R @@ -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 = "Select an endpoint...") + ), + include_js_files("dropdown.js") ) } @@ -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. @@ -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)) ) }) diff --git a/R/assaySpec.R b/R/assaySpec.R index 55d4c7a3..faf957dd 100644 --- a/R/assaySpec.R +++ b/R/assaySpec.R @@ -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 = "Select an eligible assay..." + ) + ), + include_js_files("dropdown.js") ) } @@ -119,16 +125,20 @@ 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)) ) + }) reactive({ diff --git a/R/geneSpec.R b/R/geneSpec.R index deef6e21..229d7d99 100644 --- a/R/geneSpec.R +++ b/R/geneSpec.R @@ -107,6 +107,7 @@ geneSpecInput <- function(inputId, # nolint multiple = TRUE, selected = 1, options = list( + placeholder = "Select one or more genes...", render = I("{ option: function(item, escape) { return '
' + item.label + '
' + diff --git a/inst/js/dropdown.js b/inst/js/dropdown.js new file mode 100644 index 00000000..c8162f80 --- /dev/null +++ b/inst/js/dropdown.js @@ -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" : ""; + } +}); From fd6206b935bdbb7f08159f571782f973f2b33ba4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Ver=C3=ADssimo?= <211358+averissimo@users.noreply.github.com> Date: Fri, 8 Sep 2023 14:18:19 +0200 Subject: [PATCH 2/7] docs: rename placeholder to '- Nothing selected -' --- R/adtteSpec.R | 2 +- R/assaySpec.R | 3 +-- R/geneSpec.R | 2 +- 3 files changed, 3 insertions(+), 4 deletions(-) diff --git a/R/adtteSpec.R b/R/adtteSpec.R index d929c640..7a1f062d 100644 --- a/R/adtteSpec.R +++ b/R/adtteSpec.R @@ -112,7 +112,7 @@ adtteSpecInput <- function(inputId, # nolint inputId = ns("paramcd"), label = label_paramcd, choices = "", - options = list(placeholder = "Select an endpoint...") + options = list(placeholder = "- Nothing selected -") ), include_js_files("dropdown.js") ) diff --git a/R/assaySpec.R b/R/assaySpec.R index faf957dd..8179e3ae 100644 --- a/R/assaySpec.R +++ b/R/assaySpec.R @@ -22,7 +22,7 @@ assaySpecInput <- function(inputId, # nolint label = label_assays, choices = character(0), options = list( - placeholder = "Select an eligible assay..." + placeholder = "- Nothing selected -" ) ), include_js_files("dropdown.js") @@ -138,7 +138,6 @@ assaySpecServer <- function(id, # nolint "toggle_dropdown", list(input_id = session$ns("name"), disabled = rlang::is_empty(choices)) ) - }) reactive({ diff --git a/R/geneSpec.R b/R/geneSpec.R index 229d7d99..f6ea0b9f 100644 --- a/R/geneSpec.R +++ b/R/geneSpec.R @@ -107,7 +107,7 @@ geneSpecInput <- function(inputId, # nolint multiple = TRUE, selected = 1, options = list( - placeholder = "Select one or more genes...", + placeholder = "- Nothing selected -", render = I("{ option: function(item, escape) { return '
' + item.label + '
' + From 47a873c6f4281cf77b777882dfec6af789649427 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Ver=C3=ADssimo?= <211358+averissimo@users.noreply.github.com> Date: Mon, 11 Sep 2023 12:46:30 +0200 Subject: [PATCH 3/7] fix: use of correct ticks --- inst/js/dropdown.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/inst/js/dropdown.js b/inst/js/dropdown.js index c8162f80..1937f1a4 100644 --- a/inst/js/dropdown.js +++ b/inst/js/dropdown.js @@ -21,6 +21,6 @@ Shiny.addCustomMessageHandler('toggle_dropdown', function(message) { } } else { // Fallback in case selectize is not enabled - el.disabled = disabled ? "disabled" : ""; + el.disabled = disabled ? 'disabled' : ''; } }); From 8ed01b1f6f06ec0705db04c84a2d5917d606ed7c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Ver=C3=ADssimo?= <211358+averissimo@users.noreply.github.com> Date: Mon, 11 Sep 2023 14:16:00 +0200 Subject: [PATCH 4/7] fix: adjusts tests to new output --- tests/testthat/_snaps/assaySpec.md | 30 ++++++++++++++++++++++++++++-- tests/testthat/test-adtteSpec.R | 14 +++++++++++++- 2 files changed, 41 insertions(+), 3 deletions(-) diff --git a/tests/testthat/_snaps/assaySpec.md b/tests/testthat/_snaps/assaySpec.md index ddce3e42..d1dfc3af 100644 --- a/tests/testthat/_snaps/assaySpec.md +++ b/tests/testthat/_snaps/assaySpec.md @@ -6,8 +6,34 @@
- - + +
+ diff --git a/tests/testthat/test-adtteSpec.R b/tests/testthat/test-adtteSpec.R index d70a2571..c41752f6 100644 --- a/tests/testthat/test-adtteSpec.R +++ b/tests/testthat/test-adtteSpec.R @@ -175,7 +175,19 @@ test_that("adtteSpecInput creates expected HTML", { label_paramcd = "Select right PARAMCD" )) - expect_tag(result) + expect_class(result, "shiny.tag.list") + expect_length(result, 2) + + # First element is a div tag + expect_tag(result[[1]]) + expect_class(result[[1]], "shiny.tag") + expect_identical(result[[1]]$name, "div") + + # Second element is the contents of a single js file + expect_length(result[[2]], 1) + expect_tag(result[[2]][[1]]) + expect_class(result[[2]][[1]], "shiny.tag") + expect_identical(result[[2]][[1]]$name, "script") }) # nolint start From 095e334f362efea03d3db7e0ef61f67525d67238 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Ver=C3=ADssimo?= <211358+averissimo@users.noreply.github.com> Date: Mon, 11 Sep 2023 14:30:58 +0200 Subject: [PATCH 5/7] fix: remove rlang call and add changes to NEWS --- NEWS.md | 4 ++++ R/adtteSpec.R | 2 +- R/assaySpec.R | 2 +- 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/NEWS.md b/NEWS.md index 5ddd7206..fa5ad69b 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,9 @@ # teal.modules.hermes 0.1.5.9002 +### Miscellaneous +* Added placeholders for `assaySpec`, `adtteSpec` and `geneSpec` inputs when no option is selected. +* Disabled the dropdown input for `assaySpec` and `adtteSpec` when there are no options available. + # teal.modules.hermes 0.1.5 ### Bug Fixes diff --git a/R/adtteSpec.R b/R/adtteSpec.R index 7a1f062d..5feca2a3 100644 --- a/R/adtteSpec.R +++ b/R/adtteSpec.R @@ -328,7 +328,7 @@ adtteSpecServer <- function(id, # nolint ) session$sendCustomMessage( "toggle_dropdown", - list(input_id = session$ns("paramcd"), disabled = rlang::is_empty(paramcd_choices)) + list(input_id = session$ns("paramcd"), disabled = (length(paramcd_choices) == 0)) ) }) diff --git a/R/assaySpec.R b/R/assaySpec.R index 8179e3ae..9c72ab7d 100644 --- a/R/assaySpec.R +++ b/R/assaySpec.R @@ -136,7 +136,7 @@ assaySpecServer <- function(id, # nolint updateSelectizeInput(session, "name", choices = choices) session$sendCustomMessage( "toggle_dropdown", - list(input_id = session$ns("name"), disabled = rlang::is_empty(choices)) + list(input_id = session$ns("name"), disabled = (length(choices) == 0)) ) }) From f9cd0666d36748d5de0d6fee8ef0e738e4e9a2fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Ver=C3=ADssimo?= <211358+averissimo@users.noreply.github.com> Date: Mon, 11 Sep 2023 14:33:09 +0200 Subject: [PATCH 6/7] fix: simplifies test --- tests/testthat/test-adtteSpec.R | 4 ---- 1 file changed, 4 deletions(-) diff --git a/tests/testthat/test-adtteSpec.R b/tests/testthat/test-adtteSpec.R index c41752f6..7a24c219 100644 --- a/tests/testthat/test-adtteSpec.R +++ b/tests/testthat/test-adtteSpec.R @@ -180,14 +180,10 @@ test_that("adtteSpecInput creates expected HTML", { # First element is a div tag expect_tag(result[[1]]) - expect_class(result[[1]], "shiny.tag") - expect_identical(result[[1]]$name, "div") # Second element is the contents of a single js file expect_length(result[[2]], 1) expect_tag(result[[2]][[1]]) - expect_class(result[[2]][[1]], "shiny.tag") - expect_identical(result[[2]][[1]]$name, "script") }) # nolint start From fd36f1de1159ccce0c50902b020f63a69797f291 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Ver=C3=ADssimo?= <211358+averissimo@users.noreply.github.com> Date: Mon, 11 Sep 2023 14:44:04 +0200 Subject: [PATCH 7/7] docs: replace dropdown with select on NEWS --- NEWS.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/NEWS.md b/NEWS.md index fa5ad69b..bb35245d 100644 --- a/NEWS.md +++ b/NEWS.md @@ -2,7 +2,7 @@ ### Miscellaneous * Added placeholders for `assaySpec`, `adtteSpec` and `geneSpec` inputs when no option is selected. -* Disabled the dropdown input for `assaySpec` and `adtteSpec` when there are no options available. +* Disabled the select input for `assaySpec` and `adtteSpec` when there are no options available. # teal.modules.hermes 0.1.5