flow icon indicating copy to clipboard operation
flow copied to clipboard

Composite should use eventbus from its content / ItemCountChangeListener not working in Composite

Open hoshie82 opened this issue 2 years ago • 0 comments

Description of the bug

Probably a combination of multiple things.. But I have a class extending from Composite<Grid<T>> with

protected Grid<T> initContent() {
    return grid;
}

I tried to add an ItemCountChangeListener to the grids dataview...

The problem ist, that the DataCommunicator.fireItemCountEvent method fires the event to the composite eventbus itself.

            final Optional<Component> component = Element.get(stateNode).getComponent();

returns the composite, not the grid.

Overriding the getEventbus methode does not help, because the field is also used without the getter in Component.. overwriting the fireEvent-method does not work, because i can't delegate it to the grid because it is protected...

(extending the grid is not an option, because the component is part of a bigger complex with more possible components...)

Expected behavior

The event should be fired for the content element.. Either the eventbus should be delegated, overwritten, whatever...

Minimal reproducible example

    private Component buildTestGrid() {
        TestGrid testGrid = new TestGrid();
        testGrid.content.addColumn(ValueProvider.identity());

        ArrayList<Integer> data = new ArrayList<>();
        ListDataProvider<Integer> provider = new ListDataProvider<>(data);

        AtomicInteger index = new AtomicInteger(0);
        Button button = new Button("add item");
        button.addClickListener(e -> {
            data.add(index.incrementAndGet());
            provider.refreshAll();
        });
        GridListDataView<Integer> view = testGrid.content.setItems(provider);
        view.addItemCountChangeListener(e -> Notification.show("" + e.getItemCount()));
        FlexLayout layout = new FlexLayout(testGrid, button);
        layout.setFlexDirection(FlexLayout.FlexDirection.COLUMN);
        return layout;
    }

    protected static class TestGrid extends Composite<Grid<Integer>> {
        private final Grid<Integer> content = new Grid<>();

        @Override
        protected Grid<Integer> initContent() {
            return content;
        }
    }

Versions

  • Vaadin / Flow version: 23.0.9

hoshie82 avatar May 25 '22 14:05 hoshie82