Created
March 30, 2026 08:29
-
-
Save TatuLund/c8b54d4505cf0f89f2ab7ed69fcd788d to your computer and use it in GitHub Desktop.
There are couple of features missing in Vaadin 24 DateTimePicker. 1: There is no automatic setting of time mode, which would be useful in some cases, 2: The time picker does not autoscroll to selected time if such exists. This Gist demonstrates JavaScript workarounds for these missing features.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.example.application.views; | |
| import java.time.Duration; | |
| import java.time.LocalDateTime; | |
| import java.time.temporal.ChronoUnit; | |
| import com.vaadin.flow.component.datetimepicker.DateTimePicker; | |
| import com.vaadin.flow.component.orderedlayout.VerticalLayout; | |
| import com.vaadin.flow.router.Route; | |
| @Route(value = "date-time-view", layout = MainLayout.class) | |
| public class DateTimePickerView extends VerticalLayout { | |
| private boolean hack2set; | |
| private DateTimePicker dateTimePicker1; | |
| private DateTimePicker dateTimePicker2; | |
| public DateTimePickerView() { | |
| dateTimePicker1 = new DateTimePicker(); | |
| dateTimePicker1.setLabel("Automatically set time"); | |
| dateTimePicker1.getElement().executeJs(""" | |
| this.__datePicker.addEventListener('change', (e) => { | |
| this.__timePicker.value='12:00'; | |
| }); | |
| """); | |
| add(dateTimePicker1); | |
| dateTimePicker2 = new DateTimePicker(); | |
| dateTimePicker2.setLabel("Scroll time to view"); | |
| dateTimePicker2.setVisible(true); | |
| dateTimePicker2.setValue( | |
| LocalDateTime.now().plusHours(8).truncatedTo(ChronoUnit.HOURS)); | |
| dateTimePicker2.setStep(Duration.ofMinutes(15)); | |
| dateTimePicker2.getElement().executeJs( | |
| """ | |
| const timePicker = $0.__timePicker; | |
| const comboBox = timePicker.$.comboBox; | |
| timePicker.addEventListener("opened-changed", (e) => { | |
| const scroller = comboBox._scroller; | |
| if (scroller) { | |
| scroller.scrollIntoView(scroller.items.indexOf(scroller.selectedItem)); | |
| } | |
| }); | |
| """, | |
| dateTimePicker2.getElement()); | |
| add(dateTimePicker2); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment