docs: add Grid click-to-select example for multi-select mode - #6188
totally-not-ai[bot] wants to merge 2 commits into
Conversation
Add a Flow example to the Tree Grid styling page that highlights the descendants of the selected row with a custom part name, and document how to toggle selection by clicking a row in multi-select mode while respecting a conditional selection predicate. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Preview DeploymentThis PR has been deployed for preview. URL: https://docs-preview-pr-6188.fly.dev Changed pagesAdded content is highlighted in green; removed content is marked in red on each page.
Built from 688bcd3 |
| === Selecting Rows by Clicking [badge-flow]#Flow# | ||
|
|
||
| In multi-select mode, clicking a row doesn't change its selection by default. To let users toggle the selection by clicking anywhere on a row, add an item click listener that selects or deselects the clicked item. Programmatic selection ignores the <<#conditional-selection,conditional selection>> predicate, so if you use one, check it in the listener as well. | ||
|
|
||
| [source,java] | ||
| ---- | ||
| grid.setSelectionMode(Grid.SelectionMode.MULTI); | ||
| SerializablePredicate<Order> isSelectable = order -> order | ||
| .getStatus() != Order.State.COMPLETE; | ||
| grid.setItemSelectableProvider(isSelectable); | ||
|
|
||
| grid.addItemClickListener(event -> { | ||
| Order order = event.getItem(); | ||
| if (!isSelectable.test(order)) { | ||
| return; | ||
| } | ||
| if (grid.getSelectedItems().contains(order)) { | ||
| grid.deselect(order); | ||
| } else { | ||
| grid.select(order); | ||
| } | ||
| }); | ||
| ---- |
There was a problem hiding this comment.
Doesn't Grid have an API similar to the selection column's autoSelect?
There was a problem hiding this comment.
@vursen Sorry, I missed that! Partly. The web component's <vaadin-grid-selection-column> has an autoSelect property (auto-select attribute): when it's set, the row that becomes active on click is toggled, and shift-click selects a range. That's exactly this use case. Flow doesn't expose it, though. GridMultiSelectionModel and GridSelectionColumn only have setDragSelect, and there's no public accessor for the selection column element, so in Flow the item click listener is currently the only supported way.
Options:
- Document
auto-selectfor Lit and React, and keep the click-listener snippet for Flow with a note that there's no API for it yet. - Add a
setAutoSelect(boolean)toGridMultiSelectionModelin flow-components (next tosetDragSelect), then document that for all three frameworks and drop the listener workaround.
I'd go with 2, with 1 as the interim if you want this merged before the API lands. Which would you prefer?
| == Highlighting a Subtree [badge-flow]#Flow# | ||
|
|
||
| Custom part names can depend on the position of an item in the hierarchy. In the example below, selecting a row highlights all of its descendants with a `selected-subtree` part name. The part name generator checks whether the selected item is an ancestor of the rendered item. Because part names are generated only when a row is rendered, the selection listener refreshes the descendants of both the previously and the newly selected item. | ||
|
|
||
| [.example,themes="lumo,aura"] | ||
| -- | ||
| [source,typescript] | ||
| ---- | ||
| include::{root}/frontend/demo/component/tree-grid/tree-grid-imports.ts[preimport,hidden] | ||
| ---- | ||
|
|
||
| [source,java] | ||
| ---- | ||
| include::{root}/src/main/java/com/vaadin/demo/component/treegrid/TreeGridSubtreeHighlight.java[render,tags=snippet,indent=0] | ||
| ---- | ||
|
|
||
| [source,css] | ||
| ---- | ||
| include::{root}/frontend/themes/docs/tree-grid-subtree-highlight.css[] | ||
| ---- | ||
| -- | ||
|
|
||
| See <<../grid/styling#part-name-generator,Dynamically Styling Rows & Columns>> for more information about custom part names. |
There was a problem hiding this comment.
@totally-not-ai Extract this change into a separate PR
It's now proposed in #6211. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Summary
This adds a short Flow example to the Grid selection page. It shows how to select or deselect a row by clicking it in multi-select mode, while still respecting a conditional selection rule.
What changed
articles/components/grid/selection.adoc. It is marked as Flow-only.setItemSelectableProvider). So the example checks the same predicate inside the listener before it selects a row.This is a docs-only change and is fully backward-compatible.