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
19 changes: 14 additions & 5 deletions R/0-badge_dropdown.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,13 @@
#' @param id (`character(1)`) shiny module's id
#' @param label (`shiny.tag`) Label displayed on a badge.
#' @param content (`shiny.tag`) Content of a drop-down.
#' @param badge_context (`character(1)`) Variation content of the badge i.e: "primary", "secondary" ...
#' @param fixed (`logical(1)`) Whether to return a badge with dropdown (default) or simple fixed badge if set to TRUE
#' @keywords internal
badge_dropdown <- function(id, label, content) {
badge_dropdown <- function(id, label, content, badge_context = "primary", fixed = FALSE) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the description of the new badge_context argument and I see it inherits the color from teal.
But do we need the fixed = FALSE argument? If it is fixed there is no dropdown so we might want to use a different function that generates the UI.

checkmate::assert_character(badge_context)
checkmate::assert_logical(fixed)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not only logical but also to be of length one and not accept NA values

Suggested change
checkmate::assert_logical(fixed)
checkmate::assert_flag(fixed)


ns <- shiny::NS(id)
htmltools::tagList(
htmltools::singleton(htmltools::tags$head(
Expand All @@ -20,11 +25,15 @@ badge_dropdown <- function(id, label, content) {
class = "badge-dropdown-wrapper",
htmltools::tags$span(
id = ns("summary_badge"),
class = "badge bg-primary rounded-pill badge-dropdown",
style = "cursor: pointer;",
class = sprintf("badge bg-%s rounded-pill badge-dropdown", badge_context),
style = ifelse(fixed, "", "cursor: pointer"),
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not important but I think there was some recommendations to use if instead of ifelse for these cases:

Suggested change
style = ifelse(fixed, "", "cursor: pointer"),
style = if (fixed) "" else "cursor: pointer",

tags$span(class = "badge-dropdown-label", label),
tags$span(class = "badge-dropdown-icon", bsicons::bs_icon("caret-down-fill")),
onclick = sprintf("toggleBadgeDropdown('%s', '%s')", ns("summary_badge"), ns("inputs_container"))
if (isFALSE(fixed)) tags$span(class = "badge-dropdown-icon", bsicons::bs_icon("caret-down-fill")),
onclick = ifelse(
fixed,
"",
sprintf("toggleBadgeDropdown('%s', '%s')", ns("summary_badge"), ns("inputs_container"))
)
),
htmltools::tags$div(
content,
Expand Down
25 changes: 22 additions & 3 deletions R/0-module_picks.R
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,20 @@ picks_ui.list <- function(id, picks, container) {
picks_ui.picks <- function(id, picks, container) {
ns <- shiny::NS(id)
badge_label <- shiny::uiOutput(ns("summary"), container = htmltools::tags$span)

content <- lapply(picks, function(x) .pick_ui(id = ns(methods::is(x))))
htmltools::tags$div(
if (missing(container)) {
badge_dropdown(id = ns("inputs"), label = badge_label, htmltools::tagList(content))
if (isTRUE(attr(picks$variables, "fixed")) && isTRUE(attr(picks$datasets, "fixed"))) {
Copy link
Copy Markdown
Contributor

@llrs-roche llrs-roche Apr 15, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would iterate through the whole elements of picks and check with is_pick_fixed() from #48
Maybe datasets and variables are fixed but values are not.

Se also my comment about fixed = TRUE and that we might want to have a different function to generate the UI below this: badge_locked() ? . If we don't I would only have one call and use badge_dropdown(..., fixed = all(locked)) to avoid an extra if else.

badge_dropdown(
id = ns("inputs"),
label = badge_label,
htmltools::tagList(content),
badge_context = "secondary",
fixed = TRUE
)
} else {
badge_dropdown(id = ns("inputs"), label = badge_label, htmltools::tagList(content))
}
} else {
if (!any(sapply(htmltools::tags, identical, container))) {
stop("Container should be one of `htmltools::tags`")
Expand Down Expand Up @@ -244,8 +253,18 @@ picks_srv.picks <- function(id, picks, data) {
logger::log_debug(".pick_srv@1 rerender {pick_type} input")
.validate_is_eager(choices())
.validate_is_eager(selected())
if (isTRUE(args$fixed) || length(choices()) <= 1) {
if (!length(choices()) || isTRUE(args$fixed)) {
NULL
} else if (length(choices()) == 1 && isFALSE(args$fixed)) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need this new conditional? Wouldn't it be catch up by the latest else clause?

.pick_ui_categorical(
session$ns("selected"),
label = sprintf("Select %s:", pick_type),
choices = choices(),
selected = selected(),
multiple = args$multiple,
choicesOpt = list(content = isolate(choices_opt_content())),
args = args[!names(args) %in% c("multiple")]
)
} else if (.is_ranged(choices()) && inherits(choices(), "Date")) {
.pick_ui_date(
session$ns("range"),
Expand Down
1 change: 1 addition & 0 deletions inst/WORDLIST
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
Analyse
UI
dropdown
schemas
6 changes: 5 additions & 1 deletion man/badge_dropdown.Rd

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

36 changes: 36 additions & 0 deletions tests/testthat/test-0-module_picks.R
Original file line number Diff line number Diff line change
Expand Up @@ -1072,3 +1072,39 @@ describe("picks_srv resolves picks interactively", {
it("changing date range in slider input updates picks_resolved")
it("setting picks_resolved$selected outside of range adjust to the available range")
})

describe("picks_ui creates correct badge_dropdown", {
it("with fixed = FALSE when picks has two datasets and one single choice var", {
test_picks <- picks(
datasets(choices = c("iris", "mtcars"), selected = "iris"),
variables(choices = "Species", selected = "Species")
)
ui_output <- picks_ui("test", picks = test_picks)

expect_true(grepl("cursor: pointer", as.character(ui_output)))
expect_true(grepl("badge-dropdown-icon", as.character(ui_output)))
})

it("with fixed = TRUE when picks has one dataset and single choice var", {
test_picks <- picks(
datasets(choices = "iris", selected = "iris"),
variables(choices = "Species", selected = "Species")
)
ui_output <- picks_ui("test", picks = test_picks)

expect_false(grepl("cursor: pointer", as.character(ui_output)))
expect_false(grepl("badge-dropdown-icon", as.character(ui_output)))
expect_true(grepl("bg-secondary", as.character(ui_output)))
})

it("with fixed = FALSE when picks has one dataset and single choice var with explicit fixed = FALSE", {
test_picks <- picks(
datasets(choices = "iris", selected = "iris"),
variables(choices = "Species", selected = "Species", fixed = FALSE)
)
ui_output <- picks_ui("test", picks = test_picks)

expect_true(grepl("cursor: pointer", as.character(ui_output)))
expect_true(grepl("badge-dropdown-icon", as.character(ui_output)))
})
})
Loading