Skip to content

feat: add support for class name generator for InlineDatePicker #92

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Mar 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>org.vaadin.addons.flowingcode</groupId>
<artifactId>year-month-calendar</artifactId>
<version>4.3.2-SNAPSHOT</version>
<version>4.4.0-SNAPSHOT</version>
<name>Year Month Calendar Add-on</name>
<description>Year Month Calendar Add-on for Vaadin Flow</description>

Expand Down Expand Up @@ -201,12 +201,13 @@
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>license-maven-plugin</artifactId>
<version>1.14</version>
<version>2.3.0</version>
<configuration>
<licenseName>apache_v2</licenseName>
<addJavaLicenseAfterPackage>false</addJavaLicenseAfterPackage>
<excludes>
<exclude>**/shadow-focus-mixin.js</exclude>
<exclude>**/main/dev-bundle/**</exclude>
</excludes>
</configuration>
</plugin>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* #%L
* Year Month Calendar Add-on
* %%
* Copyright (C) 2021 - 2024 Flowing Code
* Copyright (C) 2021 - 2025 Flowing Code
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* #%L
* Year Month Calendar Add-on
* %%
* Copyright (C) 2021 - 2023 Flowing Code
* Copyright (C) 2021 - 2025 Flowing Code
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* #%L
* Year Month Calendar Add-on
* %%
* Copyright (C) 2021 - 2024 Flowing Code
* Copyright (C) 2021 - 2025 Flowing Code
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -27,10 +27,13 @@
import com.vaadin.flow.component.dependency.JsModule;
import com.vaadin.flow.component.dependency.Uses;
import com.vaadin.flow.function.SerializableFunction;
import com.vaadin.flow.function.ValueProvider;
import com.vaadin.flow.internal.JsonSerializer;
import java.time.LocalDate;
import java.time.YearMonth;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.IntStream;

@SuppressWarnings("serial")
@Tag("fc-inline-date-picker")
Expand All @@ -40,7 +43,11 @@
public class InlineDatePicker extends AbstractSinglePropertyField<InlineDatePicker, LocalDate> implements HasSize, HasTheme {

private static final String VALUE_PROPERTY = "value";


private ValueProvider<LocalDate, String> classNameGenerator;

private YearMonth yearMonth;

private static <R,S> SerializableFunction<R,S> map(SerializableFunction<R,S> f) {
return r->Optional.ofNullable(r).map(f).orElse(null);
}
Expand All @@ -50,6 +57,9 @@ public InlineDatePicker() {
super(VALUE_PROPERTY, null, String.class, map(LocalDate::parse), map(LocalDate::toString));
setValue(LocalDate.now());
setWeekNumbersVisible(true);
getElement().addEventListener("month-change", event -> {
setYearMonth(YearMonth.parse(event.getEventData().getString("event.detail.value")));
}).addEventData("event.detail.value");
}

/**
Expand Down Expand Up @@ -85,4 +95,54 @@ public void setI18n(DatePickerI18n i18n) {
getElement().setPropertyJson("i18n", JsonSerializer.toJson(i18n));
}

/**
* Sets the function that generates CSS class names for days in this calendar.
*
* Returning {@code null} from the generator results in no custom class name being set. Multiple
* class names can be returned from the generator as space-separated.
*
* @param classNameGenerator the {@code ValueProvider} to use for generating class names
*/
public void setClassNameGenerator(ValueProvider<LocalDate, String> classNameGenerator) {
this.classNameGenerator = classNameGenerator;
refreshAll();
}

/** Refresh the styles of all dates in the displayed year and month. */
public void refreshAll() {
IntStream
.rangeClosed(1, yearMonth.lengthOfMonth())
.mapToObj(yearMonth::atDay).forEach(this::refreshItem);
getElement().executeJs("setTimeout(()=>this._clearEmptyDaysStyle())");
}

/**
* Refresh the style of the given date.
*
* @param date the date to update the style for
*/
public void refreshItem(LocalDate date) {
if (date.getYear() == yearMonth.getYear() && date.getMonth() == yearMonth.getMonth()) {
String className =
Optional.ofNullable(classNameGenerator).map(g -> g.apply(date)).orElse(null);
getElement().executeJs("setTimeout(()=>{this._setStyleForDay($0,$1);})", date.getDayOfMonth(),
className);
}
}

@Override
public void setValue(LocalDate value) {
Objects.requireNonNull(value);
super.setValue(value);
setYearMonth(YearMonth.from(value));
}

private void setYearMonth(YearMonth value) {
Objects.requireNonNull(value);
if (!value.equals(yearMonth)) {
yearMonth = value;
refreshAll();
}
}

}
30 changes: 9 additions & 21 deletions src/main/java/com/flowingcode/addons/ycalendar/MonthCalendar.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* #%L
* Year Month Calendar Add-on
* %%
* Copyright (C) 2021 - 2023 Flowing Code
* Copyright (C) 2021 - 2025 Flowing Code
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -31,6 +31,7 @@
import java.time.Month;
import java.time.YearMonth;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.IntStream;

/** A component for displaying a calendar view of a specific month and year. */
Expand Down Expand Up @@ -121,15 +122,9 @@ public void setClassNameGenerator(ValueProvider<LocalDate, String> classNameGene
/** Refresh the styles of all dates in the displayed year and month. */
@Override
public void refreshAll() {
IntStream.rangeClosed(1, yearMonth.lengthOfMonth()).forEach(dayOfMonth -> {
String className;
if (classNameGenerator != null) {
className = classNameGenerator.apply(yearMonth.atDay(dayOfMonth));
} else {
className = null;
}
setStyleForDay(dayOfMonth, className);
});
IntStream.rangeClosed(1, yearMonth.lengthOfMonth())
.mapToObj(yearMonth::atDay)
.forEach(this::refreshItem);
getElement().executeJs("setTimeout(()=>this._clearEmptyDaysStyle())");
}

Expand All @@ -141,20 +136,13 @@ public void refreshAll() {
*/
public void refreshItem(LocalDate date) {
if (date.getYear() == yearMonth.getYear() && date.getMonth() == yearMonth.getMonth()) {
String className;
if (classNameGenerator != null) {
className = classNameGenerator.apply(date);
} else {
className = null;
}
setStyleForDay(date.getDayOfMonth(), className);
String className =
Optional.ofNullable(classNameGenerator).map(g -> g.apply(date)).orElse(null);
getElement().executeJs("setTimeout(()=>{this._setStyleForDay($0,$1);})", date.getDayOfMonth(),
className);
} else {
throw new IllegalArgumentException();
}
}

private void setStyleForDay(int dayOfMonth, String className) {
getElement().executeJs("setTimeout(()=>{this._setStyleForDay($0,$1);})", dayOfMonth, className);
}

}
12 changes: 4 additions & 8 deletions src/main/java/com/flowingcode/addons/ycalendar/YearCalendar.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* #%L
* Year Month Calendar Add-on
* %%
* Copyright (C) 2021 - 2023 Flowing Code
* Copyright (C) 2021 - 2025 Flowing Code
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -30,6 +30,7 @@
import com.vaadin.flow.shared.Registration;
import java.time.LocalDate;
import java.time.Year;
import java.util.Optional;
import java.util.stream.IntStream;

/** A component that displays the calendar of a whole year. */
Expand Down Expand Up @@ -94,13 +95,8 @@ public void refreshAll() {
/** If the year is currently displayed, refresh the given date. Otherwise do nothing. */
public void refreshItem(LocalDate date) {
if (date.getYear() == getYear()) {
String className;
if (classNameGenerator != null) {
className = classNameGenerator.apply(date);
} else {
className = null;
}

String className =
Optional.ofNullable(classNameGenerator).map(g -> g.apply(date)).orElse(null);
getElement().executeJs("setTimeout(()=>{this._setStyleForDay($0,$1,$2);})",
date.getDayOfMonth(), date.getMonthValue(), className);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* #%L
* Year Month Calendar Add-on
* %%
* Copyright (C) 2021 - 2023 Flowing Code
* Copyright (C) 2021 - 2025 Flowing Code
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* #%L
* Year Month Calendar Add-on
* %%
* Copyright (C) 2021 - 2023 Flowing Code
* Copyright (C) 2021 - 2025 Flowing Code
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* #%L
* Year Month Calendar Add-on
* %%
* Copyright (C) 2021 - 2024 Flowing Code
* Copyright (C) 2021 - 2025 Flowing Code
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -91,13 +91,22 @@ export class InlineDatePicker extends LitElement {
}

__onDisplayMonthChange() {
this.displayDate=new Date(this.shadowRoot.querySelector("fc-year-month-field").value+'-02');
const yearMonth = this.shadowRoot.querySelector("fc-year-month-field").value;
this.displayDate=new Date(yearMonth+'-02');
this.dispatchEvent(new CustomEvent("month-change",{detail:{value: yearMonth}}));
}

__onDateSelected(e) {
this.value=e.detail.value;
}

_setStyleForDay(i,className) {
this.shadowRoot.querySelector("fc-month-calendar")._setStyleForDay(i,className);
}

_clearEmptyDaysStyle() {
this.shadowRoot.querySelector("fc-month-calendar")._clearEmptyDaysStyle();
}
}

customElements.define(InlineDatePicker.is, InlineDatePicker);
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* #%L
* Year Month Calendar Add-on
* %%
* Copyright (C) 2021 - 2023 Flowing Code
* Copyright (C) 2021 - 2025 Flowing Code
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* #%L
* Year Month Calendar Add-on
* %%
* Copyright (C) 2021 - 2023 Flowing Code
* Copyright (C) 2021 - 2025 Flowing Code
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* #%L
* Year Month Calendar Add-on
* %%
* Copyright (C) 2021 - 2023 Flowing Code
* Copyright (C) 2021 - 2025 Flowing Code
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* #%L
* Year Month Calendar Add-on
* %%
* Copyright (C) 2021 - 2023 Flowing Code
* Copyright (C) 2021 - 2025 Flowing Code
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* #%L
* Year Month Calendar Add-on
* %%
* Copyright (C) 2021 - 2023 Flowing Code
* Copyright (C) 2021 - 2025 Flowing Code
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* #%L
* Year Month Calendar Add-on
* %%
* Copyright (C) 2021 - 2023 Flowing Code
* Copyright (C) 2021 - 2025 Flowing Code
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* #%L
* Year Month Calendar Add-on
* %%
* Copyright (C) 2021 - 2024 Flowing Code
* Copyright (C) 2021 - 2025 Flowing Code
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -24,6 +24,7 @@
import com.vaadin.flow.component.notification.Notification;
import com.vaadin.flow.router.PageTitle;
import com.vaadin.flow.router.Route;
import java.time.DayOfWeek;
import java.util.Objects;

@DemoSource
Expand All @@ -41,6 +42,17 @@ public InlineDatePickerDemo() {
field.addValueChangeListener(ev->{
Notification.show(Objects.toString(ev.getValue()));
});

field.setClassNameGenerator(date -> {
if (date.getDayOfWeek() == DayOfWeek.SATURDAY || date.getDayOfWeek() == DayOfWeek.SUNDAY) {
return "weekend";
} else if (TestUtils.isPublicHoliday(date)) {
return "holiday";
} else {
return null;
}
});

add(field);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* #%L
* Year Month Calendar Add-on
* %%
* Copyright (C) 2021 - 2024 Flowing Code
* Copyright (C) 2021 - 2025 Flowing Code
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* #%L
* Year Month Calendar Add-on
* %%
* Copyright (C) 2021 - 2024 Flowing Code
* Copyright (C) 2021 - 2025 Flowing Code
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
Loading