在 confirmSweetAlert 中禁用确认按钮

Disabling Confirm Button in confirmSweetAlert

我正在尝试禁用 confirmSweetAlert 中的确认按钮,除非 selectizeInput 中有一些输入。 Javascript似乎有解决方案,例如swal.disableConfirmButton()document.getElementsByClassName().disabled = true,但是当我运行它们在shinyjs::runjs下时,这些似乎不起作用.是否有解决此问题的解决方案?这是我的示例代码:

shinyApp(
  ui <- fluidPage(
    actionButton("button", "Show Sweet Alert!")
  ),

  server <- function(input, output, session) {
    observeEvent(input$button, {
      confirmSweetAlert(
        session = session,
        inputId = "letterSelect",
        title = "Select a Letter!",
        type = "info",
        text = tags$div(
          h4("Please select from the options below then press 'Confirm'.", align = "center"),
          selectizeInput(
            inputId = "letters",
            label = NULL,
            choices = c("A", "B", "C"),
            options = list(placeholder = "None selected."),
            multiple = TRUE,
            width = '100%')
        ),
        closeOnClickOutside = FALSE
      )      
    })
  }

)

这似乎有效:

library(shiny)
library(shinyWidgets)
library(shinyjs)

shinyApp(
  ui <- fluidPage(
    useShinyjs(),
    actionButton("button", "Show Sweet Alert!")
  ),

  server <- function(input, output, session) {
    observeEvent(input$button, {
      confirmSweetAlert(
        session = session,
        inputId = "letterSelect",
        title = "Select a Letter!",
        type = "info",
        text = tags$div(
          h4("Please select from the options below then press 'Confirm'.", align = "center"),
          selectizeInput(
            inputId = "letters",
            label = NULL,
            choices = c("A", "B", "C"),
            options = list(placeholder = "None selected."),
            multiple = TRUE,
            width = '100%')
        ),
        closeOnClickOutside = FALSE
      )
      runjs("Swal.getConfirmButton().setAttribute('disabled', '');")
    })

    observe({
      if(is.null(input$letters)){
        runjs("Swal.getConfirmButton().setAttribute('disabled', '');")
      }else{
        runjs("Swal.getConfirmButton().removeAttribute('disabled');")
      }
    })
  }

)