Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

selection = "single" should ensure one row is always selected #395

Open
johann-wagner opened this issue Oct 15, 2024 · 2 comments
Open

selection = "single" should ensure one row is always selected #395

johann-wagner opened this issue Oct 15, 2024 · 2 comments
Labels
doc Documentation issue or suggestion enhancement New feature or request

Comments

@johann-wagner
Copy link

Hi,

Issue:
I'm using the row selection feature in reactable and setting selection = "single". The intention of this feature seems to function similarly to radio buttons. However, I'm finding that I can deselect the currently selected row, resulting in no rows selected at all.

Solution?
This behaviour seems counterintuitive for single selection mode, so I'm curious whether this is on purpose or indeed a bug?
Is it possible to add this feature (or perhaps a toggle) to ensure that a row must be selected at all times?

Reprex:

library(shiny)
library(reactable)

ui <- fluidPage(
    reactableOutput("car_table")
)

server <- function(input, output) {
  output$car_table <- renderReactable({
    reactable(
      mtcars,
      selection = "single",
      onClick = "select",
      defaultSelected = c(1)
    )
  })
}

shinyApp(ui = ui, server = server)

Video highlighting issue:
https://github.com/user-attachments/assets/81cdd052-1379-44cd-85fa-6612b40dc444

Thanks! :)

@glin glin added enhancement New feature or request doc Documentation issue or suggestion labels Nov 6, 2024
@glin
Copy link
Owner

glin commented Nov 6, 2024

Hi, this is intentional, and honestly I've always felt that radio buttons not allowing deselection in general was odd. Maybe it differs based on whether there's a default selected value. Like if there must always be a selection, then yeah, deselection makes a lot less sense. But for most radio buttons where you start out with an unselected value, then I never understood why we couldn't deselect and return to the initial state, outside of refreshing the page to reset the form completely.

An option to disable deselection makes sense to me, and we'd just have to figure out the best way to put it in the API. For now, you can disable deselect on the Shiny server side, by watching for NULL selections and force-reselecting the previously selected row:

library(shiny)
library(reactable)

ui <- fluidPage(
  reactableOutput("car_table")
)

server <- function(input, output) {
  output$car_table <- renderReactable({
    reactable(
      mtcars,
      selection = "single",
      onClick = "select",
      defaultSelected = c(1)
    )
  })

  # You would use rv$selected to get the selected row, instead of getReactableState("car_table", "selected")
  rv <- reactiveValues(
    selected = NULL
  )

  observe({
    selected <- getReactableState("car_table", "selected")
    if (!is.null(selected)) {
      rv$selected <- selected
    } else {
      updateReactable("car_table", selected = rv$selected)
    }
  })
}

shinyApp(ui = ui, server = server)

Also worth noting is that other big data table libraries all support single row deselection by default, DataTables, ag-Grid, MUI. So it's not totally out of the ordinary to at least have this behavior by default.

@johann-wagner
Copy link
Author

Thanks @glin for the response, this seems very reasonable and appreciate your workaround solution! Yes, agreed having an option to disable deselect is probably a good approach. For the meantime, this workaround is great! Cheers!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
doc Documentation issue or suggestion enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants