Skip to content

Commit

Permalink
add tabs but bug in multiple selected tabs...
Browse files Browse the repository at this point in the history
  • Loading branch information
DivadNojnarg committed Oct 14, 2018
1 parent 46fede5 commit c356252
Show file tree
Hide file tree
Showing 4 changed files with 183 additions and 0 deletions.
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ export(argonBadge)
export(argonCard)
export(argonImage)
export(argonSection)
export(argonTab)
export(argonTabSet)
126 changes: 126 additions & 0 deletions R/argonTabs.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
#' Create a Boostrap 4 tabs
#'
#' Build an argon tabs
#'
#' @param ... Slot for \link{argonTab}.
#' @param id argonTabs id. Should be unique.
#' @param card_wrapper Whether to embed tab content in a card. FALSE by default.
#' @param horizontal Whether to display tabs horizontally. TRUE by default.
#' @param circle Whether to display circled design. FALSE by default.
#' @param size Tabs size. "sm" by default. "md", "lg".
#'
#' @examples
#' if (interactive()) {
#' }
#'
#'
#' @author David Granjon, \email{dgranjon@@ymail.com}
#'
#' @export
argonTabSet <- function(..., id, card_wrapper = FALSE, horizontal = TRUE, circle = FALSE,
size = "sm") {

tabCl <- "nav nav-pills nav-fill flex-column"
if (horizontal) {
if (!is.null(size)) tabCl <- paste0(tabCl, " flex-", size, "-row")
}
if (circle) tabCl <- paste0(tabCl, " nav-pills-circle")

tabItems <- list(...)
len_items <- length(tabItems)

found_active <- FALSE

# generate the menu
tabMenu <- htmltools::tags$div(
class = "nav-wrapper",
htmltools::tags$ul(
class = tabCl,
id = paste0(id, "-menu"),
role = "tablist",
lapply(X = 1:len_items, FUN = function(i) {
current_item <- tabItems[[i]]
current_item_cl <- current_item$attribs$class
current_item_name <- current_item$attribs$id
current_item_label <- paste0(current_item_name, "-tab")

# make sure that if the user set 2 tabs active at the same time,
# only the first one is selected
if (!found_active) {
active <- sum(grep(x = current_item_cl, pattern = "show active")) == 1
if (active) found_active <<- TRUE
# if there is already an active panel, set all other to inactive
} else {
active <- FALSE
}

htmltools::tags$li(
class = "nav-item",
htmltools::a(
class = if (active) "nav-link mb-sm-3 mb-md-0 active" else "nav-link mb-sm-3 mb-md-0",
id = current_item_label,
`data-toggle` = "tab",
href = paste0("#", current_item_name),
role = "tab",
`aria-controls` = current_item_name,
`aria-selected` = "true",
current_item_name
)
)
})
)
)

# generate the container
tabContent <- htmltools::tags$div(
class = "tab-content",
id = id,
...
)

tabWrapper <- if (card_wrapper) {
htmltools::tags$div(
class = "card shadow",
htmltools::tags$div(class = "card-body", tabContent)
)
} else {
tabContent
}

htmltools::tagList(tabMenu, tabWrapper)

}




#' Create a Boostrap 4 tab item
#'
#' Build an argon tab item
#'
#' @param ... Tab content
#' @param tabName Tab name: it will be also passed as the id argument. Should be unique.
#' @param active Whether the tab is active or not. FALSE bu default.
#'
#' @author David Granjon, \email{dgranjon@@ymail.com}
#'
#' @export
argonTab <- function(..., tabName, active = FALSE) {

tabCl <- if (active) "tab-pane fade show active" else "tab-pane fade"

id <- tabName
# handle punctuation
id <- gsub(x = id, pattern = "[[:punct:]]", replacement = "")
# handle tab names with space
id <- gsub(x = id, pattern = " ", replacement = "")

htmltools::tags$div(
class = tabCl,
role = "tabpanel",
id = id,
`aria-labelledby` = paste0(id, "-tab"),
...
)

}
21 changes: 21 additions & 0 deletions man/argonTab.Rd

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

34 changes: 34 additions & 0 deletions man/argonTabSet.Rd

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

0 comments on commit c356252

Please sign in to comment.