Skip to content

Commit

Permalink
show if now working on questions on different pages
Browse files Browse the repository at this point in the history
  • Loading branch information
jhelvy committed Oct 6, 2024
1 parent dbbe64e commit beae45b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
2 changes: 1 addition & 1 deletion R/server.R
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ sd_server <- function(
current_page_id()

# Add a small delay to ensure DOM is updated
shiny::invalidateLater(10)
shiny::invalidateLater(50)

results <- show_if_results()
current_visibility <- question_visibility()
Expand Down
24 changes: 19 additions & 5 deletions R/ui.R
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ sd_question <- function(
return(output)
}

# Modify the output_div creation
# Create wrapper div
output_div <- shiny::tags$div(
id = paste0("container-", id),
`data-question-id` = id,
Expand All @@ -405,9 +405,10 @@ sd_question <- function(
if (!is.null(shiny::getDefaultReactiveDomain())) {
# In a reactive context, directly add to output with renderUI
shiny::isolate({
output_div <- shiny::tags$div(output)
output <- shiny::getDefaultReactiveDomain()$output
output[[id]] <- shiny::renderUI({
output_div
output_div
})
})
} else {
Expand Down Expand Up @@ -823,6 +824,7 @@ sd_display_value <- function(id, display_type = "inline", wrapper = NULL, ...) {
#' @param id Character string. A unique identifier for the output element.
#' @param type Character string. Specifies the type of output. Can be "question", "value", or `NULL.`
#' If `NULL`, the function behaves like `shiny::uiOutput()`.
#' @param width Character string. The width of the UI element. Defaults to "100%".
#' @param display Character string. Specifies the display type for "value" outputs.
#' Can be "inline", "text", "verbatim", or "ui". Only used when `type = "value"`.
#' @param wrapper Function. A function to wrap the output. Only used when `type = "value"`.
Expand Down Expand Up @@ -854,17 +856,29 @@ sd_display_value <- function(id, display_type = "inline", wrapper = NULL, ...) {
#' }
#'
#' @export
sd_output <- function(id, type = NULL, display = "inline", wrapper = NULL, ...) {
sd_output <- function(
id,
type = NULL,
width = "100%",
display = "inline",
wrapper = NULL,
...
) {
if (is.null(type)) {
# If only id is provided, behave like shiny::uiOutput
return(shiny::uiOutput(id, ...))
}

if (type == "question") {
return(shiny::div(
id = paste0("placeholder-", id),

# Create wrapper div
js_interaction <- sprintf("Shiny.setInputValue('%s_interacted', true, {priority: 'event'});", id)
return(shiny::tags$div(
id = paste0("container-", id),
`data-question-id` = id,
class = "question-container",
style = sprintf("width: %s;", width),
oninput = js_interaction,
shiny::uiOutput(id),
shiny::tags$span(class = "hidden-asterisk", "*")
))
Expand Down

0 comments on commit beae45b

Please sign in to comment.