Skip to content

Commit 33dc41c

Browse files
committed
Add disabled argument to actionButton and updateActionButton
1 parent 4b6e257 commit 33dc41c

File tree

10 files changed

+87
-48
lines changed

10 files changed

+87
-48
lines changed

R/input-action.R

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
#' @param label The contents of the button or link--usually a text label, but
88
#' you could also use any other HTML, like an image.
99
#' @param icon An optional [icon()] to appear on the button.
10+
#' @param disabled If `TRUE`, the button will not be clickable. Use
11+
#' [updateActionButton()] to dynamically enable/disable the button.
1012
#' @param ... Named attributes to be applied to the button or link.
1113
#'
1214
#' @family input elements
@@ -49,7 +51,8 @@
4951
#' * Event handlers (e.g., [observeEvent()], [eventReactive()]) won't execute on initial load.
5052
#' * Input validation (e.g., [req()], [need()]) will fail on initial load.
5153
#' @export
52-
actionButton <- function(inputId, label, icon = NULL, width = NULL, ...) {
54+
actionButton <- function(inputId, label, icon = NULL, width = NULL,
55+
disabled = FALSE, ...) {
5356

5457
value <- restoreInput(id = inputId, default = NULL)
5558

@@ -58,6 +61,7 @@ actionButton <- function(inputId, label, icon = NULL, width = NULL, ...) {
5861
type="button",
5962
class="btn btn-default action-button",
6063
`data-val` = value,
64+
disabled = if (isTRUE(disabled)) NA else NULL,
6165
list(validateIcon(icon), label),
6266
...
6367
)

R/update-input.R

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,8 @@ updateCheckboxInput <- function(session = getDefaultReactiveDomain(), inputId, l
119119
#' Change the label or icon of an action button on the client
120120
#'
121121
#' @template update-input
122+
#' @param disabled If `TRUE`, the button will not be clickable; if `FALSE`, it
123+
#' will be.
122124
#' @inheritParams actionButton
123125
#'
124126
#' @seealso [actionButton()]
@@ -169,16 +171,18 @@ updateCheckboxInput <- function(session = getDefaultReactiveDomain(), inputId, l
169171
#' }
170172
#' @rdname updateActionButton
171173
#' @export
172-
updateActionButton <- function(session = getDefaultReactiveDomain(), inputId, label = NULL, icon = NULL) {
174+
updateActionButton <- function(session = getDefaultReactiveDomain(), inputId, label = NULL, icon = NULL, disabled = NULL) {
173175
validate_session_object(session)
174176

175177
if (!is.null(icon)) icon <- as.character(validateIcon(icon))
176-
message <- dropNulls(list(label=label, icon=icon))
178+
message <- dropNulls(list(label=label, icon=icon, disabled=disabled))
177179
session$sendInputMessage(inputId, message)
178180
}
179181
#' @rdname updateActionButton
180182
#' @export
181-
updateActionLink <- updateActionButton
183+
updateActionLink <- function(session = getDefaultReactiveDomain(), inputId, label = NULL, icon = NULL) {
184+
updateActionButton(session, inputId=inputId, label=label, icon=icon)
185+
}
182186

183187

184188
#' Change the value of a date input on the client

inst/www/shared/shiny.js

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9970,22 +9970,31 @@
99709970
key: "receiveMessage",
99719971
value: function receiveMessage(el, data) {
99729972
var $el = (0, import_jquery16.default)(el);
9973-
var label = $el.text();
9974-
var icon = "";
9975-
if ($el.find("i[class]").length > 0) {
9976-
var iconHtml = $el.find("i[class]")[0];
9977-
if (iconHtml === $el.children()[0]) {
9978-
icon = (0, import_jquery16.default)(iconHtml).prop("outerHTML");
9973+
if (hasDefinedProperty(data, "label") || hasDefinedProperty(data, "icon")) {
9974+
var label = $el.text();
9975+
var icon = "";
9976+
if ($el.find("i[class]").length > 0) {
9977+
var iconHtml = $el.find("i[class]")[0];
9978+
if (iconHtml === $el.children()[0]) {
9979+
icon = (0, import_jquery16.default)(iconHtml).prop("outerHTML");
9980+
}
99799981
}
9982+
if (hasDefinedProperty(data, "label")) {
9983+
label = data.label;
9984+
}
9985+
if (hasDefinedProperty(data, "icon")) {
9986+
var _data$icon;
9987+
icon = Array.isArray(data.icon) ? "" : (_data$icon = data.icon) !== null && _data$icon !== void 0 ? _data$icon : "";
9988+
}
9989+
$el.html(icon + " " + label);
99809990
}
9981-
if (hasDefinedProperty(data, "label")) {
9982-
label = data.label;
9983-
}
9984-
if (hasDefinedProperty(data, "icon")) {
9985-
var _data$icon;
9986-
icon = Array.isArray(data.icon) ? "" : (_data$icon = data.icon) !== null && _data$icon !== void 0 ? _data$icon : "";
9991+
if (hasDefinedProperty(data, "disabled")) {
9992+
if (data.disabled) {
9993+
$el.attr("disabled", "");
9994+
} else {
9995+
$el.attr("disabled", null);
9996+
}
99879997
}
9988-
$el.html(icon + " " + label);
99899998
}
99909999
}, {
999110000
key: "unsubscribe",

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: 1 addition & 1 deletion
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/actionButton.Rd

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

man/updateActionButton.Rd

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

srcts/src/bindings/input/actionbutton.ts

Lines changed: 37 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@ import $ from "jquery";
22
import { hasDefinedProperty } from "../../utils";
33
import { InputBinding } from "./inputBinding";
44

5-
type ActionButtonReceiveMessageData = { label?: string; icon?: string | [] };
5+
type ActionButtonReceiveMessageData = {
6+
label?: string;
7+
icon?: string | [];
8+
disabled?: boolean;
9+
};
610

711
class ActionButtonInputBinding extends InputBinding {
812
find(scope: HTMLElement): JQuery<HTMLElement> {
@@ -38,34 +42,44 @@ class ActionButtonInputBinding extends InputBinding {
3842
receiveMessage(el: HTMLElement, data: ActionButtonReceiveMessageData): void {
3943
const $el = $(el);
4044

41-
// retrieve current label and icon
42-
let label: string = $el.text();
43-
let icon = "";
45+
if (hasDefinedProperty(data, "label") || hasDefinedProperty(data, "icon")) {
46+
// retrieve current label and icon
47+
let label: string = $el.text();
48+
let icon = "";
4449

45-
// to check (and store) the previous icon, we look for a $el child
46-
// object that has an i tag, and some (any) class (this prevents
47-
// italicized text - which has an i tag but, usually, no class -
48-
// from being mistakenly selected)
49-
if ($el.find("i[class]").length > 0) {
50-
const iconHtml = $el.find("i[class]")[0];
50+
// to check (and store) the previous icon, we look for a $el child
51+
// object that has an i tag, and some (any) class (this prevents
52+
// italicized text - which has an i tag but, usually, no class -
53+
// from being mistakenly selected)
54+
if ($el.find("i[class]").length > 0) {
55+
const iconHtml = $el.find("i[class]")[0];
5156

52-
if (iconHtml === $el.children()[0]) {
53-
// another check for robustness
54-
icon = $(iconHtml).prop("outerHTML");
57+
if (iconHtml === $el.children()[0]) {
58+
// another check for robustness
59+
icon = $(iconHtml).prop("outerHTML");
60+
}
5561
}
56-
}
5762

58-
// update the requested properties
59-
if (hasDefinedProperty(data, "label")) {
60-
label = data.label;
61-
}
62-
if (hasDefinedProperty(data, "icon")) {
63-
// `data.icon` can be an [] if user gave `character(0)`.
64-
icon = Array.isArray(data.icon) ? "" : data.icon ?? "";
63+
// update the requested properties
64+
if (hasDefinedProperty(data, "label")) {
65+
label = data.label;
66+
}
67+
if (hasDefinedProperty(data, "icon")) {
68+
// `data.icon` can be an [] if user gave `character(0)`.
69+
icon = Array.isArray(data.icon) ? "" : data.icon ?? "";
70+
}
71+
72+
// produce new html
73+
$el.html(icon + " " + label);
6574
}
6675

67-
// produce new html
68-
$el.html(icon + " " + label);
76+
if (hasDefinedProperty(data, "disabled")) {
77+
if (data.disabled) {
78+
$el.attr("disabled", "");
79+
} else {
80+
$el.attr("disabled", null);
81+
}
82+
}
6983
}
7084

7185
unsubscribe(el: HTMLElement): void {

srcts/types/src/bindings/input/actionbutton.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { InputBinding } from "./inputBinding";
22
type ActionButtonReceiveMessageData = {
33
label?: string;
44
icon?: string | [];
5+
disabled?: boolean;
56
};
67
declare class ActionButtonInputBinding extends InputBinding {
78
find(scope: HTMLElement): JQuery<HTMLElement>;

0 commit comments

Comments
 (0)