Skip to content

Commit 58891e6

Browse files
committed
Improve custom click action example; add warning about accessibility
1 parent a7b126b commit 58891e6

File tree

1 file changed

+42
-10
lines changed

1 file changed

+42
-10
lines changed

vignettes/examples.Rmd

Lines changed: 42 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1273,11 +1273,12 @@ reactable(iris[1:5, ], rownames = TRUE)
12731273

12741274

12751275
## Cell Click Actions
1276-
You can add cell click actions using `onClick`, which accepts the following values:
1276+
You can add cell click actions using the `onClick` argument, which accepts the
1277+
following values:
12771278

12781279
- `"expand"` to expand the row
12791280
- `"select"` to select the row
1280-
- a JavaScript function for a custom action, e.g., sending the click event to Shiny
1281+
- A JavaScript function for a custom action, e.g., sending the click event to Shiny
12811282

12821283
### Expand on click
12831284
```{r}
@@ -1297,20 +1298,51 @@ reactable(iris[1:5, ], selection = "multiple", onClick = "select")
12971298
```
12981299

12991300
### Custom action
1301+
1302+
This example uses a custom click action to create custom "show details" action
1303+
buttons in each row of the table:
1304+
13001305
```{r}
1301-
reactable(iris[1:5, ], onClick = JS("
1302-
function(rowInfo, colInfo, state) {
1303-
window.alert('clicked row ' + rowInfo.index + ', column ' + colInfo.id + ', on page ' + state.page)
1306+
data <- cbind(
1307+
MASS::Cars93[1:5, c("Manufacturer", "Model", "Type", "Price")],
1308+
details = NA
1309+
)
13041310
1305-
// Send the click event to Shiny, which will be available at input$clicked
1306-
// (Note that the row index starts at 0 in JavaScript, so we add 1)
1311+
reactable(
1312+
data,
1313+
columns = list(
1314+
# Render a "show details" button in the last column of the table.
1315+
# This button won't do anything by itself, but will trigger the custom
1316+
# click action on the column.
1317+
details = colDef(
1318+
name = "",
1319+
sortable = FALSE,
1320+
cell = function() htmltools::tags$button("Show details")
1321+
)
1322+
),
1323+
onClick = JS("function(rowInfo, colInfo) {
1324+
// Only handle click events on the 'details' column
1325+
if (colInfo.id !== 'details') {
1326+
return
1327+
}
1328+
1329+
// Display an alert dialog with details for the row
1330+
window.alert('Details for row ' + rowInfo.index + ':\\n' + JSON.stringify(rowInfo.row, null, 2))
1331+
1332+
// Send the click event to Shiny, which will be available in input$show_details
1333+
// Note that the row index starts at 0 in JavaScript, so we add 1
13071334
if (window.Shiny) {
1308-
Shiny.onInputChange('clicked', { column: colInfo.id, index: rowInfo.index + 1 })
1335+
Shiny.setInputValue('show_details', { index: rowInfo.index + 1 })
13091336
}
1310-
}
1311-
"))
1337+
}")
1338+
)
13121339
```
13131340

1341+
> **WARNING:** Custom click actions are currently not accessible to keyboard
1342+
> users, and are generally not recommended. If they must be used, ensure that
1343+
> they can be triggered by a keyboard through other means, such as a button
1344+
> in the example above.
1345+
13141346

13151347
## Language Options
13161348

0 commit comments

Comments
 (0)