vaadin / flow-components

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

[Grid] scrollToEnd is always executed after scrollToStart regardless of their call order #5880

Open vursen opened 8 months ago

vursen commented 8 months ago

Description

scrollToEnd gets always executed after scrollToStart regardless of their call order in the code.

Expected outcome

When scrollToStart comes after scrollToEnd, I'd expect the grid to scroll to the end and then to the start.

Minimal reproducible example

@Route("grid")
public class GridView extends Div {
    public GridView() {
        Grid<String> grid = new Grid<>();
        grid.addColumn(item -> item);
        grid.setItems(IntStream.range(0, 1000).mapToObj(i -> "Item " + i).collect(Collectors.toList()));

        Button scrollToEndAndStart = new Button("scroll to end and start", event -> {
            grid.scrollToEnd();
            grid.scrollToStart();
        });

        add(grid, scrollToEndAndStart);
    }
}

Steps to reproduce

  1. Click "scroll to end and start"
  2. See that the grid's scroll position is at the end while I'd expect it at the start

Environment

Vaadin version(s): 24.4 and earlier OS: Mac OS

Browsers

No response

vursen commented 8 months ago

Same problem with TreeGrid:

@Route("grid")
public class GridView extends Div {
    private static class DataProvider
            extends AbstractHierarchicalDataProvider<String, Void> {

        @Override
        public int getChildCount(HierarchicalQuery<String, Void> query) {
            return 1000;
        }

        @Override
        public Stream<String> fetchChildren(
                HierarchicalQuery<String, Void> query) {
            int limit = query.getLimit();
            int offset = query.getOffset();
            return IntStream.range(offset, offset + limit)
                    .mapToObj(index -> "Item " + index);
        }

        @Override
        public boolean hasChildren(String item) {
            return false;
        }

        @Override
        public boolean isInMemory() {
            return true;
        }

    }

    public GridView() {
        TreeGrid<String> treeGrid = new TreeGrid<>();
        treeGrid.setDataProvider(new DataProvider());
        treeGrid.addHierarchyColumn(ValueProvider.identity()).setHeader("Name");

        Button scrollToEndAndStart = new Button("scroll to end and start", event -> {
            treeGrid.scrollToEnd();
            treeGrid.scrollToStart();
        });

        add(treeGrid, scrollToEndAndStart);
    }
}