-
Notifications
You must be signed in to change notification settings - Fork 65
Add input_switch() #483
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
Merged
Merged
Add input_switch() #483
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
b762e41
Add input_switch()
cpsievert dc33aa1
document()
cpsievert e0aa340
Merge branch 'main' into input-switch
cpsievert 34f1c8f
Get rid of inline argument; improve Rd docs
cpsievert 3b1cb8f
Fix _pkgdown.yml
cpsievert 4d8b13b
`devtools::document()` (GitHub Actions)
cpsievert 03c6655
chore: Apply my suggestions
gadenbuie 0c74d31
Resave data (GitHub Action)
gadenbuie 58e8ecd
docs(input_switch): Add an example
gadenbuie 67a68e5
Apply suggestions from code review
cpsievert e4b42f1
`devtools::document()` (GitHub Actions)
cpsievert f0378fc
docs: Add NEWS item
gadenbuie d8e31bb
Merge branch 'main' into input-switch
gadenbuie File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -73,6 +73,7 @@ Collate: | |
| 'files.R' | ||
| 'fill.R' | ||
| 'imports.R' | ||
| 'input-switch.R' | ||
| 'layout.R' | ||
| 'nav-items.R' | ||
| 'nav-update.R' | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| #' Switch input control | ||
| #' | ||
| #' Create an on-off style switch control for specifying logical values. | ||
| #' | ||
| #' @examplesIf interactive() | ||
| #' library(shiny) | ||
| #' library(bslib) | ||
| #' | ||
| #' ui <- page_fixed( | ||
| #' title = "Keyboard Settings", | ||
| #' h2("Keyboard Settings"), | ||
| #' input_switch("auto_capitalization", "Auto-Capitalization", TRUE), | ||
| #' input_switch("auto_correction", "Auto-Correction", TRUE), | ||
| #' input_switch("check_spelling", "Check Spelling", TRUE), | ||
| #' input_switch("smart_punctuation", "Smart Punctuation"), | ||
| #' h2("Preview"), | ||
| #' verbatimTextOutput("preview") | ||
| #' ) | ||
| #' | ||
| #' server <- function(input, output, session) { | ||
| #' output$preview <- renderPrint({ | ||
| #' list( | ||
| #' auto_capitalization = input$auto_capitalization, | ||
| #' auto_correction = input$auto_correction, | ||
| #' check_spelling = input$check_spelling, | ||
| #' smart_punctuation = input$smart_punctuation | ||
| #' ) | ||
| #' }) | ||
| #' } | ||
| #' | ||
| #' shinyApp(ui, server) | ||
| #' | ||
| #' @param id An input id. | ||
| #' @param label A label for the switch. | ||
| #' @param value Whether or not the switch should be checked by default. | ||
| #' @param width Any valid [CSS unit][htmltools::validateCssUnit] (e.g., | ||
| #' `width="200px"`). | ||
| #' | ||
| #' @return Returns a UI element for a switch input control. The server value | ||
| #' received for the input corresponding to `id` will be a logical | ||
| #' (`TRUE`/`FALSE`) value. | ||
| #' | ||
| #' @family input controls | ||
| #' @export | ||
| input_switch <- function(id, label, value = FALSE, width = NULL) { | ||
| tag <- input_checkbox(id, label, class = "form-check form-switch", value = value, width = width) | ||
| tag <- tag_require(tag, version = 5, caller = "input_switch()") | ||
| as_fragment(tag) | ||
| } | ||
|
|
||
| #' @rdname input_switch | ||
| #' @inheritParams nav_insert | ||
| #' @export | ||
| update_switch <- function(id, label = NULL, value = NULL, session = get_current_session()) { | ||
| message <- dropNulls(list(label = label, value = value)) | ||
| session$sendInputMessage(id, message) | ||
| } | ||
|
|
||
| input_checkbox <- function(id, label, class = "form-check", value = FALSE, width = NULL, inline = FALSE) { | ||
| div( | ||
| class = "form-group shiny-input-container", | ||
| class = if (inline) "shiny-input-container-inline", | ||
| style = css(width = width), | ||
| div( | ||
| class = class, | ||
| tags$input( | ||
| id = id, | ||
| class = "form-check-input", | ||
| type = "checkbox", | ||
| role = "switch", | ||
| checked = if (value) NA, | ||
| ), | ||
| tags$label( | ||
| # The span here is needed to adhere to shiny.js' checkbox binding logic | ||
| # https://github.com/rstudio/shiny/blob/c21ba0b/srcts/src/bindings/input/checkbox.ts#L42-L43 | ||
| tags$span(label), | ||
| class = "form-check-label", | ||
| `for` = id | ||
| ) | ||
| ) | ||
| ) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.