Skip to content

Commit

Permalink
add media list support for f7CheckboxGroup
Browse files Browse the repository at this point in the history
  • Loading branch information
DivadNojnarg committed Mar 16, 2024
1 parent 4cbabff commit ecc66ec
Show file tree
Hide file tree
Showing 12 changed files with 190 additions and 15 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export(f7BlockTitle)
export(f7Button)
export(f7Card)
export(f7Checkbox)
export(f7CheckboxChoice)
export(f7CheckboxGroup)
export(f7Chip)
export(f7Col)
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ We now return an object of objects which becomes a list of lists.
- `f7Card()` get a new `raised` and `divider` parameters.
- `f7CheckboxGroup()` has a new `position` parameter to control
the check icon position. Default to left.
- `f7CheckboxChoice()`: new function to pass inside `choices` in a
`f7CheckboxGroup()`. Improved choice with title, subtitle, ...
- Fix various issues in documentation.

# shinyMobile 1.0.1
Expand Down
75 changes: 69 additions & 6 deletions R/f7-inputs.R
Original file line number Diff line number Diff line change
Expand Up @@ -944,8 +944,11 @@ updateF7Checkbox <- function(inputId, label = NULL, value = NULL,
#'
#' @param inputId Checkbox group input.
#' @param label Checkbox group label.
#' @param choices Checkbox group choices.
#' @param selected Checkbox group selected value.
#' @param choices Checkbox group choices. Can be a simple
#' vector or named list or a list of \link{f7CheckboxChoice}.
#' @param selected Checkbox group selected value. If you pass
#' \link{f7CheckboxChoice} in choices, selected must be a numeric
#' value corresponding to the element to select.
#' @param position Check mark position.
#' \code{"left"} or \code{"right"}.
#'
Expand All @@ -961,35 +964,62 @@ f7CheckboxGroup <- function(
"left" = "start",
"right" = "end"
)
selectedPosition <- if (!is.null(selected)) match(selected, choices) else NULL

has_media_list <- inherits(choices[[1]], "custom_choice")
if (has_media_list) position <- "start"

selectedPosition <- NULL
selectedPosition <- if (!is.null(selected)) {
if (has_media_list) {
if (!is.numeric(selected) || selected > length(choices)) {
stop("When using f7CheckboxChoice, selected must be a numeric
value of the choice to select. selected can't be higher than
the total number of choices.")
}
selected
} else {
match(selected, choices)
}
}
choicesTag <- lapply(X = seq_along(choices), function(i) {
shiny::tags$li(
shiny::tags$label(
class = sprintf("item-checkbox item-checkbox-icon-%s item-content", position),
shiny::tags$input(
type = "checkbox",
name = inputId,
value = choices[[i]],
value = if (has_media_list) {
names(choices)[[i]] %OR% i
} else {
choices[[i]]
},
checked = if (!is.null(selectedPosition)) {
if (i == selectedPosition) NA
}
),
shiny::tags$i(class = "icon icon-checkbox"),
shiny::tags$div(
class = "item-inner",
shiny::tags$div(class = "item-title", choices[[i]])
if (has_media_list) {
choices[[i]]
} else {
shiny::tags$div(class = "item-title", choices[[i]])
}
)
)
)
})

mainCl <- "list list-strong-ios list-outline-ios list-dividers-ios shiny-input-checkboxgroup"
if (has_media_list) mainCl <- paste(mainCl, "media-list")

shiny::tagList(
shiny::tags$div(
class = "block-title",
label
),
shiny::tags$div(
class = "list list-strong-ios list-outline-ios list-dividers-ios shiny-input-checkboxgroup",
class = mainCl,
id = inputId,
shiny::tags$ul(
choicesTag
Expand All @@ -998,6 +1028,39 @@ f7CheckboxGroup <- function(
)
}

#' Framework7 checkbox group custom choice
#'
#' Custom choice item for \link{f7CheckboxGroup}.
#'
#' @param ... Choice content. Text is striped if too long.
#' @param title Item title.
#' @param subtitle Item subtitle.
#' @param after Display at the right of title.
#'
#' @export
#' @rdname checkboxgroup
f7CheckboxChoice <- function(..., title, subtitle = NULL, after = NULL) {
structure(
tagList(
shiny::tags$div(
class = "item-title-row",
shiny::tags$div(class = "item-title", title),
if (!is.null(after)) {
shiny::tags$div(
class = "item-after",
after
)
}
),
if (!is.null(subtitle)) {
shiny::tags$div(class = "item-subtitle", subtitle)
},
shiny::tags$div(class = "item-text", ...)
),
class = "custom_choice"
)
}

#' Create option html tag based on choice input
#'
#' Used by \link{f7SmartSelect} and \link{f7Select}
Expand Down
30 changes: 29 additions & 1 deletion inst/examples/checkboxgroup/app.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,41 @@ app <- shinyApp(
title = "My app",
f7SingleLayout(
navbar = f7Navbar(title = "f7CheckboxGroup"),
f7BlockTitle("Simple choices", size = "large"),
f7CheckboxGroup(
inputId = "checkboxgroup",
label = "Choose a variable:",
choices = colnames(mtcars)[-1],
selected = "disp",
position = "right"
),
tableOutput("data")
tableOutput("data"),
f7BlockTitle("Custom choices: f7CheckboxChoice", size = "large"),
f7CheckboxGroup(
inputId = "checkboxgroup2",
label = "Custom choices",
choices = list(
f7CheckboxChoice(
"Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Nulla sagittis tellus ut turpis condimentum,
ut dignissim lacus tincidunt",
title = "Choice 1",
subtitle = "David",
after = "March 16, 2024"
),
f7CheckboxChoice(
"Cras dolor metus, ultrices condimentum sodales sit
amet, pharetra sodales eros. Phasellus vel felis tellus.
Mauris rutrum ligula nec dapibus feugiat",
title = "Choice 2",
subtitle = "Veerle",
after = "March 17, 2024"
)
),
selected = 2,
position = "right"
),
textOutput("selected")
)
),
server = function(input, output) {
Expand All @@ -23,6 +50,7 @@ app <- shinyApp(
},
rownames = TRUE
)
output$selected <- renderText(input$checkboxgroup2)
}
)

Expand Down
50 changes: 47 additions & 3 deletions man/checkboxgroup.Rd

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

Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"input": {
"checkboxgroup": "disp"
"checkboxgroup": "disp",
"checkboxgroup2": "2"
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"input": {
"checkboxgroup": "wt"
"checkboxgroup": "wt",
"checkboxgroup2": "1"
}
}
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
41 changes: 38 additions & 3 deletions tests/testthat/test-checkbox.R
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,41 @@ test_that("checkboxgroup tag", {
selectedTag()

expect_length(items, length(colnames(mtcars)) - 1)

# With f7CheckboxChoice
expect_error(
f7CheckboxGroup(
inputId = "checkboxgroup2",
label = "Custom choices",
choices = list(
f7CheckboxChoice(
"Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Nulla sagittis tellus ut turpis condimentum,
ut dignissim lacus tincidunt",
title = "Choice 1",
subtitle = "David",
after = "March 16, 2024"
)
),
selected = 2
)
)
checkbox_group <- f7CheckboxGroup(
inputId = "checkboxgroup2",
label = "Custom choices",
choices = list(
f7CheckboxChoice(
"Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Nulla sagittis tellus ut turpis condimentum,
ut dignissim lacus tincidunt",
title = "Choice 1",
subtitle = "David",
after = "March 16, 2024"
)
)
)

expect_true(grepl("media-list", checkbox_group[[2]]$attribs$class))
})

library(shinytest2)
Expand All @@ -58,7 +93,7 @@ test_that("checkboxgroup works as expected", {
name = "checkboxgroup-app",
variant = platform_variant()
)
app$expect_values(input = "checkboxgroup")
app$set_inputs("checkboxgroup" = "wt")
app$expect_values(input = "checkboxgroup")
app$expect_values(input = c("checkboxgroup", "checkboxgroup2"))
app$set_inputs("checkboxgroup" = "wt", "checkboxgroup2" = "1")
app$expect_values(input = c("checkboxgroup", "checkboxgroup2"))
})

0 comments on commit ecc66ec

Please sign in to comment.