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

selectizeInput()'s input value can be incorrect after updateSelectizeInput() #3925

Closed
cpsievert opened this issue Oct 26, 2023 · 0 comments · Fixed by #3926
Closed

selectizeInput()'s input value can be incorrect after updateSelectizeInput() #3925

cpsievert opened this issue Oct 26, 2023 · 0 comments · Fixed by #3926

Comments

@cpsievert
Copy link
Collaborator

cpsievert commented Oct 26, 2023

Introduced by #3875 and discovered via this test app. Below is a minimal reprex of the problem we were seeing there.

To reproduce, run the app.R below. Once open, remove "A" and "D" from the 1st selectize (choices). That causes input$selected to be NULL (when it should be "B"!).

If you then open the browser's devtools, and run the following JS $("select#selected").val(), you'll see the result is [], which is why the binding is reporting NULL. Interestingly, $("select#selected").selectize().getValue() returns the expect result (["B"]), so selectize.js must've recently moved away from using the DOM as the source of truth for the select's state.

app.R
library(shiny)

choices <- c("A", "B", "C", "D")

ui <- fluidPage(
  verbatimTextOutput("selected"),
  div(
    style = "display:flex;",
    selectInput(
      "choices", NULL, multiple = TRUE,
      choices = choices, selected = choices
    ),
    selectInput(
      "selected", NULL, multiple = TRUE, 
      choices, selected = c("A", "B", "D")
    )
  )
)

server <- function(input, output, session) {

  observeEvent(input$choices, ignoreInit = TRUE, {
    updateSelectInput(
      inputId = "selected", choices = input$choices,
      selected = input$selected
    )
  })

  output$selected <- renderPrint({
    paste("Value of the 2nd selectize (input$selected):", paste(input$selected, collapse = ", "))
  })

}

shinyApp(ui, server)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant