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

Display multiple examples in dashboard #185

Merged
merged 4 commits into from
Jun 30, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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 inst/examples/DetailsList3.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ CustomComponents <- tags$script(HTML("(function() {
const React = jsmodule['react'];
const Fluent = jsmodule['@fluentui/react'];
const Shiny = jsmodule['@/shiny'];
const CustomComponents = jsmodule['CustomComponents'] = {};
const CustomComponents = jsmodule['CustomComponents'] ??= {};

function useSelection(inputId) {
const selection = React.useRef(new Fluent.Selection({
Expand Down
2 changes: 1 addition & 1 deletion inst/examples/TextField2.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ library(shiny.fluent)
CustomComponents <- tags$script(HTML("(function() {
const { InputAdapter } = jsmodule['@/shiny.react'];
const { TextField } = jsmodule['@fluentui/react'];
const CustomComponents = jsmodule['CustomComponents'] = {};
const CustomComponents = jsmodule['CustomComponents'] ??= {};

CustomComponents.UpperCaseTextField = InputAdapter(TextField, (value, setValue) => ({
value: value.toUpperCase(),
Expand Down
11 changes: 4 additions & 7 deletions inst/examples/dashboard/app.R
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,10 @@ sass(

server <- function(input, output, session) {
router_server()
example_servers <- unlist(map(examples_routes, "server"))
lapply(
examples,
function(item, modules = example_servers) {
modules[[item]](item)
}
)
examples_routes %>%
map("server") %>%
flatten() %>%
iwalk(function(server, id) server(id))
}

shinyApp(ui, server)
53 changes: 35 additions & 18 deletions inst/examples/dashboard/examples.R
Original file line number Diff line number Diff line change
Expand Up @@ -92,35 +92,52 @@ readExample <- function(path) {
list(code = code, ui = module$ui, server = module$server)
}

makeExamplePage <- function(name, ui, code) {
makeText <- function(text) {
jakubsob marked this conversation as resolved.
Show resolved Hide resolved
strsplit(text, "\\n\\n")[[1]] %>%
map(Text) %>%
Stack(tokens = list(childrenGap = 10))
}

makeExamplePage <- function(name, example) {
help <- getHelpList(name)
makePage(
name,
"Fluent UI component",
div(
makeCard("Description", Text(nowrap = FALSE, help$description)),
makeCard("Description", makeText(help$description)),
makeCard("Usage", pre(help$usage)),
makeCard("Live example", div(style = "padding: 20px", ui)),
makeCard("Live example code", pre(code))
imap(example, makeLiveExamplePage)
)
)
}

makeLiveExamplePage <- function(example, id) {
tagList(
makeCard("Live example", div(style = "padding: 20px", example$ui(id))),
makeCard("Live example code", pre(example$code))
)
jakubsob marked this conversation as resolved.
Show resolved Hide resolved
}

makeExampleRoute <- function(name) {
path <- system.file(file.path("examples", paste0(name, ".R")), package = "shiny.fluent")
example <- readExample(path)
example_server <- list()
example_server[[name]] <- example$server
return(
list(
server = example_server,
router = route(
path = name,
ui = makeExamplePage(
name = name,
ui = example$ui(name),
code = example$code
)
examples_files <- list.files(
system.file("examples", package = "shiny.fluent"),
full.names = TRUE
)
# Match on component names with optional digits at the end
pattern <- paste0("^", name, "([0-9]+)?.R")
jakubsob marked this conversation as resolved.
Show resolved Hide resolved
path <- examples_files[grepl(pattern, basename(examples_files))]
examples_names <- tools::file_path_sans_ext(basename(path))
example <- path %>%
map(readExample) %>%
set_names(examples_names)

list(
server = map(example, "server"),
router = route(
jakubsob marked this conversation as resolved.
Show resolved Hide resolved
path = name,
ui = makeExamplePage(
name = name,
example = example
)
)
)
Expand Down
11 changes: 5 additions & 6 deletions inst/examples/dashboard/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@ makePage <- function (title, subtitle, contents) {
}

makeCard <- function(title, content) {
div(class = "card ms-depth-8",
Stack(
tokens = list(childrenGap = 5),
Text(variant = "large", title, block = TRUE),
content
))
div(
class = "card ms-depth-8",
Text(variant = "large", title, block = TRUE),
content
)
}
Loading