Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add ClientItemToggleEvent to notify when user toggles an item #6884

Merged
merged 20 commits into from
Dec 16, 2024

Conversation

vursen
Copy link
Contributor

@vursen vursen commented Dec 4, 2024

Description

The PR adds a new event ClientItemToggleEvent that fires when the user selects or deselects an item in the grid. The event provides information about the item, its selected state, and whether Shift was pressed during selection. The main purpose of this new event is to enable developers to implement features like range selection. The below example demonstrates how to implement range selection with behavior similar to Gmail:

@Route("range-selection")
public class RangeSelectionPage extends Div {
    private String startItem;

    public RangeSelectionPage() {
        Grid<String> grid = new Grid<>();
        grid.addColumn(s -> s);
        grid.setItems("Item 1", "Item 2", "Item 3", "Item 4");
        grid.setSelectionMode(Grid.SelectionMode.MULTI);
        ((GridMultiSelectionModel<String>) grid.getSelectionModel())
                .addClientItemToggleListener(event -> {
                    String item = event.getItem();

                    startItem = startItem != null ? startItem : item;
                    if (event.isShiftKey()) {
                        Set<String> range = getItemsRange(grid, startItem,
                                item);
                        if (event.isSelected()) {
                            grid.asMultiSelect().select(range);
                        } else {
                            grid.asMultiSelect().deselect(range);
                        }
                    }
                    startItem = item;
                });
        add(grid);
    }

    private Set<String> getItemsRange(Grid<String> grid, String startItem,
            String endItem) {
        var dataView = grid.getListDataView();
        var startIndex = dataView.getItemIndex(startItem).get();
        var endIndex = dataView.getItemIndex(endItem).get();

        return dataView.getItems().skip(Math.min(startIndex, endIndex))
                .limit(Math.abs(startIndex - endIndex) + 1)
                .collect(Collectors.toSet());
    }
}

Depends on

Part of vaadin/web-components#8142

Type of change

  • Feature

@vursen vursen changed the title feat: add ClientItemToggleEvent to fire when user toggles an item feat: add ClientItemToggleEvent to notify when user toggles an item Dec 4, 2024
@vursen vursen force-pushed the feat/shift-click-range-selection branch from b5d5026 to fce4344 Compare December 5, 2024 07:03
@@ -388,7 +388,7 @@ void select(GridTRElement row) {
CheckboxElement checkbox = wrapElement(cell.getFirstChildElement(),
getCommandExecutor()).wrap(CheckboxElement.class);
if (!checkbox.isChecked()) {
checkbox.click();
checkbox.getWrappedElement().click();
Copy link
Contributor Author

@vursen vursen Dec 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In TestBench, the click() method is overridden to use the JS click() function, which doesn't fully simulate a complete mouse action. We need to use the real simulation to trigger mousedown that will set _shiftKeyDown.

@vursen vursen marked this pull request as ready for review December 5, 2024 07:11
@vursen vursen requested review from sissbruecker and tomivirkki and removed request for sissbruecker and tomivirkki December 12, 2024 07:00
…din/flow/component/grid/ClientItemToggleEvent.java

Co-authored-by: Sascha Ißbrücker <sissbruecker@vaadin.com>
@vursen vursen enabled auto-merge (squash) December 16, 2024 14:56
@vursen vursen merged commit b981411 into main Dec 16, 2024
5 checks passed
@vursen vursen deleted the feat/shift-click-range-selection branch December 16, 2024 15:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants