Skip to content

Commit

Permalink
start table
Browse files Browse the repository at this point in the history
  • Loading branch information
DivadNojnarg committed Nov 12, 2023
1 parent 00eaf9f commit ab1a455
Show file tree
Hide file tree
Showing 5 changed files with 174 additions and 0 deletions.
6 changes: 6 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ export(snippet)
export(spacer)
export(switch_input)
export(tab)
export(table)
export(table_body)
export(table_cell)
export(table_col)
export(table_header)
export(table_row)
export(tabs)
export(tabs_variants)
export(text_input)
Expand Down
59 changes: 59 additions & 0 deletions R/components.R
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,65 @@ skeleton <- component("Skeleton")
#' @export
snippet <- component("Snippet")

#' @rdname table
#' @inherit component params return
#' @keywords internal
.table <- component("Table")

#' @rdname table
#' @export
table_header <- component("TableHeader")

#' @rdname table
#' @export
table_body <- component("TableBody")

#' @rdname table
#' @export
table_col <- component("TableColumn")

#' @rdname table
#' @export
table_row <- component("TableRow")

#' @rdname table
#' @export
table_cell <-component("TableCell")

#' Table widget
#'
#' @rdname table
#' @param data Data to render.
#' @export
table <- function(data = NULL, ...) {
cols <- colnames(data)

if (is.null(data) || nrow(data) == 0) {
body <- table_body(
emptyContent = chip("No data :( ...", color = "danger"),
JS("[]")
)
} else {
body <- table_body(
lapply(seq_len(nrow(data)), function(i) {
table_row(
key = i,
lapply(seq_along(data[i, ]), function(j) {
table_cell(data[i, j])
})
)
})
)
}

.table(
...,
label = "My Table",
table_header(lapply(cols, table_col)),
body
)
}

#' @rdname user
#' @inherit component params return
#' @export
Expand Down
57 changes: 57 additions & 0 deletions inst/examples/table/app.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
library(shiny)
library(shinyNextUI)

ui <- nextui_page(
dark_mode = TRUE,
spacer(y = 5),
p(class = "font-extrabold text-2xl uppercase my-2", "No data"),
spacer(y = 2),
table(iris[0, ], hideHeader = TRUE),
spacer(y = 5),
p(class = "font-extrabold text-2xl uppercase my-2", "Single selection table"),
spacer(y = 2),
table(
iris[1:5, ],
removeWrapper = TRUE,
isStriped = TRUE,
color = "primary",
selectionMode = "single",
defaultSelectedKeys = JS("['2']")
),
spacer(y = 5),
p(class = "font-extrabold text-2xl uppercase my-2", "Multiple selection table"),
spacer(y = 2),
table(
iris[1:5, ],
color = "primary",
selectionMode = "multiple",
defaultSelectedKeys = JS("['1', '2']"),
disabledKeys = JS("['3']")
),
spacer(y = 5),
p(class = "font-extrabold text-2xl uppercase my-2", "Top/Bottom content"),
spacer(y = 2),
table(
iris[1:5, ],
isCompact = TRUE,
topContent = div(
class = "flex justify-between",
p("Top content ..."),
chip("My chip"),
badge(button("CLick me"), color = "success", placement = "top-right", content = "New")
),
bottomContent = div(
class = "flex justify-between",
p("Bottom content ..."),
link(
href = "https://nextui.org/docs/components/table",
target = "_blank",
"To the doc."
)
)
)
)

server <- function(input, output, session) {}

shinyApp(ui, server)
39 changes: 39 additions & 0 deletions man/table.Rd

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

13 changes: 13 additions & 0 deletions tests/testthat/test-table.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
library(shinytest2)
test_that("table works as expected", {
# Don't run these tests on the CRAN build servers
skip_on_cran()
shiny_app_path <-
system.file("examples/table/app.R", package = "shinyNextUI")
app <- AppDriver$new(
shiny_app_path,
name = "table-app",
variant = platform_variant()
)
app$expect_values()
})

0 comments on commit ab1a455

Please sign in to comment.