Skip to content

Commit 35520e4

Browse files
committed
added expandedName and startExpanded params to menuItem()
1 parent 3182d49 commit 35520e4

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
@@ -240,6 +240,13 @@ sidebarSearchForm <- function(textId, buttonId, label = "Search...",
240240
#' @param selected If \code{TRUE}, this \code{menuItem} or \code{menuSubItem}
241241
#' will start selected. If no item have \code{selected=TRUE}, then the first
242242
#' \code{menuItem} will start selected.
243+
#' @param expandedName A unique name given to each \code{menuItem} that serves
244+
#' to indicate which one (if any) is currently expanded. (This is only applicable
245+
#' to \code{menuItem}s that have children and it is mostly only useful for
246+
#' bookmarking state.)
247+
#' @param startExpanded Should this \code{menuItem} be expanded on app startup?
248+
#' (This is only applicable to \code{menuItem}s that have children, and only
249+
#' one of these can be expanded at any given time).
243250
#' @param ... For menu items, this may consist of \code{\link{menuSubItem}}s.
244251
#' @param .list An optional list containing items to put in the menu Same as the
245252
#' \code{...} arguments, but in list format. This can be useful when working
@@ -351,7 +358,9 @@ sidebarMenu <- function(..., id = NULL, .list = NULL) {
351358
#' @rdname sidebarMenu
352359
#' @export
353360
menuItem <- function(text, ..., icon = NULL, badgeLabel = NULL, badgeColor = "green",
354-
tabName = NULL, href = NULL, newtab = TRUE, selected = NULL) {
361+
tabName = NULL, href = NULL, newtab = TRUE, selected = NULL,
362+
expandedName = as.character(gsub("[[:space:]]", "", text)),
363+
startExpanded = FALSE) {
355364
subItems <- list(...)
356365

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

416-
# If `dataExpanded` is not the empty string, we need to check that there is a subMenuItem
417-
# in the list of `subItems` that actually has a `data-value` attribute equal to `dataExpanded`
418-
isExpanded <- nzchar(dataExpanded) &&
419-
any(unlist(lapply(subItems, findAttribute, "data-value", dataExpanded)))
426+
# If `dataExpanded` is not the empty string, we need to check that it is eqaul to the
427+
# this menuItem's `expandedName``
428+
isExpanded <- nzchar(dataExpanded) && (dataExpanded == expandedName)
420429

421430
tags$li(class = "treeview",
422431
a(href = href,
@@ -430,6 +439,7 @@ menuItem <- function(text, ..., icon = NULL, badgeLabel = NULL, badgeColor = "gr
430439
do.call(tags$ul, c(
431440
class = paste0("treeview-menu", if (isExpanded) " menu-open" else ""),
432441
style = paste0("display: ", if (isExpanded) "block;" else "none;"),
442+
`data-expanded` = expandedName,
433443
subItems))
434444
)
435445
}

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)