I want to change background color of tabs based on a list. It should also change whenever a tab is clicked. So far, I do this by addClass
, which works for clicking only. How can I change color, before a Tab is clicked (based on this magic list):
tab_UI <- function(id) {
ns <- NS(id)
shiny::tagList(
shinyjs::useShinyjs(),
shinyjs::inlineCSS('.selct {background: green; color: white;border: 5px solid black;}'),
shiny::tabsetPanel(id = shiny::NS(id,"tabs"))
)
}
tab_server <- function(id,inTab) {
shiny::moduleServer(id, function(input, output, session){
confirmedTabs = reactiveVal()
observeEvent(inTab(),{
l = c()
for (i_tab in isolate(inTab())) {
message("append ",i_tab$name)
shiny::appendTab(
inputId = "tabs",
select = FALSE,
shiny::tabPanel(
title=i_tab$name,
"TestTab"
)
)
if(i_tab$confirmed == TRUE) {
l = append(l,i_tab$name)
}
}
confirmedTabs(l)
})
markConfirmed = function(tab) {
message("color tab: ", tab)
s = paste0(" li a[data-value=",tab,"]")
shinyjs::addClass(
selector = s,
class = "selct")
}
observeEvent(confirmedTabs(),{
for (i in confirmedTabs()) {
markConfirmed(i)
}
})
shiny::observeEvent(input$tabs,{
if(!input$tabs %in% confirmedTabs()) {
ct = confirmedTabs()
ct = append(ct,input$tabs)
confirmedTabs(ct)
}
})
})
}
shiny::shinyApp(
ui = shiny::fluidPage(
shinyjs::useShinyjs(),
tab_UI(id = "test")
),
server = function(input, output, session) {
tabs = reactiveVal()
df = list(A1= list(name = "A1", confirmed = TRUE),
A2 = list(name = "A2", confirmed = FALSE),
A3 = list(name = "A3", confirmed = TRUE))
tabs(df)
tab_server(
id = "test",
inTab = tabs
)
}
)