Skip to content

Commit

Permalink
fix(inputs): action or actions added to supported inputs (#166)
Browse files Browse the repository at this point in the history
  • Loading branch information
nteetor committed Nov 26, 2019
1 parent f9d6757 commit d7433d5
Show file tree
Hide file tree
Showing 27 changed files with 241 additions and 114 deletions.
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ Depends:
R (>= 3.2),
shiny (>= 1.1.0)
Imports:
htmltools (>= 0.3.6.9004),
magrittr,
utils
Remotes:
Expand Down
43 changes: 37 additions & 6 deletions R/actions.R
Original file line number Diff line number Diff line change
@@ -1,12 +1,43 @@
#' Input actions
#'
#' These functions are used in conjunction with yonder's reactive input functions'
#' `actions` argument.
#' @description
#'
#' @param id A character string specifying the id of a [navPane()] or
#' [collapsePane()].
#' Input actions are a new feature in yonder. These actions allow a reactive
#' input to interact with elements of your application's user interface without
#' requiring server-side logic. See below for components inputs may interact
#' with using input actions. Not all of yonder's reactive inputs may be used
#' with input actions.
#'
#' @section Supported inputs:
#'
#' * [buttonInput()]
#'
#' * [buttonGroupInput()]
#'
#' * [linkInput()]
#'
#' * [navInput()]
#'
#' @section Actionable components:
#'
#' **Nav panes**
#'
#' A nav pane may be shown with [showNavTarget()] or hidden with
#' [hideNavTarget()].
#'
#' **Collapse panes**
#'
#' A collapse pane may be opened with [showCollapseTarget()] or hidden with
#' [hideCollapseTarget()] or toggled with [toggleCollapseTarget()].
#'
#' Toggling a collapse pane will open the pane if closed or close the pane
#' if currently open.
#'
#' **Modals**
#'
#'
#' **Toasts**
#'
#' @param ... Additional arguments, currently ignored.
#'
#' @aliases action
#' @name actions
Expand Down Expand Up @@ -48,7 +79,7 @@ c.input_action <- function(...) {

as.list.input_action <- function(x) {
list(
`data-toggle` = x$plugin,
`data-plugin` = x$plugin,
`data-target` = x$target,
`data-action` = x$action
)
Expand Down
14 changes: 7 additions & 7 deletions R/button.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
#' @param tooltip A call to [tooltip()] specifying a tooltip for the button or
#' link input, defaults to `NULL`.
#'
#' @param actions An input [action] or `NULL`.
#' @param action An input [action] or `NULL`.
#'
#' @param ... Additional named arguments passed as HTML attributes to the parent
#' element.
Expand Down Expand Up @@ -127,10 +127,10 @@
#' )
#'
buttonInput <- function(id, label, ..., stretch = FALSE, download = FALSE,
tooltip = NULL, actions = NULL) {
tooltip = NULL, action = NULL) {
assert_id()
assert_label()
assert_actions(list = FALSE)
assert_action()

dep_attach({
component <- (if (download) tags$a else tags$button)(
Expand All @@ -145,7 +145,7 @@ buttonInput <- function(id, label, ..., stretch = FALSE, download = FALSE,
href = if (download) "",
`_target` = if (download) NA,
download = if (download) NA,
!!!actions,
!!!action,
id = id,
label,
...,
Expand Down Expand Up @@ -189,10 +189,10 @@ updateButtonInput <- function(id, label = NULL, value = NULL,
#' @rdname buttonInput
#' @export
linkInput <- function(id, label, ..., stretch = FALSE, download = FALSE,
tooltip = NULL, actions = NULL) {
tooltip = NULL, action = NULL) {
assert_id()
assert_label()
assert_actions(list = FALSE)
assert_action()

dep_attach({
component <- (if (download) tags$a else tags$button)(
Expand All @@ -206,7 +206,7 @@ linkInput <- function(id, label, ..., stretch = FALSE, download = FALSE,
href = if (download) "",
`_target` = if (download) NA,
download = if (download) NA,
!!!actions,
!!!action,
id = id,
label,
...
Expand Down
7 changes: 2 additions & 5 deletions R/checkbox.R
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
#' active("indigo")
#'
checkboxInput <- function(id, choices = NULL, values = choices, selected = NULL,
..., inline = FALSE, actions = NULL) {
..., inline = FALSE) {
assert_id()
assert_choices()
assert_actions()
Expand Down Expand Up @@ -169,20 +169,18 @@ updateSwitchInput <- function(id, choices = NULL, values = choices,
))
}

map_checkboxes <- function(choices, values, selected, inline, actions,
map_checkboxes <- function(choices, values, selected, inline,
switches = FALSE) {
if (is.null(choices) && is.null(values)) {
return(NULL)
}

selected <- values %in% selected
actions <- normalize_actions(actions, values)

Map(
choice = choices,
value = values,
select = selected,
action = actions,
last = seq_along(choices) == length(choices),
function(choice, value, select, action, last) {
id <- generate_id("checkbox")
Expand All @@ -199,7 +197,6 @@ map_checkboxes <- function(choices, values, selected, inline, actions,
id = id,
name = id,
value = value,
!!!action,
checked = if (select) NA,
autocomplete = "off"
),
Expand Down
25 changes: 16 additions & 9 deletions R/list-group.R
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
#' List group inputs
#'
#' List group inputs are an actionable list of items. They behave similarly to
#' checkboxes or radios, that is, users may select one or more items from the
#' list. However, list group items may include highly variable content.
#' radio buttons. However, list group items may include highly variable content.
#'
#' @inheritParams checkboxInput
#'
#' @inheritParams buttonGroupInput
#'
#' @param choices A vector of character strings or list of tag elements specifying
#' the content of the list group's items.
#'
#' @param values A character vector specifying the values of the list items,
#' defaults to `choices`.
#'
#' @param selected One or more of `values` specifying which choices are selected
#' by default, defaults to `NULL`, in which case no choice is selected.
#' @param selected One of `values` specifying which choices are selected by
#' default, defaults to `NULL`, in which case no choice is selected.
#'
#' @param layout A [responsive] argument. One of `"vertical"` or `"horizontal"`
#' specifying how list items are laid out, defaults to `"vertical"`. Note, if
Expand Down Expand Up @@ -108,10 +109,12 @@
#' )
#'
listGroupInput <- function(id, choices = NULL, values = choices,
selected = NULL, ..., layout = "vertical",
flush = FALSE) {
selected = NULL, ..., actions = NULL,
layout = "vertical", flush = FALSE) {
assert_id()
assert_choices()
assert_selected(length = 1)
assert_actions()
assert_possible(layout, c("vertical", "horizontal"))
assert_possible(flush, c(TRUE, FALSE))

Expand All @@ -122,7 +125,7 @@ listGroupInput <- function(id, choices = NULL, values = choices,
# drop vertical classes as they do not actually exist
classes <- classes[!grepl("vertical", classes, fixed = TRUE)]

items <- map_listitems(choices, values, selected)
items <- map_listitems(choices, values, selected, actions)

tags$div(
class = str_collate(
Expand All @@ -145,6 +148,7 @@ updateListGroupInput <- function(id, choices = NULL, values = choices,
session = getDefaultReactiveDomain()) {
assert_id()
assert_choices()
assert_selected(length = 1)
assert_session()

items <- map_listitems(choices, values, selected)
Expand All @@ -162,18 +166,20 @@ updateListGroupInput <- function(id, choices = NULL, values = choices,
))
}

map_listitems <- function(choices, values, selected) {
map_listitems <- function(choices, values, selected, actions) {
if (is.null(choices) && is.null(values)) {
return(NULL)
}

selected <- values %in% selected
actions <- normalize_actions(actions, values)

Map(
choice = choices,
value = values,
select = selected,
function(choice, value, select) {
action = actions,
function(choice, value, select, action) {
tags$button(
class = str_collate(
"list-group-item",
Expand All @@ -182,6 +188,7 @@ map_listitems <- function(choices, values, selected) {
if (select) "active"
),
value = value,
!!!action,
choice
)
}
Expand Down
17 changes: 12 additions & 5 deletions R/menu.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
#'
#' @inheritParams checkboxInput
#'
#' @inheritParams buttonGroupInput
#'
#' @param label A character string or tag element specifying the label of the
#' menu's toggle button.
#'
Expand Down Expand Up @@ -59,15 +61,17 @@
#' )
#'
menuInput <- function(id, label, choices = NULL, values = choices,
selected = NULL, ..., direction = "down",
align = "left") {
selected = NULL, ..., actions = NULL,
direction = "down", align = "left") {
assert_id()
assert_label()
assert_choices()
assert_actions()
assert_possible(direction, c("up", "right", "down", "left"))
assert_possible(align, c("right", "left"))

dep_attach({
items <- map_menuitems(choices, values, selected)
items <- map_menuitems(choices, values, selected, actions)

tags$div(
class = str_collate(
Expand Down Expand Up @@ -123,25 +127,28 @@ updateMenuInput <- function(id, label = NULL, choices = NULL, values = choices,
))
}

map_menuitems <- function(choices, values, selected) {
map_menuitems <- function(choices, values, selected, actions) {
if (is.null(choices) && is.null(values)) {
return(NULL)
}

selected <- values %in% selected
actions <- normalize_actions(actions, values)

Map(
choice = choices,
value = values,
select = selected,
function(choice, value, select) {
action = actions,
function(choice, value, select, action) {
tags$button(
class = str_collate(
"dropdown-item",
if (select) "active"
),
type = "button",
value = value,
!!!action,
choice
)
}
Expand Down
4 changes: 2 additions & 2 deletions R/nav.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
#'
#' @inheritParams checkboxInput
#'
#' @inheritParams buttonInput
#'
#' @param choices A character vector or list of tag elements specifying the
#' navigation items of the input.
#'
Expand All @@ -25,8 +27,6 @@
#' @param appearance One of `"links"`, `"pills"`, or `"tabs"` specifying the
#' appearance of the nav input, defaults to `"links"`.
#'
#' @param actions Any number of [actions], defaults to `NULL`.
#'
#' @section Including a menu:
#'
#' Use the reactive id of any nav menus to know when a menu item is clicked.
Expand Down
Loading

0 comments on commit d7433d5

Please sign in to comment.