Skip to content

Commit

Permalink
Disable buttons instead of hiding
Browse files Browse the repository at this point in the history
Gives a more consistent user-intuitive look.
  • Loading branch information
JasperSch committed Jan 30, 2025
1 parent 67bcc18 commit 2621f5e
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 51 deletions.
98 changes: 67 additions & 31 deletions editbl/R/eDT.R
Original file line number Diff line number Diff line change
Expand Up @@ -1320,14 +1320,17 @@ addButtons <- function(
df
}


#' Helper function to write HTML
#' @details generate HTML as character once and reuse.
#' Since buttons have to be generated a lot, this otherwhise slows down the app.
#' @param suffix `character(1)` sprintf placeholer for suffix
#' @param ns `character(1)` sprintf placeholder for ns
#' @inheritParams createDeleteButtonHTML
#' @details only to be used interactively. sprintf() implementation
#' is faster.
#' @importFrom shiny div actionButton icon
#' @return `character(1)` HTML to be filled in with \code{sprintf}
createDeleteButtonHTML <- function(ns = "%1$s", suffix = "%2$s"){
#' @seealso createEditButtonHTML
createDeleteButtonHTML_shiny <- function(
ns = "%1$s",
suffix = "%2$s",
disabled = FALSE){
as.character(
actionButton(
inputId = sprintf("%1$sdelete_row_%2$s", ns,suffix),
Expand All @@ -1343,18 +1346,22 @@ createDeleteButtonHTML <- function(ns = "%1$s", suffix = "%2$s"){
)
}


#' Helper function to write HTML
#' @details generate HTML as character once and reuse.
#' Since buttons have to be generated a lot, this otherwhise slows down the app.
#' @param suffix `character(1)` sprintf placeholer for suffix
#' @param ns `character(1)` sprintf placeholder for ns
#' @inheritParams createEditButtonHTML
#' @details only to be used interactively. sprintf() implementation
#' is faster.
#' @seealso createEditButtonHTML
#' @importFrom shiny div actionButton icon
#' @return `character(1)` HTML to be filled in with \code{sprintf}
createEditButtonHTML <- function(ns = "%1$s", suffix = "%2$s"){
createEditButtonHTML_shiny <- function(
ns = "%1$s",
suffix = "%2$s",
disabled = FALSE){
as.character(
actionButton(
inputId = sprintf("%1$sedit_row_%2$s", ns,suffix),
label = "",
disabled = disabled,
icon = icon("pen-to-square"),
style = "background-color: white",
onclick = HTML(sprintf("get_id(this.id, '%1$s');
Expand All @@ -1365,9 +1372,45 @@ createEditButtonHTML <- function(ns = "%1$s", suffix = "%2$s"){
)
}

deleteButtonHTML <- createDeleteButtonHTML()
#' Generate HTML for an in-row edit button
#' @param suffix `character(1)` id of the row
#' @param ns `character(1)` namespace
#' @param disabled `logical(1)` wether or not the button has to be disabled
#' @return `character(1)` HTML
createEditButtonHTML <- function(
ns,
suffix,
disabled = FALSE
){
if(disabled){
disabled_str = 'disabled'
} else {
disabled_str = ''
}
sprintf(r"(<button id="%1$sedit_row_%2$s" type="button" class="btn btn-default action-button" %3$s style="background-color: white" onclick="get_id(this.id, &#39;%1$s&#39;);&#10; Shiny.setInputValue(&quot;%1$sedit&quot;, Math.random(), {priority: &quot;event&quot;});">
<i class="far fa-pen-to-square" role="presentation" aria-label="pen-to-square icon"></i>
</button>)", ns, suffix, disabled_str)
}
#' Generate HTML for an in-row delete button
#' @param suffix `character(1)` id of the row
#' @param ns `character(1)` namespace
#' @param disabled `logical(1)` wether or not the button has to be disabled
#' @return `character(1)` HTML
createDeleteButtonHTML <- function(
ns = "%1$s",
suffix = "%2$s",
disabled=FALSE){
if(disabled){
disabled_str = 'disabled'
} else {
disabled_str = ''
}
sprintf(r"(<button id="%1$sdelete_row_%2$s" type="button" class="btn btn-default action-button" %3$s style="color: red;background-color: white" onclick="get_id(this.id, &#39;%1$s&#39;);&#10; Shiny.setInputValue(&quot;%1$sdelete&quot;, Math.random(), {priority: &quot;event&quot;});">
<i class="fas fa-trash" role="presentation" aria-label="trash icon"></i>
</button>)", ns, suffix, disabled_str)
}
editButtonHTML <- createEditButtonHTML()
#' Re-usable documentation
#' @param canEditRow can be either of the following:
Expand Down Expand Up @@ -1397,27 +1440,20 @@ createButtons <- function(
canDeleteRow = TRUE,
statusCol = 'status'
){
if(evalCanDeleteRow(row=row, canDeleteRow=canDeleteRow, statusCol=statusCol)){
deleteButton <- deleteButtonHTML
} else {
deleteButton <- ""
}
if(evalCanEditRow(row=row, canEditRow=canEditRow, statusCol=statusCol)){
editButton <- editButtonHTML
} else {
editButton <- ""
}

deleteButton <- createDeleteButtonHTML(
ns=ns,
suffix=suffix,
disabled = !evalCanDeleteRow(row=row, canDeleteRow=canDeleteRow, statusCol=statusCol))
editButton <- createEditButtonHTML(
ns=ns,
suffix=suffix,
disabled = !evalCanEditRow(row=row, canEditRow=canEditRow, statusCol=statusCol))
result <- sprintf('<div class="btn-group">%1$s%2$s</div>',
deleteButton,
editButton
)
# Conditional since sprintf() throws warning when no placeholders are available
if(editButton != '' && deleteButton != ''){
result <- sprintf(result,ns, suffix)
}

result
}
Expand All @@ -1431,7 +1467,7 @@ disableDoubleClickButtonCss <- function(id){
#%1$s > .dataTables_wrapper > table tbody td:nth-child(1) {pointer-events: none;}
#%1$s > .dataTables_wrapper > table tbody td:nth-child(1)>div {pointer-events: auto;}
",id)
}
}
keyTableJS <- c(
# Trigger doubleclick by enter
Expand Down
18 changes: 8 additions & 10 deletions editbl/man/createDeleteButtonHTML.Rd

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

25 changes: 25 additions & 0 deletions editbl/man/createDeleteButtonHTML_shiny.Rd

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

18 changes: 8 additions & 10 deletions editbl/man/createEditButtonHTML.Rd

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

25 changes: 25 additions & 0 deletions editbl/man/createEditButtonHTML_shiny.Rd

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

Binary file modified editbl/vignettes/screenshots/howto_row_level_access_1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 2621f5e

Please sign in to comment.