dreamRs / shinyWidgets

shinyWidgets : Extend widgets available in shiny
https://dreamrs.github.io/shinyWidgets/
GNU General Public License v3.0
833 stars 153 forks source link

updateSearchInput() doesn't work with trigger = TRUE when btnSearch = NULL and btnReset = NULL in searchInput() #619

Closed ZekeMarshall closed 1 year ago

ZekeMarshall commented 1 year ago

Hi @pvictor ,

I'm trying to update the value of one searchInput() based on another searchInput() with trigger = TRUE, similarly to the excellent example provided by @cvmartin here https://github.com/dreamRs/shinyWidgets/issues/133.

cvmartin's reprex works as expected, but only when the arguments btnSearch = icon("search") and btnReset = icon("remove") are included.

See an adjusted reprex below.

# The module 1 --------------------------------------------------------------
module1SearchInput <- function(id, title) {
  ns <- NS(id)

  div(style = "padding:10px; margin:10px; border-style:solid; border-width:2px;",
      tags$h2(title),
      searchInput(
        inputId = ns("search"), label = "Enter your text",
        placeholder = "A placeholder",
        btnSearch = icon("search"),
        btnReset = icon("remove"),
        width = "450px"
      ),
      br(),
      verbatimTextOutput(outputId = ns("res")),
      br(),
      textInput(
        inputId = ns("update_search"),
        label = "Update search"
      ),
      checkboxInput(
        inputId = ns("trigger_search"),
        label = "Trigger update search",
        value = TRUE
      )
  )
}

module1Search <- function(input, output, session) {
  output$res <- renderPrint({
    input$search
  })

  observeEvent(input$update_search, {
    print(input$update_search)

    updateSearchInput(
      session = session,
      inputId = "search",
      value = input$update_search,
      trigger = input$trigger_search
    )
  }, ignoreInit = TRUE)
}

# The module 2 --------------------------------------------------------------
module2SearchInput <- function(id, title) {
  ns <- NS(id)

  div(style = "padding:10px; margin:10px; border-style:solid; border-width:2px;",
      tags$h2(title),
      searchInput(
        inputId = ns("search"), label = "Enter your text",
        placeholder = "A placeholder",
        btnSearch = NULL,
        btnReset = NULL,
        width = "450px"
      ),
      br(),
      verbatimTextOutput(outputId = ns("res")),
      br(),
      textInput(
        inputId = ns("update_search"),
        label = "Update search"
      ),
      checkboxInput(
        inputId = ns("trigger_search"),
        label = "Trigger update search",
        value = TRUE
      )
  )
}

module2Search <- function(input, output, session) {
  output$res <- renderPrint({
    input$search
  })

  observeEvent(input$update_search, {
    print(input$update_search)

    updateSearchInput(
      session = session,
      inputId = "search",
      value = input$update_search,
      trigger = input$trigger_search
    )
  }, ignoreInit = TRUE)
}

# The app -----------------------------------------------------------------
library(shiny)
library(shinyWidgets)

ui <- fluidPage(
  module1SearchInput("module1", "Successfully triggers update server-side"),
  module2SearchInput("module2", "Does not trigger update server-side")
)

server <- function(input, output, session) {
  callModule(module1Search, "module1")
  callModule(module2Search, "module2")
}

shinyApp(ui, server)

I was wondering whether there is a way to make trigger = TRUE work when btnSearch = NULL and btnReset = NULL?

Any help would be greatly appreciated!

Best regards,

Zeke

pvictor commented 1 year ago

Hello, Try installing package from GitHub, this should have been fixed (similar to https://github.com/dreamRs/shinyWidgets/issues/607).

Victor

ZekeMarshall commented 1 year ago

Hi @pvictor,

Thanks for the tip!

I can confirm that moving from {shinyWidgets} v0.7.6 installed from CRAN, to v0.7.6.9850 installed from dreamRs/shinyWidgets@master fixes this problem!

Thanks again!

Zeke