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

question_text() uses textAreaInput() if rows or cols are provided #460

Merged
merged 5 commits into from Jan 5, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions NEWS.md
Expand Up @@ -31,6 +31,7 @@ learnr (development version)
* `exercise.cap` now accepts HTML input. If no `exercise.cap` is provided, an icon of the exercise engine will be displayed. If no icon is known, the `exercise.cap` will default to the combination of the exercise engine and `" code"`. ([#397](https://github.com/rstudio/learnr/pull/397), [#429](https://github.com/rstudio/learnr/pull/429))
* `engine` is now passed to the `exercise.checker` to help distinguish what language is being checked in the exercise. ([#397](https://github.com/rstudio/learnr/pull/397))
* Hitting the `TAB` key in an exercise has always opened the auto-completion drop down. Now, hitting the `TAB` key will also complete the currently selected code completion. ([#428](https://github.com/rstudio/learnr/pull/428))
* `question_text()` gains `rows` and `cols` parameters. If either is provided, a multi-line `textAreaInput()` is used for the text input. ([#460](https://github.com/rstudio/learnr/pull/460), [#455](https://github.com/rstudio/learnr/issues/455))
* Feedback messages can now be an htmltools tag or tagList, or a character message ([#458](https://github.com/rstudio/learnr/pull/458))

## Bug fixes
Expand Down
23 changes: 21 additions & 2 deletions R/question_text.R
Expand Up @@ -5,6 +5,11 @@
#'
#' @inheritParams question
#' @inheritParams shiny::textInput
#' @param rows,cols Defines the size of the text input area in terms of the
#' number of rows or character columns visible to the user. If either `rows`
#' or `cols` are provided, the quiz input will use [shiny::textAreaInput()]
#' for the text input, otherwise the default input element is a single-line
#' [shiny::textInput()].
#' @param ... answers and extra parameters passed onto \code{\link{question}}.
#' @param trim Logical to determine if whitespace before and after the answer should be removed. Defaults to \code{TRUE}.
#' @seealso \code{\link{question_radio}}, \code{\link{question_checkbox}}
Expand All @@ -31,6 +36,8 @@ question_text <- function(
random_answer_order = FALSE,
placeholder = "Enter answer here...",
trim = TRUE,
rows = NULL,
cols = NULL,
options = list()
) {
checkmate::assert_character(placeholder, len = 1, null.ok = TRUE, any.missing = FALSE)
Expand All @@ -48,7 +55,9 @@ question_text <- function(
options,
list(
placeholder = placeholder,
trim = trim
trim = trim,
rows = rows,
cols = cols
)
)
)
Expand All @@ -58,7 +67,17 @@ question_text <- function(


question_ui_initialize.learnr_text <- function(question, value, ...) {
textInput(
# Use textInput() unless one of rows or cols are provided
textInputFn <-
if (is.null(question$options$rows) && is.null(question$options$cols)) {
textInput
} else {
function(...) {
textAreaInput(..., cols = question$options$cols, rows = question$options$rows)
}
}

textInputFn(
question$ids$answer,
label = question$question,
placeholder = question$options$placeholder,
Expand Down
8 changes: 8 additions & 0 deletions man/question_text.Rd

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

20 changes: 20 additions & 0 deletions tests/testthat/test-question_text.R
@@ -0,0 +1,20 @@
test_that("question text uses textAreaInput if rows or cols are provided", {
ans <- answer("", TRUE)

q_textArea_rows <- question_ui_initialize(question_text("A", ans, rows = 3), "")
expect_equal(q_textArea_rows$children[[2]]$name, "textarea")
expect_equal(q_textArea_rows$children[[2]]$attribs$rows, 3)

q_textArea_cols <- question_ui_initialize(question_text("A", ans, cols = 40), "")
expect_equal(q_textArea_cols$children[[2]]$name, "textarea")
expect_equal(q_textArea_cols$children[[2]]$attribs$cols, 40)

q_textArea <- question_ui_initialize(question_text("A", ans, rows = 4, cols = 30), "")
expect_equal(q_textArea$children[[2]]$name, "textarea")
expect_equal(q_textArea$children[[2]]$attribs$rows, 4)
expect_equal(q_textArea$children[[2]]$attribs$cols, 30)

q_text <- question_ui_initialize(question_text("A", ans), "")
expect_equal(q_text$children[[2]]$name, "input")
expect_equal(q_text$children[[2]]$attribs$type, "text")
})