From ab1a4558f7b1f512b53a2ab34571b09ebfba3b19 Mon Sep 17 00:00:00 2001 From: DivadNojnarg Date: Sun, 12 Nov 2023 22:19:09 +0100 Subject: [PATCH] start table --- NAMESPACE | 6 ++++ R/components.R | 59 +++++++++++++++++++++++++++++++++++++ inst/examples/table/app.R | 57 +++++++++++++++++++++++++++++++++++ man/table.Rd | 39 ++++++++++++++++++++++++ tests/testthat/test-table.R | 13 ++++++++ 5 files changed, 174 insertions(+) create mode 100644 inst/examples/table/app.R create mode 100644 man/table.Rd create mode 100644 tests/testthat/test-table.R diff --git a/NAMESPACE b/NAMESPACE index 0abf54f..4a38a1a 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -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) diff --git a/R/components.R b/R/components.R index d3e643b..ca28a93 100644 --- a/R/components.R +++ b/R/components.R @@ -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 diff --git a/inst/examples/table/app.R b/inst/examples/table/app.R new file mode 100644 index 0000000..2df3162 --- /dev/null +++ b/inst/examples/table/app.R @@ -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) diff --git a/man/table.Rd b/man/table.Rd new file mode 100644 index 0000000..8dc4925 --- /dev/null +++ b/man/table.Rd @@ -0,0 +1,39 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/components.R +\name{.table} +\alias{.table} +\alias{table_header} +\alias{table_body} +\alias{table_col} +\alias{table_row} +\alias{table_cell} +\alias{table} +\title{Table widget} +\usage{ +.table(...) + +table_header(...) + +table_body(...) + +table_col(...) + +table_row(...) + +table_cell(...) + +table(data = NULL, ...) +} +\arguments{ +\item{...}{Props to pass to the component. +The allowed props are listed below in the \bold{Details} section.} + +\item{data}{Data to render.} +} +\value{ +Object with \code{shiny.tag} class suitable for use in the UI of a Shiny app. +} +\description{ +Table widget +} +\keyword{internal} diff --git a/tests/testthat/test-table.R b/tests/testthat/test-table.R new file mode 100644 index 0000000..4bb37ae --- /dev/null +++ b/tests/testthat/test-table.R @@ -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() +})