Skip to content

Commit

Permalink
fix(card): grouping children for card body elements
Browse files Browse the repository at this point in the history
  • Loading branch information
nteetor committed Mar 14, 2020
1 parent ce0fbbe commit fc48547
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 10 deletions.
33 changes: 25 additions & 8 deletions R/card.R
Original file line number Diff line number Diff line change
Expand Up @@ -191,19 +191,36 @@ card <- function(..., header = NULL, footer = NULL) {

is_named <- names2(args) != ""

args[!is_named] <- lapply(args[!is_named], function(x) {
if (is_tag(x) && tag_class_re(x, "row|list-group")) {
x
attrs <- args[is_named]
args <- args[!is_named]

grouped <- reduce(args, function(acc, arg) {
if (is_tag(arg) && tag_class_re(arg, "row|list-group")) {
acc[[length(acc) + 1]] <- arg
acc[length(acc) + 1] <- list(NULL)
} else {
acc[[length(acc)]] <- c(acc[[length(acc)]], list(arg))
}

acc
}, init = list(NULL))

body <- lapply(grouped, function(grp) {
if (is_bare_list(grp)) {
div(class = "card-body", grp)
} else {
tags$div(class = "card-body", x)
grp
}
})

tag <- tags$div(
tag <- div(
class = "card",
header,
!!!args,
footer
!!!attrs,
!!!drop_nulls(list2(
header,
!!!body,
footer
))
)

s3_class_add(tag, "yonder_card")
Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/test-card.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ test_that("can pass strings to card", {
expect_silent(card("hello", "world"))

this <- card("hello")
expect_true(is_tag(this$children[[2]]))
expect_true(tag_class_re(this$children[[2]], "card-body"))
expect_true(is_tag(this$children[[1]]))
expect_true(tag_class_re(this$children[[1]], "card-body"))
})

test_that("card paragraphs are wrapped in .card-body", {
Expand Down

0 comments on commit fc48547

Please sign in to comment.