Skip to content

Cell focus listener stops working after Grid is scrolled and there is no count callback #8192

@TatuLund

Description

@TatuLund

Description

The cellFocusListener stops working if the data provider has the exact size of a page, and returns an empty stream for the next callback because count callback is not used.

Expected outcome

Cell focus event should be dispatched after scrolling too.

Minimal reproducible example

@Route(value = "grid-cell-focus", layout = MainLayout.class)
public class GridCellFocusView extends VerticalLayout {

    public GridCellFocusView() {
        setSizeFull();
        setMargin(true);
        setSpacing(true);
        initGridProblem();
    }

    private void initGridProblem() {
        Div focusedElement = new Div("focused: ");
        final Grid<String> grid = new Grid<>();
        grid.addColumn(s -> s);
        grid.addColumn("another-column "::concat);
        grid.setPageSize(50);
        setDataProvider(grid,
                IntStream.range(1, 51).mapToObj("Item %d"::formatted).toList());
        grid.addCellFocusListener(event -> focusedElement
                .setText("focused: " + event.getItem().orElse("NONE")));
        this.add(grid, focusedElement);
    }

    private <T> void setDataProvider(Grid<T> grid, List<T> items) {
        grid.setItems(query -> {
            query.getLimit();
            if (items.isEmpty() || query.getOffset() >= items.size()) {
                return Stream.empty();
            }
            return items
                    .subList(query.getOffset(),
                            Math.min(items.size(),
                                    query.getLimit() + query.getOffset()))
                    .stream();
        });
    }
}

Steps to reproduce

  • Open the test view (use the code above)
  • Click Grid cell
  • Move up/down with cursor keys
  • Observe "focused: " is being updated
  • Scroll Grid to bottom
  • Move up/down with cursor keys
  • Observe "focused: " is not being updated

Environment

Vaadin version(s): 24.9.2

Browsers

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions