Skip to content

Commit 531f31b

Browse files
wchgadenbuie
andauthored
textInput(), textAreaInput(), numericInput(), passwordInput(): allow updating value on blur (#4183)
* textInput: Add updateOn parameter and allow setting debounce delay * `devtools::document()` (GitHub Actions) * `yarn build` (GitHub Actions) * Update news * Remove debounce parameter * `devtools::document()` (GitHub Actions) * `yarn build` (GitHub Actions) * Add updateOn parameter to numericInput, passwordInput * Add updateOn to textAreaInput() * `devtools::document()` (GitHub Actions) * feat: Ignore change events unless from server messages when `updateOn="blur"` * refactor: `updateOn="change"` instead of `"input"` * feat: Update inputs on Enter or Cmd/Ctrl+Enter (textarea) * chore: Document `...` and ensure they are empty * chore: Use `rlang::arg_match()` * chore: code style (air format) * fix: textAreaInput, not inputTextArea * docs(NEWS): Minor edit * chore: If element has focus, ignore change event --------- Co-authored-by: wch <[email protected]> Co-authored-by: Garrick Aden-Buie <[email protected]>
1 parent 58e1521 commit 531f31b

File tree

14 files changed

+237
-70
lines changed

14 files changed

+237
-70
lines changed

NEWS.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111

1212
* Shiny's Typescript assets are now compiled to ES2021 instead of ES5. (#4066)
1313

14+
* When `textInput()` is called with `updateOn="blur"`, instead of updating as the user types, the input value will update only when the text input loses focus or when the user presses Enter (or Cmd/Ctrl + Enter for `textAreaInput()`). (#4183)
15+
16+
1417
## Bug fixes
1518

1619
* Fixed a bug with modals where calling `removeModal()` too quickly after `showModal()` would fail to remove the modal if the remove modal message was received while the modal was in the process of being revealed. (#4173)

R/input-numeric.R

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,22 +29,36 @@
2929
#' A numeric vector of length 1.
3030
#'
3131
#' @export
32-
numericInput <- function(inputId, label, value, min = NA, max = NA, step = NA,
33-
width = NULL) {
32+
numericInput <- function(
33+
inputId,
34+
label,
35+
value,
36+
min = NA,
37+
max = NA,
38+
step = NA,
39+
width = NULL,
40+
...,
41+
updateOn = c("change", "blur")
42+
) {
43+
rlang::check_dots_empty()
44+
updateOn <- rlang::arg_match(updateOn)
3445

3546
value <- restoreInput(id = inputId, default = value)
3647

3748
# build input tag
38-
inputTag <- tags$input(id = inputId, type = "number", class="shiny-input-number form-control",
39-
value = formatNoSci(value))
40-
if (!is.na(min))
41-
inputTag$attribs$min = min
42-
if (!is.na(max))
43-
inputTag$attribs$max = max
44-
if (!is.na(step))
45-
inputTag$attribs$step = step
49+
inputTag <- tags$input(
50+
id = inputId,
51+
type = "number",
52+
class = "shiny-input-number form-control",
53+
value = formatNoSci(value),
54+
`data-update-on` = updateOn
55+
)
56+
if (!is.na(min)) inputTag$attribs$min = min
57+
if (!is.na(max)) inputTag$attribs$max = max
58+
if (!is.na(step)) inputTag$attribs$step = step
4659

47-
div(class = "form-group shiny-input-container",
60+
div(
61+
class = "form-group shiny-input-container",
4862
style = css(width = validateCssUnit(width)),
4963
shinyInputLabel(inputId, label),
5064
inputTag

R/input-password.R

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,29 @@
3030
#' shinyApp(ui, server)
3131
#' }
3232
#' @export
33-
passwordInput <- function(inputId, label, value = "", width = NULL,
34-
placeholder = NULL) {
35-
div(class = "form-group shiny-input-container",
33+
passwordInput <- function(
34+
inputId,
35+
label,
36+
value = "",
37+
width = NULL,
38+
placeholder = NULL,
39+
...,
40+
updateOn = c("change", "blur")
41+
) {
42+
rlang::check_dots_empty()
43+
updateOn <- rlang::arg_match(updateOn)
44+
45+
div(
46+
class = "form-group shiny-input-container",
3647
style = css(width = validateCssUnit(width)),
3748
shinyInputLabel(inputId, label),
38-
tags$input(id = inputId, type="password", class="shiny-input-password form-control", value=value,
39-
placeholder = placeholder)
49+
tags$input(
50+
id = inputId,
51+
type = "password",
52+
class = "shiny-input-password form-control",
53+
value = value,
54+
placeholder = placeholder,
55+
`data-update-on` = updateOn
56+
)
4057
)
4158
}

R/input-text.R

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@
1010
#' @param placeholder A character string giving the user a hint as to what can
1111
#' be entered into the control. Internet Explorer 8 and 9 do not support this
1212
#' option.
13+
#' @param ... Ignored, included to require named arguments and for future
14+
#' feature expansion.
15+
#' @param updateOn A character vector specifying when the input should be
16+
#' updated. Options are `"change"` (default) and `"blur"`. Use `"change"` to
17+
#' update the input immediately whenever the value changes. Use `"blur"`to
18+
#' delay the input update until the input loses focus (the user moves away
19+
#' from the input), or when Enter is pressed (or Cmd/Ctrl + Enter for
20+
#' [textAreaInput()]).
1321
#' @return A text input control that can be added to a UI definition.
1422
#'
1523
#' @family input elements
@@ -34,15 +42,31 @@
3442
#' unless `value` is provided.
3543
#'
3644
#' @export
37-
textInput <- function(inputId, label, value = "", width = NULL,
38-
placeholder = NULL) {
45+
textInput <- function(
46+
inputId,
47+
label,
48+
value = "",
49+
width = NULL,
50+
placeholder = NULL,
51+
...,
52+
updateOn = c("change", "blur")
53+
) {
54+
rlang::check_dots_empty()
55+
updateOn <- rlang::arg_match(updateOn)
3956

4057
value <- restoreInput(id = inputId, default = value)
4158

42-
div(class = "form-group shiny-input-container",
59+
div(
60+
class = "form-group shiny-input-container",
4361
style = css(width = validateCssUnit(width)),
4462
shinyInputLabel(inputId, label),
45-
tags$input(id = inputId, type="text", class="shiny-input-text form-control", value=value,
46-
placeholder = placeholder)
63+
tags$input(
64+
id = inputId,
65+
type = "text",
66+
class = "shiny-input-text form-control",
67+
value = value,
68+
placeholder = placeholder,
69+
`data-update-on` = updateOn
70+
)
4771
)
4872
}

R/input-textarea.R

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,21 @@
4141
#' unless `value` is provided.
4242
#'
4343
#' @export
44-
textAreaInput <- function(inputId, label, value = "", width = NULL, height = NULL,
45-
cols = NULL, rows = NULL, placeholder = NULL, resize = NULL) {
44+
textAreaInput <- function(
45+
inputId,
46+
label,
47+
value = "",
48+
width = NULL,
49+
height = NULL,
50+
cols = NULL,
51+
rows = NULL,
52+
placeholder = NULL,
53+
resize = NULL,
54+
...,
55+
updateOn = c("change", "blur")
56+
) {
57+
rlang::check_dots_empty()
58+
updateOn <- rlang::arg_match(updateOn)
4659

4760
value <- restoreInput(id = inputId, default = value)
4861

@@ -57,7 +70,8 @@ textAreaInput <- function(inputId, label, value = "", width = NULL, height = NUL
5770
resize = resize
5871
)
5972

60-
div(class = "form-group shiny-input-container",
73+
div(
74+
class = "form-group shiny-input-container",
6175
shinyInputLabel(inputId, label),
6276
style = if (!is.null(width)) paste0("width: ", validateCssUnit(width), ";"),
6377
tags$textarea(
@@ -67,6 +81,7 @@ textAreaInput <- function(inputId, label, value = "", width = NULL, height = NUL
6781
style = style,
6882
rows = rows,
6983
cols = cols,
84+
`data-update-on` = updateOn,
7085
value
7186
)
7287
)

inst/www/shared/shiny.js

Lines changed: 27 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

inst/www/shared/shiny.js.map

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

inst/www/shared/shiny.min.js

Lines changed: 15 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

inst/www/shared/shiny.min.js.map

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/numericInput.Rd

Lines changed: 13 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)