Skip to content

Commit ffb2f0a

Browse files
author
Felipe Lang
committed
feat: add method for setting tooltip to days of week
Close #12
1 parent 5abd1cc commit ffb2f0a

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

src/main/java/com/flowingcode/vaadin/addons/dayofweekselector/DayOfWeekSelector.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,12 @@ public void setValue(DayOfWeek first, DayOfWeek... rest) {
178178
* @param i18n the internationalized properties, not <code>null</code>
179179
*/
180180
public void setI18N(DatePickerI18n i18n) {
181-
setWeekDaysShort(i18n.getWeekdaysShort());
181+
if (i18n.getWeekdaysShort() != null) {
182+
setWeekDaysShort(i18n.getWeekdaysShort());
183+
}
184+
if (i18n.getWeekdays() != null) {
185+
setWeekDaysTooltip(i18n.getWeekdays());
186+
}
182187
if (i18n.getFirstDayOfWeek() == 0) {
183188
setFirstDayOfWeek(DayOfWeek.SUNDAY);
184189
} else {
@@ -202,6 +207,22 @@ public void setWeekDaysShort(List<String> weekdaysShort) {
202207
}
203208
}
204209

210+
/**
211+
* Sets the tooltips of the week days, starting from {@code sun} and ending on {@code sat}.
212+
*
213+
* @param weekdaysTooltip the tooltips of the week days
214+
*/
215+
public void setWeekDaysTooltip(List<String> weekdaysTooltip) {
216+
Objects.requireNonNull(weekdaysTooltip);
217+
218+
for (DayOfWeek day : DayOfWeek.values()) {
219+
int index = day.getValue() % 7;
220+
String text = weekdaysTooltip.get(index);
221+
getButtons().filter(button -> button.getDayOfWeek() == day)
222+
.forEach(button -> button.setTooltipText(text));
223+
}
224+
}
225+
205226
/**
206227
* Sets the first day of the week.
207228
* <p>

src/test/java/com/flowingcode/vaadin/addons/dayofweekselector/DayOfWeekSelectorI18NDemo.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,14 @@
2020
package com.flowingcode.vaadin.addons.dayofweekselector;
2121

2222
import com.flowingcode.vaadin.addons.demo.DemoSource;
23+
import com.vaadin.flow.component.datepicker.DatePicker.DatePickerI18n;
2324
import com.vaadin.flow.component.html.Div;
2425
import com.vaadin.flow.router.PageTitle;
2526
import com.vaadin.flow.router.Route;
2627
import java.time.DayOfWeek;
28+
import java.time.format.TextStyle;
2729
import java.util.List;
30+
import java.util.stream.Stream;
2831

2932
@DemoSource
3033
@PageTitle("I18N")
@@ -34,6 +37,21 @@ public class DayOfWeekSelectorI18NDemo extends Div {
3437

3538
public DayOfWeekSelectorI18NDemo() {
3639
DayOfWeekSelector selector = new DayOfWeekSelector();
40+
selector.setLabel("I18N with DatePickerI18n");
41+
42+
DatePickerI18n i18n = new DatePickerI18n();
43+
i18n.setWeekdaysShort(Stream.of(DayOfWeek.values())
44+
.map(d -> d.getDisplayName(TextStyle.NARROW_STANDALONE, getLocale())).toList());
45+
i18n.setWeekdays(Stream.of(DayOfWeek.values())
46+
.map(d -> d.getDisplayName(TextStyle.FULL_STANDALONE, getLocale())).toList());
47+
i18n.setFirstDayOfWeek(DayOfWeek.MONDAY.getValue() - 1);
48+
selector.setI18N(i18n);
49+
add(selector);
50+
51+
selector = new DayOfWeekSelector();
52+
selector.setLabel("I18N without DatePickerI18n");
53+
selector.setWeekDaysTooltip(
54+
List.of("Domingo", "Lunes", "Martes", "Miércoles", "Jueves", "Viernes", "Sábado"));
3755
selector.setWeekDaysShort(List.of("D", "L", "M", "X", "J", "V", "S"));
3856
selector.setFirstDayOfWeek(DayOfWeek.MONDAY);
3957
add(selector);

0 commit comments

Comments
 (0)