javascriptrshiny

How to prevent the user from unselecting an option of a selectizeInput?


The default behavior for selectizeInput allows the user to click on a choice and delete it by hitting the Backspace or Delete key.

Is there any way of disabling that behavior?.

I have an app that allows users to add and remove groupings from the selectizeInput via 'add' and 'remove' action buttons. I don't want users to remove choices with the selectizeInput widget but rather by using the 'remove' action button.


Solution

  • Yes, use an onDelete event and return false:

    library(shiny)
    
    ui <- fluidPage(
      selectizeInput(
        inputId = 'selInput',
        label = 'No deletion',
        choices = state.name,
        multiple = TRUE,
        options = list(onDelete = I("function(value) {return false;}"))
      )
    )
    
    shinyApp(ui, \(...) {
    })