Skip to content

Commit 201b767

Browse files
committed
added expandedName and startExpanded params to menuItem()
1 parent e4a9f1d commit 201b767

File tree

7 files changed

+41
-77
lines changed

7 files changed

+41
-77
lines changed

R/dashboardSidebar.R

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,13 @@ sidebarSearchForm <- function(textId, buttonId, label = "Search...",
243243
#' @param selected If \code{TRUE}, this \code{menuItem} or \code{menuSubItem}
244244
#' will start selected. If no item have \code{selected=TRUE}, then the first
245245
#' \code{menuItem} will start selected.
246+
#' @param expandedName A unique name given to each \code{menuItem} that serves
247+
#' to indicate which one (if any) is currently expanded. (This is only applicable
248+
#' to \code{menuItem}s that have children and it is mostly only useful for
249+
#' bookmarking state.)
250+
#' @param startExpanded Should this \code{menuItem} be expanded on app startup?
251+
#' (This is only applicable to \code{menuItem}s that have children, and only
252+
#' one of these can be expanded at any given time).
246253
#' @param ... For menu items, this may consist of \code{\link{menuSubItem}}s.
247254
#' @param .list An optional list containing items to put in the menu Same as the
248255
#' \code{...} arguments, but in list format. This can be useful when working
@@ -354,7 +361,9 @@ sidebarMenu <- function(..., id = NULL, .list = NULL) {
354361
#' @rdname sidebarMenu
355362
#' @export
356363
menuItem <- function(text, ..., icon = NULL, badgeLabel = NULL, badgeColor = "green",
357-
tabName = NULL, href = NULL, newtab = TRUE, selected = NULL) {
364+
tabName = NULL, href = NULL, newtab = TRUE, selected = NULL,
365+
expandedName = as.character(gsub("[[:space:]]", "", text)),
366+
startExpanded = FALSE) {
358367
subItems <- list(...)
359368

360369
if (!is.null(icon)) tagAssert(icon, type = "i")
@@ -414,12 +423,12 @@ menuItem <- function(text, ..., icon = NULL, badgeLabel = NULL, badgeColor = "gr
414423
# these actually independent in AdminLTE). If no menuItem was expanded, `dataExpanded`
415424
# is NULL. However, we want to this input to get passed on (and not dropped), so we
416425
# do `%OR% ""` to assure this.
417-
dataExpanded <- shiny::restoreInput(id = "sidebarItemExpanded", default = "") %OR% ""
426+
default <- if (startExpanded) expandedName else ""
427+
dataExpanded <- shiny::restoreInput(id = "sidebarItemExpanded", default) %OR% ""
418428

419-
# If `dataExpanded` is not the empty string, we need to check that there is a subMenuItem
420-
# in the list of `subItems` that actually has a `data-value` attribute equal to `dataExpanded`
421-
isExpanded <- nzchar(dataExpanded) &&
422-
any(unlist(lapply(subItems, findAttribute, "data-value", dataExpanded)))
429+
# If `dataExpanded` is not the empty string, we need to check that it is eqaul to the
430+
# this menuItem's `expandedName``
431+
isExpanded <- nzchar(dataExpanded) && (dataExpanded == expandedName)
423432

424433
tags$li(class = "treeview",
425434
a(href = href,
@@ -433,6 +442,7 @@ menuItem <- function(text, ..., icon = NULL, badgeLabel = NULL, badgeColor = "gr
433442
do.call(tags$ul, c(
434443
class = paste0("treeview-menu", if (isExpanded) " menu-open" else ""),
435444
style = paste0("display: ", if (isExpanded) "block;" else "none;"),
445+
`data-expanded` = expandedName,
436446
subItems))
437447
)
438448
}

inst/shinydashboard.js

Lines changed: 11 additions & 34 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

inst/shinydashboard.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

inst/shinydashboard.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)