选择导航菜单中的选项卡会切换下拉菜单。

huangapple go评论59阅读模式
英文:

Selecting a tab inside nav_menu toggles the dropdown

问题

我对bslib中的nav_menunav_select有一个问题。

当我尝试选择嵌套在nav_menu内的选项卡时,页面会导航到新选项卡,但它还会打开nav_menu的下拉列表,就好像被点击了一样,而不是从服务器中选择。然后,下拉列表不会关闭,直到再次点击导航菜单按钮。在页面的其他地方点击不会切换它。

有人见过这个问题吗?这是否是预期行为?有没有解决方法?谢谢!

一个可重现的示例:

library(shiny)
library(bslib)
library(rlang)

nav_items <- function(prefix) {
  list(
    nav("a", tagList(
      paste(prefix, ": tab a content"),
      actionButton(inputId = "go_to_tab_c", label = "Go to tab C")
    )),
    nav("b", paste(prefix, ": tab b content")),
    nav_item(
      tags$a(icon("github"), "Shiny", href = "https://github.com/rstudio/shiny", target = "_blank")
    ),
    nav_spacer(),
    nav_menu(
      value = "ya",
      title = "Other links",
      align = "right",
      nav("c", paste(prefix, ": tab c content")),
      nav_item(
        tags$a(
          icon("r-project"),
          "RStudio",
          href = "https://rstudio.com",
          target = "_blank"
        )
      )
    )
  )
}

ui <- page_fluid(
  shinyjs::useShinyjs(),
  page_navbar(
    id = "main_nav",
    title = "Page",
    bg = "#0062cc",
    !!!nav_items(prefix = "page_navbar")
  )
)

server <- function(input, output, session) {
  observeEvent(input$go_to_tab_c, {
    nav_select(id = "main_nav", selected = "c")
  })

}

shinyApp(ui, server)
英文:

I have a question about nav_menu and nav_select from bslib.

When I try to select a tab that is nested inside a nav_menu, the page
navigates to the new tab, but it also opens the nav_menu dropdown list as it
were clicked, instead of selected from the server. Then, the dropdown does not
close until the nav menu button is clicked again. Clicking elsewhere on the page
does not toggle it.

Has anyone seen this? Is it expected behavior? Any ways around it? Thanks!

A reprex:

library(shiny)
library(bslib)
library(rlang)

nav_items &lt;- function(prefix) {
  list(
    nav(&quot;a&quot;, tagList(
      paste(prefix, &quot;: tab a content&quot;),
      actionButton(inputId = &quot;go_to_tab_c&quot;, label = &quot;Go to tab C&quot;)
    )),
    nav(&quot;b&quot;, paste(prefix, &quot;: tab b content&quot;)),
    nav_item(
      tags$a(icon(&quot;github&quot;), &quot;Shiny&quot;, href = &quot;https://github.com/rstudio/shiny&quot;, target = &quot;_blank&quot;)
    ),
    nav_spacer(),
    nav_menu(
      value = &quot;ya&quot;,
      title = &quot;Other links&quot;,
      align = &quot;right&quot;,
      nav(&quot;c&quot;, paste(prefix, &quot;: tab c content&quot;)),
      nav_item(
        tags$a(
          icon(&quot;r-project&quot;),
          &quot;RStudio&quot;,
          href = &quot;https://rstudio.com&quot;,
          target = &quot;_blank&quot;
        )
      )
    )
  )
}

ui &lt;- page_fluid(
  shinyjs::useShinyjs(),
  page_navbar(
    id = &quot;main_nav&quot;,
    title = &quot;Page&quot;,
    bg = &quot;#0062cc&quot;,
    !!!nav_items(prefix = &quot;page_navbar&quot;)
  )
)

server &lt;- function(input, output, session) {
  observeEvent(input$go_to_tab_c, {
    nav_select(id = &quot;main_nav&quot;, selected = &quot;c&quot;)
  })

}

shinyApp(ui, server)

答案1

得分: 1

请阅读bslib存储库上的此线程,了解发生这种情况的根本原因。简而言之,这是由于Bootstrap 5的更改传播到bslib,然后传播到shiny引起的。该线程还包括一些示例代码,说明如何使用来自shiny服务器的一些JS关闭nav_menu

英文:

For anyone arriving here, please read through this thread on the bslib repo about the underlying reason why this happens. In short, it's a change in Bootstrap 5 that propagates through bslib and then shiny. The thread also includes some example code how to close the nav_menu using some JS from the shiny server.

huangapple
  • 本文由 发表于 2023年6月6日 01:05:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/76408603.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定