@@ -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
356363menuItem <- 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}
0 commit comments