Skip to content

Commit

Permalink
sd_next() spacing update
Browse files Browse the repository at this point in the history
  • Loading branch information
pingfan-hu committed Sep 11, 2024
1 parent 03e6ec6 commit 5a9b9c2
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 9 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# surveydown (development version)

- No need to use `<br>` above the next button anymore.

# surveydown 0.2.3

- Solved the speed problem for database connections: refactored `sd_server()` for efficiency; converted local data storage to lists instead of data frames.
Expand Down
37 changes: 29 additions & 8 deletions R/server.R
Original file line number Diff line number Diff line change
Expand Up @@ -375,10 +375,13 @@ basic_show_if_logic <- function(input, show_if) {
stop("show_if must be a data frame or tibble.")
}

# Initially hide all conditional questions
# Initially hide all conditional questions and their containers
unique_targets <- unique(show_if$target)
for (target in unique_targets) {
shinyjs::hide(target)
shinyjs::runjs(sprintf("
$('#%s').closest('.question-container').hide();
$('#%s').hide();
", target, target))
}

# Group show_if rules by question_id and target
Expand All @@ -394,18 +397,29 @@ basic_show_if_logic <- function(input, show_if) {
# Check if the condition is met to show/hide the question
val <- input[[question_id]]
if (!is.null(val) && val %in% question_values) {
shinyjs::show(target)
shinyjs::runjs(sprintf("
$('#%s').closest('.question-container').show();
$('#%s').show();
", target, target))
} else {
shinyjs::hide(target)
shinyjs::runjs(sprintf("
$('#%s').closest('.question-container').hide();
$('#%s').hide();
", target, target))
}
}, ignoreNULL = TRUE)
}
}

# Handle custom show-if logic
custom_show_if_logic <- function(input, show_if_custom) {
# Initially hide all conditional questions
lapply(show_if_custom, function(x) shinyjs::hide(x$target))
# Initially hide all conditional questions and their containers
lapply(show_if_custom, function(x) {
shinyjs::runjs(sprintf("
$('#%s').closest('.question-container').hide();
$('#%s').hide();
", x$target, x$target))
})

# Create a reactive expression for each condition
condition_reactives <- lapply(show_if_custom, function(rule) {
Expand All @@ -417,11 +431,18 @@ custom_show_if_logic <- function(input, show_if_custom) {
for (i in seq_along(show_if_custom)) {
condition_result <- condition_reactives[[i]]()
condition_met <- isTRUE(condition_result)
target <- show_if_custom[[i]]$target

if (condition_met) {
shinyjs::show(show_if_custom[[i]]$target)
shinyjs::runjs(sprintf("
$('#%s').closest('.question-container').show();
$('#%s').show();
", target, target))
} else {
shinyjs::hide(show_if_custom[[i]]$target)
shinyjs::runjs(sprintf("
$('#%s').closest('.question-container').hide();
$('#%s').hide();
", target, target))
}
}
})
Expand Down
2 changes: 1 addition & 1 deletion R/ui.R
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ sd_next <- function(next_page = NULL, label = "Next") {

shiny::tagList(
shiny::div(
style = "margin-top: 0.5rem;",
style = "margin-top: 0.5rem; margin-bottom: 0.5rem;",
shiny::actionButton(
inputId = button_id,
label = label,
Expand Down

0 comments on commit 5a9b9c2

Please sign in to comment.