Skip to content

Commit

Permalink
calendarPro: added locale + input value in month/year type
Browse files Browse the repository at this point in the history
  • Loading branch information
pvictor committed Feb 4, 2025
1 parent d6f43ad commit b828332
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 13 deletions.
6 changes: 5 additions & 1 deletion R/calendar-pro-input.R
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ html_dependency_calendar_pro <- function() {
#' @param selectionTimeMode This parameter enables time selection. You can also specify the time format using a boolean value or a number: 24-hour or 12-hour format.
#' @param selectedTime Initial time value.
#' @param ... Other settings passed to Slim Select JAvaScript method.
#' @param locale This parameter sets the language localization of the calendar. You can specify a language label according to BCP 47 or provide arrays of month and weekday names.
#' See https://vanilla-calendar.pro/docs/reference/settings#locale.
#' @param format Format to use when displaying date in input field, if an initial value is provided it must be a date so that the format apply.
#' @param positionToInput This parameter specifies the position of the calendar relative to input,
#' if the calendar is initialized with the input parameter. Possible values: 'auto' | 'center' | 'left' | 'right' | c('bottom' | 'top', 'center' | 'left' | 'right')
Expand Down Expand Up @@ -81,6 +83,7 @@ calendarProInput <- function(inputId,
selectionTimeMode = NULL,
selectedTime = NULL,
...,
locale = NULL,
format = "%Y-%m-%d",
positionToInput = "auto",
selectedTheme = "light",
Expand Down Expand Up @@ -136,7 +139,8 @@ calendarProInput <- function(inputId,
disableDates = list1(disableDates),
selectionTimeMode = selectionTimeMode,
selectedTime = selectedTime,
firstWeekday = firstWeekday
firstWeekday = firstWeekday,
locale = locale
))
options <- modifyList(options, list(...))
config <- list(
Expand Down
13 changes: 11 additions & 2 deletions examples/calendar-pro.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ ui <- fluidPage(
calendarProInput(
inputId = "cal3",
label = "Calendar with initial value:",
format = "%d/%m/%Y",
value = Sys.Date() + 1,
width = "100%"
),
Expand All @@ -37,7 +36,16 @@ ui <- fluidPage(
enableWeekNumbers = TRUE,
width = "100%"
),
verbatimTextOutput("res7")
verbatimTextOutput("res7"),
calendarProInput(
inputId = "cal9",
label = "Calendar with format and locale:",
format = "%d/%m/%Y",
locale = "fr",
value = Sys.Date() + 1,
width = "100%"
),
verbatimTextOutput("res9"),
),
column(
width = 6,
Expand Down Expand Up @@ -89,6 +97,7 @@ server <- function(input, output, session) {
output$res6 <- renderPrint(input$cal6)
output$res7 <- renderPrint(input$cal7)
output$res8 <- renderPrint(input$cal8)
output$res9 <- renderPrint(input$cal9)

}

Expand Down
4 changes: 3 additions & 1 deletion inst/examples/calendar-pro/value/app.R
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,10 @@ ui <- fluidPage(
placeholder = "Select a year",
type = "year",
selectionMonthsMode = FALSE,
selectedMonth = 0,
width = "100%",
parseValue = parseValue
parseValue = parseValue,
format = "%Y"
),
verbatimTextOutput("res11")
),
Expand Down
2 changes: 1 addition & 1 deletion inst/packer/calendar-pro.js

Large diffs are not rendered by default.

17 changes: 15 additions & 2 deletions man/calendarProInput.Rd

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

29 changes: 23 additions & 6 deletions srcjs/inputs/vanilla-calendar-pro.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ import dayjs from "dayjs";
function changeToInputMonth(fmt) {
return function(self) {
if (!self.context.inputElement) return;
console.log(self);
if (self.context.selectedMonth[0]) {
var date = self.context.selectedYear[0] + "-" + self.context.selectedMonth[0] + "-01";
if (self.context.selectedMonth) {
var date = self.context.selectedYear + "-" + self.context.selectedMonth + "-01";
self.context.inputElement.value = dayjs(date).format(fmt);
//self.hide();
} else {
Expand All @@ -20,10 +19,22 @@ function changeToInputMonth(fmt) {
};
}

function changeToInputSingle(fmt) {
function changeToInputYear(fmt) {
return function(self) {
if (!self.context.inputElement) return;
if (self.context.selectedYear) {
var date = self.context.selectedYear + "01-01";
self.context.inputElement.value = dayjs(date).format(fmt);
//self.hide();
} else {
self.context.inputElement.value = "";
}
};
}

function changeToInputSingle(fmt) {
return function(self) {
if (!self.context.inputElement) return;
if (self.context.selectedDates[0]) {
var date = self.context.selectedDates[0];
if (self.context?.selectedTime) {
Expand Down Expand Up @@ -151,9 +162,15 @@ $.extend(calendarProBinding, {
$(el).trigger("change");
}
if (options.type == "month") {
options.onClickMonth = updateValueOnChange;
options.onClickMonth = function(self) {
updateValueOnChange(self);
changeToInputMonth(config.format)(self);
};
} else if (options.type == "year") {
options.onClickYear = updateValueOnChange;
options.onClickYear = function(self) {
updateValueOnChange(self);
changeToInputYear(config.format)(self);
};
} else {
options.onClickDate = updateValueOnChange;
options.onChangeTime = updateValueOnChange;
Expand Down

0 comments on commit b828332

Please sign in to comment.