vaadin / flow-components

Java counterpart of Vaadin Web Components
101 stars 66 forks source link

DateTimePicker in GridPro formats date wrong #2428

Open skiedrowski opened 2 years ago

skiedrowski commented 2 years ago

Description

A DateTimePicker within a GridPro cell

With regard to these two behaviors, DateTimePicker behaves better when used outside a grid:

Expected outcome

Behavior of DateTimePicker in GridPro as least as good as with Vaadin 21.

Actual outcome

Live Demo (optional)

dtp inside of gridpro dtp outside of gridpro

Minimal reproducible example

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Locale;

import com.vaadin.flow.component.Component;
import com.vaadin.flow.component.Composite;
import com.vaadin.flow.component.datetimepicker.DateTimePicker;
import com.vaadin.flow.component.grid.Grid;
import com.vaadin.flow.component.gridpro.GridPro;
import com.vaadin.flow.component.gridpro.ItemUpdater;
import com.vaadin.flow.component.notification.Notification;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
import com.vaadin.flow.data.renderer.LocalDateTimeRenderer;
import com.vaadin.flow.router.Route;

@Route("grid-pro")
public class GridProView extends Composite<Component> {

    @Override
    protected Component initContent() {
        final var grid = new GridPro<Book>();
        grid.addColumn(Book::getTitle).setHeader("Book").setAutoWidth(true).setSortable(true);
        grid.addColumn(book -> book.getPublisher().getName()).setHeader("Publisher").setSortable(true);
        grid.addColumn(Book::getAuthor).setHeader("Author").setSortable(true);

        grid.addEditColumn(
                        Book::getLastSoldDateTime,
                        new LocalDateTimeRenderer<>(Book::getLastSoldDateTime, DateTimeFormatter.ofPattern("dd.MM.yyyy HH:mm:ss", Locale.GERMANY)))
                .custom(
                        new DateTimePicker(),
                        (ItemUpdater<Book, LocalDateTime>) (book, localDateTime) -> {
                            Notification.show("book " + book.getTitle() + " last sold on " + localDateTime);
                            book.setLastSoldDateTime(localDateTime);
                        }
                ).setHeader("last sold");

        updateGrid(grid);

        final var dtp = new DateTimePicker("DateTimePicker outside of GridPro");

        return new VerticalLayout(grid, dtp);
    }

    private void updateGrid(Grid<Book> grid) {
        List<Book> books = BookService.findAll();
        grid.setItems(books);
    }
}

Helper classes (Book) taken from Book "Practical Vaadin" by Alejandro Duarte, chapter 6. They should be self-explanatory.

Steps to reproduce

Environment

Browsers Affected

did not check others

DiegoCardoso commented 2 years ago

Part of the issue has been fixed by https://github.com/vaadin/web-components/pull/3398. Clicking on the calendar icon works when it is inside a GridPro element. Note that this issue was also present on DatePicker.

About the formats date issue, I can confirm it, but I haven't been able to figure out what is causing it yet. Unlike the calendar icon issue, this one only happens with DateTimePicker. The DatePicker for Flow uses custom formatDate/parseDate function that are set up by the connector when the element is created. I could verify that on the moment the element is created on the client side (but before it is attached to the GridPro), the custom functions are correctly added. But, when the user edits the cell (which effectively attaches the DTP to the GridPro), the functions are changed back to their original versions coming from the web component. I couldn't find what is causing this issue yet.

skiedrowski commented 2 years ago

Confirmed: Fix for blur behavior works with Vaadin 23.0.5.

The buggy formatting is still present.