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

disabled in verbatim_popup_srv is longer triggered when button i… #99

Merged
merged 5 commits into from
Oct 27, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# teal.widgets 0.2.0.9000

### Enhancements

* `disabled` in `verbatim_popup_srv` is longer triggered when button is hidden.

# teal.widgets 0.2.0

### Breaking changes
Expand Down
35 changes: 21 additions & 14 deletions R/verbatim_popup.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,14 @@ verbatim_popup_ui <- function(id, button_label, ...) {
shiny::singleton(
shiny::tags$head(shiny::includeScript(system.file("js/verbatim_popup.js", package = "teal.widgets")))
),
shiny::actionButton(ns("button"), label = button_label, ...)
shiny::actionButton(
ns("button"),
label = div(
button_label,
uiOutput(ns("disable_controller"), inline = TRUE) # this is dummy output - see the comment in srv
),
...
)
)
}

Expand All @@ -53,7 +60,11 @@ verbatim_popup_srv <- function(id, verbatim_content, title, style = FALSE, disab
checkmate::assert_class(disabled, classes = "reactive")
moduleServer(id, function(input, output, session) {
ns <- session$ns
disabled_flag_observer(disabled, "button")
# In normal case we could enable/disable using observeEvent(disabled_flag(), ...)
# but observeEvent doesn't care whether output is displayed or not
# This means that if we want to prevent calulation of disabled_flag for hidden
# output, we need to use renderUI which triggers when the output is shown.
output$disable_controller <- shiny::renderUI(disable_button(disabled, "button"))
modal_content <- format_content(verbatim_content, style)
button_click_observer(
click_event = shiny::reactive(input$button),
Expand All @@ -65,25 +76,21 @@ verbatim_popup_srv <- function(id, verbatim_content, title, style = FALSE, disab
})
}

#' Creates a `shiny` observer handling the disabled flag.
#' Disables a button
#'
#' @details
#' When the flag is `TRUE` the button to open the popup is disabled; it is enabled otherwise.
#'
#' @keywords internal
#' @param disabled_flag (`reactive`) containing the flag
#' @param button_id (`character(1)`) the id of the controlled button
disabled_flag_observer <- function(disabled_flag, button_id) {
shiny::observeEvent(
disabled_flag(),
handlerExpr = {
if (disabled_flag()) {
shinyjs::disable(button_id)
} else {
shinyjs::enable(button_id)
}
}
)
disable_button <- function(disabled_flag, button_id) {
if (disabled_flag()) {
shinyjs::disable(button_id)
} else {
shinyjs::enable(button_id)
}
NULL
}

#' Creates a `shiny` observer handling button clicks.
Expand Down
10 changes: 5 additions & 5 deletions man/disabled_flag_observer.Rd → man/disable_button.Rd

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

9 changes: 7 additions & 2 deletions tests/testthat/test-verbatim_popup.R
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,13 @@ testthat::test_that(
}
)

testthat::test_that("disabled_flag_observer returns an observer", {
testthat::expect_true(inherits(disabled_flag_observer(shiny::reactive(FALSE), "test_id"), "Observer"))
testthat::test_that("disable_button returns NULL", {
shiny::withReactiveDomain(
domain = shiny::MockShinySession$new(),
expr = testthat::expect_null(
isolate(disable_button(shiny::reactive(FALSE), "test_id"))
)
)
})

testthat::test_that("button_click_observer returns an observer", {
Expand Down