From 0f4bd9ae273693cc3e4191fdceaa178f872e45e6 Mon Sep 17 00:00:00 2001 From: pvictor Date: Fri, 7 Feb 2025 15:19:12 +0100 Subject: [PATCH] calendarPro: update examples --- R/calendar-pro-input.R | 19 ++++++-- examples/calendar-pro-update.R | 85 +++++++++++++++++++++++++++++++--- man/updateCalendarPro.Rd | 85 +++++++++++++++++++++++++++++++--- 3 files changed, 172 insertions(+), 17 deletions(-) diff --git a/R/calendar-pro-input.R b/R/calendar-pro-input.R index 9881158a..1ffe5c6c 100644 --- a/R/calendar-pro-input.R +++ b/R/calendar-pro-input.R @@ -218,13 +218,22 @@ updateCalendarPro <- function(inputId, session = shiny::getDefaultReactiveDomain()) { if (!is.null(label)) label <- doRenderTags(label) + options <- list( + selectedDates = list1(format(value, format = "%Y-%m-%d")), + selectionDatesMode = mode, + ... + ) + if (inherits(value, "POSIXt") & is.null(options$selectedTime)) + options$selectedTime <- format(value, format = "%H:%M") + if (!is.null(options$disableWeekdays)) + options$disableWeekdays <- list1(options$disableWeekdays) + if (!is.null(options$enableDates)) + options$enableDates <- list1(options$enableDates) + if (!is.null(options$disableDates)) + options$disableDates <- list1(options$disableDates) message <- dropNulls(list( label = label, - options = dropNulls(list( - selectedDates = list1(format(value, format = "%Y-%m-%d")), - selectionDatesMode = mode, - ... - )) + options = dropNulls(options) )) session$sendInputMessage(inputId, message) } diff --git a/examples/calendar-pro-update.R b/examples/calendar-pro-update.R index 4b7d26db..5b567f41 100644 --- a/examples/calendar-pro-update.R +++ b/examples/calendar-pro-update.R @@ -7,7 +7,7 @@ ui <- fluidPage( tags$h2("Calendar Pro Input: update from server"), fluidRow( column( - width = 6, + width = 4, calendarProInput( inputId = "calendar", label = "Select a date:", @@ -35,7 +35,48 @@ ui <- fluidPage( ) ), column( - width = 6 + width = 4, + calendarProInput( + inputId = "time", + label = "Select date and time:", + placeholder = "Select date and time:", + selectionTimeMode = 24, + format = "%Y/%m/%d %H:%M", + width = "100%" + ), + verbatimTextOutput("res2"), + actionButton( + inputId = "set_time_1", + label = "Set yesterday 11am" + ), + actionButton( + inputId = "set_time_2", + label = "Set now" + ) + ), + column( + width = 4, + calendarProInput( + inputId = "disable", + label = "Select a date:", + placeholder = "Select a date", + width = "100%" + ), + verbatimTextOutput("res3"), + radioButtons( + inputId = "disable_fridays", + label = "Disable fridays:", + choices = c("yes", "no"), + selected = "no", + inline = TRUE + ), + radioButtons( + inputId = "disable_tomorrow", + label = "Disable tomorrow:", + choices = c("yes", "no"), + selected = "no", + inline = TRUE + ), ) ) ) @@ -46,20 +87,52 @@ server <- function(input, output, session) { observeEvent(input$label, { if (isTruthy(input$label)) { - updateCalendarPro(inputId = "calendar", label = input$label) + updateCalendarPro("calendar", label = input$label) } }) observeEvent(input$today, { - updateCalendarPro(inputId = "calendar", value = Sys.Date()) + updateCalendarPro("calendar", value = Sys.Date()) }) observeEvent(input$today3, { - updateCalendarPro(inputId = "calendar", value = Sys.Date() + 3) + updateCalendarPro("calendar", value = Sys.Date() + 3) }) observeEvent(input$mode, { - updateCalendarPro(inputId = "calendar", selectionDatesMode = input$mode) + updateCalendarPro("calendar", selectionDatesMode = input$mode) + }, ignoreInit = TRUE) + + + + output$res2 <- renderPrint(input$time) + + observeEvent(input$set_time_1, { + updateCalendarPro("time", value = Sys.Date() - 1, selectedTime = "11:00") + }) + + observeEvent(input$set_time_2, { + updateCalendarPro("time", value = Sys.time()) + }) + + + + output$res3 <- renderPrint(input$disable) + + observeEvent(input$disable_fridays, { + if (input$disable_fridays == "yes") { + updateCalendarPro("disable", disableWeekdays = 5) + } else { + updateCalendarPro("disable", disableWeekdays = numeric(0)) + } + }, ignoreInit = TRUE) + + observeEvent(input$disable_tomorrow, { + if (input$disable_tomorrow == "yes") { + updateCalendarPro("disable", disableDates = Sys.Date() + 1) + } else { + updateCalendarPro("disable", disableDates = numeric(0)) + } }, ignoreInit = TRUE) } diff --git a/man/updateCalendarPro.Rd b/man/updateCalendarPro.Rd index 2b369f24..cae35c78 100644 --- a/man/updateCalendarPro.Rd +++ b/man/updateCalendarPro.Rd @@ -44,7 +44,7 @@ ui <- fluidPage( tags$h2("Calendar Pro Input: update from server"), fluidRow( column( - width = 6, + width = 4, calendarProInput( inputId = "calendar", label = "Select a date:", @@ -72,7 +72,48 @@ ui <- fluidPage( ) ), column( - width = 6 + width = 4, + calendarProInput( + inputId = "time", + label = "Select date and time:", + placeholder = "Select date and time:", + selectionTimeMode = 24, + format = "\%Y/\%m/\%d \%H:\%M", + width = "100\%" + ), + verbatimTextOutput("res2"), + actionButton( + inputId = "set_time_1", + label = "Set yesterday 11am" + ), + actionButton( + inputId = "set_time_2", + label = "Set now" + ) + ), + column( + width = 4, + calendarProInput( + inputId = "disable", + label = "Select a date:", + placeholder = "Select a date", + width = "100\%" + ), + verbatimTextOutput("res3"), + radioButtons( + inputId = "disable_fridays", + label = "Disable fridays:", + choices = c("yes", "no"), + selected = "no", + inline = TRUE + ), + radioButtons( + inputId = "disable_tomorrow", + label = "Disable tomorrow:", + choices = c("yes", "no"), + selected = "no", + inline = TRUE + ), ) ) ) @@ -83,20 +124,52 @@ server <- function(input, output, session) { observeEvent(input$label, { if (isTruthy(input$label)) { - updateCalendarPro(inputId = "calendar", label = input$label) + updateCalendarPro("calendar", label = input$label) } }) observeEvent(input$today, { - updateCalendarPro(inputId = "calendar", value = Sys.Date()) + updateCalendarPro("calendar", value = Sys.Date()) }) observeEvent(input$today3, { - updateCalendarPro(inputId = "calendar", value = Sys.Date() + 3) + updateCalendarPro("calendar", value = Sys.Date() + 3) }) observeEvent(input$mode, { - updateCalendarPro(inputId = "calendar", selectionDatesMode = input$mode) + updateCalendarPro("calendar", selectionDatesMode = input$mode) + }, ignoreInit = TRUE) + + + + output$res2 <- renderPrint(input$time) + + observeEvent(input$set_time_1, { + updateCalendarPro("time", value = Sys.Date() - 1, selectedTime = "11:00") + }) + + observeEvent(input$set_time_2, { + updateCalendarPro("time", value = Sys.time()) + }) + + + + output$res3 <- renderPrint(input$disable) + + observeEvent(input$disable_fridays, { + if (input$disable_fridays == "yes") { + updateCalendarPro("disable", disableWeekdays = 5) + } else { + updateCalendarPro("disable", disableWeekdays = numeric(0)) + } + }, ignoreInit = TRUE) + + observeEvent(input$disable_tomorrow, { + if (input$disable_tomorrow == "yes") { + updateCalendarPro("disable", disableDates = Sys.Date() + 1) + } else { + updateCalendarPro("disable", disableDates = numeric(0)) + } }, ignoreInit = TRUE) }