Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

accordions: allow multiple panels open and control which panels are open initially #113

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
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
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ Imports:
URL: https://github.com/ijlyttle/bsplus
BugReports: https://github.com/ijlyttle/bsplus/issues
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.2.1
RoxygenNote: 7.2.3
Encoding: UTF-8
Suggests: testthat,
shiny,
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ S3method(bs_set_opts,default)
S3method(bs_set_opts,shiny.tag)
export("%>%")
export(bs_accordion)
export(bs_accordion_multi)
export(bs_accordion_sidebar)
export(bs_append)
export(bs_attach_collapse)
Expand Down
102 changes: 102 additions & 0 deletions R/bs_accordion_multi.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
#' Allow multiple `bs_accordion` sections to be open, and specify which ones start as open
#'
#' @param X shinyBS::bs_accordion object
#' @param multi logical Should more than one panel be permitted to be open at one time.
#' @param open integer vector Indices of panels that should be open when the page loads.
#'
#' @return Modified shinyBS::bs_accordion object
#'
#' @export
#'
#' @examples
#'
#' if(interactive())
#' {
#' library(shiny)
#' library(bsplus)
#'
#' shinyApp(
#' ui = fluidPage(
#'
#' tags$header(
#' tags$style(
#' HTML(
#' '
#' .panel-heading.collapsed > .panel-title:before {
#' float: right !important;
#' padding-right: 5px;
#' content:"+"
#' }
#'
#' .panel-heading > .panel-title:before {
#' float: right !important;
#' font-family: FontAwesome;
#' padding-right: 5px;
#' content:"-"
#' }
#' '
#' )
#' )
#' ),
#'
#' tags$h2("Multiple Open Panels, Panels 1 and 3 open at page load"),
#' bs_accordion(id = "beatles") %>%
#' bs_set_opts(panel_type = "success", use_heading_link = TRUE) %>%
#' bs_append(title = "John Lennon", content = "Rhythm guitar, vocals") %>%
#' bs_append(title = "Paul McCartney", content = "Bass guitar, vocals") %>%
#' bs_append(title = "George Harrison", content = "Lead guitar, vocals") %>%
#' bs_append(title = "Ringo Starr", content = "Drums, vocals") %>%
#' bs_accordion_multi(
#' multi=TRUE,
#' open=c(1,3)
#' ),
#'
#' tags$h2("One Open Panel, No panels open on page load."),
#' bs_accordion(id = "fruit") %>%
#' bs_set_opts(panel_type = "info") %>%
#' bs_append(title = "Apples", content = "An apple a day keeps the doctor away.") %>%
#' bs_append(title = "Bannana", content = "Watch out for bannana peels!") %>%
#' bs_append(title = "Kumquat", content = "What is a kumquat?!") %>%
#' bs_accordion_multi(
#' multi=FALSE,
#' open=c()
#' )
#' ),
#'
#' server = function(input, output) {}
#' )
#'
#' }
#'
bs_accordion_multi <- function(X, multi=TRUE, open=1) {
for(i in 1:length(X$children))
{
if(multi)
# Remove 'data-parent' attribute so multiple panels can be open at once
X$children[[i]]$children[[1]]$attribs$`data-parent` <- NULL

# Remove 'in' class to prevent *any* panel from starting as open
classAttribs <- which(names(X$children[[i]]$children[[2]]$attribs) == "class")
for(j in classAttribs)
{
if(X$children[[i]]$children[[2]]$attribs[j]=="in")
{
X$children[[i]]$children[[2]]$attribs[j] <- NULL
}
}

if(i %in% open)
{
# Add 'in' class (back) to panels selected to start as open
X$children[[i]]$children[[2]]$attribs <- append(X$children[[i]]$children[[2]]$attribs, list(class="in"))
}
else
{
# Add 'collapsed' class to panels slected to start as closed, so css rules work properly
X$children[[i]]$children[[1]]$attribs <- append(X$children[[i]]$children[[1]]$attribs, list(class="collapsed"))
}
}

X

}
82 changes: 82 additions & 0 deletions man/bs_accordion_multi.Rd

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