Skip to content

Commit

Permalink
air-datepicker tz + update
Browse files Browse the repository at this point in the history
  • Loading branch information
pvictor committed Aug 19, 2024
1 parent c94c298 commit ca76456
Show file tree
Hide file tree
Showing 12 changed files with 132 additions and 150 deletions.
5 changes: 2 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: shinyWidgets
Title: Custom Inputs Widgets for Shiny
Version: 0.8.6
Version: 0.8.6.9000
Authors@R: c(
person("Victor", "Perrier", email = "[email protected]", role = c("aut", "cre", "cph")),
person("Fanny", "Meyer", role = "aut"),
Expand All @@ -18,12 +18,11 @@ BugReports: https://github.com/dreamRs/shinyWidgets/issues
License: GPL-3
Encoding: UTF-8
LazyData: true
RoxygenNote: 7.3.1
RoxygenNote: 7.3.2
Roxygen: list(markdown = TRUE)
Depends:
R (>= 3.1.0)
Imports:
anytime,
bslib,
sass,
shiny (>= 1.6.0),
Expand Down
52 changes: 21 additions & 31 deletions R/input-airDatepicker.R
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
#' @param language Language to use, can be one of
#' `ar`, `cs`, `da`, `de`, `en`, `es`, `fi`, `fr`, `hu`, `it`, `ja`, `ko`, `nl`,
#' `pl`, `pt-BR`, `pt`, `ro`, `ru`, `si`, `sk`, `sv`, `th`, `tr`, `uk`, `zh`.
#' @param tz The timezone.
#' @param inline If `TRUE`, datepicker will always be visible.
#' @param readonly If `TRUE`, datepicker will be readonly and the input field won't be editable.
#' @param onkeydown Attribute passed to the input field.
Expand Down Expand Up @@ -138,6 +139,7 @@ airDatepickerInput <- function(inputId,
addon = c("right", "left", "none"),
addonAttributes = list(class = "btn-outline-secondary"),
language = "en",
tz = NULL,
inline = FALSE,
readonly = FALSE,
onkeydown = NULL,
Expand All @@ -147,24 +149,16 @@ airDatepickerInput <- function(inputId,
# dput(tools::file_path_sans_ext(list.files("node_modules/air-datepicker/locale/", pattern = "\\.js")))
language <- match.arg(
arg = language,
choices = c("ar", "bg", "cs", "da", "de", "en", "es", "fi", "fr", "hr", "hu", "it",
"nl", "pl", "pt-BR", "pt", "ro", "ru", "si", "sk", "sv", "th",
"tr", "uk", "zh", "ja", "ko"),
choices = c(
"ar", "bg", "cs", "da", "de", "en", "es", "fi", "fr", "hr", "hu", "it",
"nl", "pl", "pt-BR", "pt", "ro", "ru", "si", "sk", "sv", "th",
"tr", "uk", "zh", "ja", "ko"
),
several.ok = FALSE
)

version <- getOption("air-datepicker", default = 3)

list1 <- function(x) {
if (is.null(x))
return(x)
if (length(x) == 1 & !is.list(x)) {
list(x)
} else {
x
}
}

buttons <- character(0)
if (clearButton)
buttons <- c(buttons, "clear")
Expand All @@ -173,14 +167,14 @@ airDatepickerInput <- function(inputId,
if (length(buttons) < 1)
buttons <- FALSE


airParams <- dropNulls(list(
updateOn = match.arg(update_on),
disabledDates = list1(disabledDates),
disabledDaysOfWeek = list1(disabledDaysOfWeek),
highlightedDates = list1(highlightedDates),
startView = startView,
value = list1(value),
tz = tz,
todayButtonAsDate = inherits(todayButton, c("Date", "POSIXt")),
language = toupper(language),
options = c(dropNulls(list(
Expand Down Expand Up @@ -215,8 +209,7 @@ airDatepickerInput <- function(inputId,
placeholder = placeholder,
autocomplete = "off",
readonly = if (isTRUE(readonly)) "",
onkeydown = onkeydown,
`data-timepicker` = tolower(timepicker)
onkeydown = onkeydown
)
if (!identical(addon, "none")) {
tagAir <- tags$div(
Expand Down Expand Up @@ -419,6 +412,7 @@ airYearpickerInput <- function(inputId, label = NULL, value = NULL, ...) {
#' @param inputId The id of the input object.
#' @param label The label to set for the input object.
#' @param value The value to set for the input object.
#' @param tz The timezone.
#' @param clear Logical, clear all previous selected dates.
#' @param options Options to update, see available ones in [JavaScript documentation](https://air-datepicker.com/docs)
#' @param show,hide Show / hide datepicker.
Expand All @@ -437,23 +431,12 @@ updateAirDateInput <- function(session = getDefaultReactiveDomain(),
inputId,
label = NULL,
value = NULL,
tz = NULL,
clear = FALSE,
options = NULL,
show = FALSE,
hide = FALSE) {
stopifnot(is.logical(clear))
to_ms <- function(x) {
if (is.null(x))
return(NULL)
1000 * as.numeric(as.POSIXct(as.character(x), tz = Sys.timezone()))
}
if (!is.null(value)) {
value <- as.character(toJSON(x = to_ms(value), auto_unbox = FALSE))
}
if (!is.null(options)) {
options$minDate <- to_ms(options$minDate)
options$maxDate <- to_ms(options$maxDate)
}
if (!is.null(options$disabledDates)) {
options$disabledDates <- list1(options$disabledDates)
}
Expand All @@ -463,11 +446,18 @@ updateAirDateInput <- function(session = getDefaultReactiveDomain(),
message <- dropNulls(list(
id = session$ns(inputId),
label = label,
value = value,
clear = isTRUE(clear),
options = dropNulls(options),
show = isTRUE(show),
hide = isTRUE(hide)
hide = isTRUE(hide),
config = jsonlite::toJSON(
x = dropNullsOrEmpty(list(
options = dropNulls(options),
value = dropNulls(list(value = list1(value), tz = tz))
)),
auto_unbox = TRUE,
json_verbatim = TRUE,
POSIXt = "epoch"
)
))
session$sendInputMessage(inputId, message)
}
Expand Down
45 changes: 25 additions & 20 deletions R/onLoad.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,31 +20,36 @@
}
}, force = TRUE)
shiny::registerInputHandler("air.date", function(data, ...) {
if (is.null(data)) {
NULL
if (is.null(data))
return(NULL)
if (!is.list(data))
return(NULL)
if (is.null(data$date))
return(NULL)
res <- try(as.Date(unlist(data$date)), silent = TRUE)
if (inherits(res, "try-error")) {
warning("Failed to parse dates!")
# as.Date(NA)
data
} else {
res <- try(anytime::anydate(unlist(data)), silent = TRUE)
if ("try-error" %in% class(res)) {
warning("Failed to parse dates!")
# as.Date(NA)
data
} else {
res
}
res
}
}, force = TRUE)
shiny::registerInputHandler("air.datetime", function(data, ...) {
if (is.null(data)) {
NULL
if (is.null(data))
return(NULL)
if (!is.list(data))
return(NULL)
if (is.null(data$date))
return(NULL)
tz <- data$tz %||% ""
res <- try(as.POSIXct(unlist(data$date), tz = tz), silent = TRUE)
if (inherits(res, "try-error")) {
warning("Failed to parse dates!")
# as.Date(NA)
data
} else {
res <- try(anytime::anytime(unlist(data)), silent = TRUE)
if ("try-error" %in% class(res)) {
warning("Failed to parse dates!")
# as.Date(NA)
data
} else {
res
}
res
}
}, force = TRUE)
shiny::registerInputHandler("sw.tree", function(data, ...) {
Expand Down
2 changes: 2 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ starts_with <- function(x, prefix) {
}

list1 <- function(x) {
if (is.null(x))
return(x)
if (length(x) == 1) {
list(x)
} else {
Expand Down
4 changes: 2 additions & 2 deletions inst/examples/airDatepicker/datepicker/app.R
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ ui <- fluidPage(

airDatepickerInput(
inputId = "defaultValue",
label = "With a default date:",
value = Sys.Date()-7
label = paste("With a default date:", Sys.Date() - 7),
value = Sys.Date() - 7
),
verbatimTextOutput(outputId = "res_defaultValue"),

Expand Down
15 changes: 14 additions & 1 deletion inst/examples/airDatepicker/timepicker/app.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,15 @@ ui <- fluidPage(
),
verbatimTextOutput(outputId = "res_datetime"),


airDatepickerInput(
inputId = "datetime_tz",
label = "Pick date and time (specific timezone:",
timepicker = TRUE,
tz = "UTC"
),
verbatimTextOutput(outputId = "res_datetime_tz"),

airDatepickerInput(
inputId = "time",
label = "Pick time:",
Expand All @@ -40,7 +49,10 @@ ui <- fluidPage(

airDatepickerInput(
inputId = "options",
label = "With some options:",
label = sprintf(
"With default value (%s) and options:",
as.POSIXct("2018-05-01 20:00:00")
),
multiple = TRUE,
value = as.POSIXct("2018-05-01 20:00:00"),
timepicker = TRUE,
Expand Down Expand Up @@ -76,6 +88,7 @@ ui <- fluidPage(
server <- function(input, output, session) {

output$res_datetime <- renderPrint( input$datetime )
output$res_datetime_tz <- renderPrint( input$datetime_tz )
output$res_time <- renderPrint( input$time )
output$res_options <- renderPrint( input$options )
output$res_french <- renderPrint( input$french )
Expand Down
2 changes: 1 addition & 1 deletion inst/packer/air-datepicker.js

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions man/airDatepicker.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions man/updateAirDateInput.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 14 additions & 27 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,10 @@
"devDependencies": {
"@simonwep/pickr": "^1.6.0",
"@widgetjs/tree": "^1.8.3",
"air-datepicker": "^3.5.0",
"air-datepicker": "^3.5.3",
"autonumeric": "^4.6.0",
"css-loader": "^6.5.1",
"dayjs": "^1.11.4",
"dayjs-plugin-utc": "^0.1.2",
"dayjs": "^1.11.12",
"multi.js": "0.5.0",
"nouislider": "^15.7.1",
"style-loader": "^3.3.1",
Expand Down
Loading

0 comments on commit ca76456

Please sign in to comment.