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