daattali / shinyalert

🗯️ Easily create pretty popup messages (modals) in Shiny
https://daattali.com/shiny/shinyalert-demo/
Other
241 stars 26 forks source link

shinyalert does not reset on close #81

Closed nick-youngblut closed 6 months ago

nick-youngblut commented 8 months ago

I have a problem similar to https://github.com/daattali/shinyalert/issues/60.

My code:

observeEvent(input$submit, {  # action button
    # validate input
    msg = validate_samples_table(
      DF = DF,
      input = input
    )

  # show pop-up
  if(msg$type %in% c("success", "warning")){
      # Success/Warning
      shinyalert::shinyalert(
        title = msg$header,
        text = paste(msg$text, "Are you ready to submit?"),
        type = "info",
        html = TRUE,
        showCancelButton = TRUE,   
        confirmButtonText = "Yes!",  
        cancelButtonText = "Not yet...",
        inputId = "shinyalert_validation" 
      )
    } else {
      # if error message, show error
      shinyalert::shinyalert(msg$header, msg$text, type=msg$type)
      return(NULL)
    }
}

If the user clicks the "Yes!" button in the pop-up, and then re-clicks the "submit" action button (which should trigger the validation and pop-up again), the pop-up is skipped, and no validation occurs.

I've tried using shinyjs::reset("shinyalert_validation"), but it does not reset the shinyalert_validation input.

Unlike https://github.com/daattali/shinyalert/issues/60, the shinyalert is not wrapped in renderUI().

daattali commented 6 months ago

Could you please provide a small reprex? I tried taking your code and making a small app from it, and it does run successfully.

library(shinyalert)
library(shiny)

ui <- fluidPage(
actionButton('submit', 'submit')  
)

server <- function(input, output, session) {
  observeEvent(input$submit, {
    shinyalert::shinyalert(
      text = "Are you ready to submit?",
      type = "info",
      html = TRUE,
      showCancelButton = TRUE,   
      confirmButtonText = "Yes!",  
      cancelButtonText = "Not yet...",
      inputId = "shinyalert_validation" 
    )
  })
}

shinyApp(ui, server)
nick-youngblut commented 6 months ago

I fixed the issue by using a reactive object with the callbackR parameter:

shinyalert_validation_react = reactiveVal(NULL)
observeEvent(input$submit, {  # action button
    # validate input
    msg = validate_samples_table(
      DF = DF,
      input = input
    )

  # show pop-up
  if(msg$type %in% c("success", "warning")){
      # Success/Warning
      shinyalert::shinyalert(
        title = msg$header,
        text = paste(msg$text, "Are you ready to submit?"),
        type = "info",
        html = TRUE,
        showCancelButton = TRUE,   
        confirmButtonText = "Yes!",  
        cancelButtonText = "Not yet...",
        inputId = "shinyalert_validation" 
        callbackR = \(x) shinyalert_validation_react(x)
      )
    } else {
      # if error message, show error
      shinyalert::shinyalert(msg$header, msg$text, type=msg$type)
      return(NULL)
    }
}

observer ({
   req(shinyalert_validation_react())
   # complete next step only if user presses the `Yes!` button
})
daattali commented 6 months ago

I'm glad you found a fix! But if you still think there is a bug, please do share a reproducible example so that I can look into it.

nick-youngblut commented 6 months ago

Thanks for following up on this! I don't think that it is a bug, but maybe more docs/examples on using the callbackR parameter could be useful.