-
Notifications
You must be signed in to change notification settings - Fork 77
feat: add signal bind methods to grid component #8742
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
Merged
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
7e274e3
feat: add signal bind methods to grid component
Artur- ee055ba
fix: clean up selection signal binding on model switch and reduce dup…
heruan 83f2e91
feat: add methods to get grid selection as a signal
heruan 884877c
Revert "feat: add methods to get grid selection as a signal"
heruan 8ff05de
Merge branch 'main' into bind-methods-grid
heruan 4d30cdc
revert: remove bindColumns signal method from Grid
heruan d612153
test: strengthen signal binding cleanup tests on selection model switch
heruan bb1149a
test: add coverage for signal-originated write callback guard
heruan 30b2451
Merge branch 'main' into bind-methods-grid
heruan 38619e4
revert: remove bindHeader, bindFooter, bindEmptyStateText signal methods
heruan b1c3f23
Merge remote-tracking branch 'origin/main' into bind-methods-grid
heruan 2b9cae7
Merge remote-tracking branch 'origin/main' into bind-methods-grid
heruan feb68be
update serialization test
sissbruecker a940a1a
Merge branch 'main' into bind-methods-grid
heruan e46e4f9
update serialization test
sissbruecker 8ec4ae7
Merge branch 'main' into bind-methods-grid
sissbruecker File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
116 changes: 116 additions & 0 deletions
116
...din-grid-flow/src/main/java/com/vaadin/flow/component/grid/GridSelectionSignalHelper.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| /* | ||
| * Copyright 2000-2026 Vaadin Ltd. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); you may not | ||
| * use this file except in compliance with the License. You may obtain a copy of | ||
| * the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
| * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
| * License for the specific language governing permissions and limitations under | ||
| * the License. | ||
| */ | ||
| package com.vaadin.flow.component.grid; | ||
|
|
||
| import java.io.Serializable; | ||
| import java.util.Objects; | ||
|
|
||
| import com.vaadin.flow.component.Component; | ||
| import com.vaadin.flow.component.HasValue; | ||
| import com.vaadin.flow.component.HasValueAndElement; | ||
| import com.vaadin.flow.dom.BindingContext; | ||
| import com.vaadin.flow.dom.SignalBinding; | ||
| import com.vaadin.flow.function.SerializableConsumer; | ||
| import com.vaadin.flow.shared.Registration; | ||
| import com.vaadin.flow.signals.Signal; | ||
|
|
||
| /** | ||
| * Shared helper for binding signals to grid selection model values. | ||
| * <p> | ||
| * For internal use only. May be renamed or removed in a future release. | ||
| */ | ||
| class GridSelectionSignalHelper { | ||
|
|
||
| /** | ||
| * Result of binding a signal to a selection model value. | ||
| * | ||
| * @param signalBinding | ||
| * the signal binding for registering change callbacks | ||
| * @param cleanup | ||
| * a registration to remove the effect and listener when the | ||
| * selection model is removed | ||
| * @param <V> | ||
| * the value type | ||
| */ | ||
| record BindResult<V>(SignalBinding<V> signalBinding, | ||
| Registration cleanup) implements Serializable { | ||
| } | ||
|
|
||
| /** | ||
| * Binds a signal to a {@link HasValueAndElement} target, keeping the value | ||
| * synchronized while the owner component is attached. | ||
| * | ||
| * @param owner | ||
| * the component that owns the effect lifecycle | ||
| * @param target | ||
| * the HasValueAndElement to bind to | ||
| * @param valueSignal | ||
| * the signal to bind | ||
| * @param writeCallback | ||
| * the callback to propagate value changes back, or {@code null} | ||
| * for a read-only binding | ||
| * @param <E> | ||
| * the value change event type | ||
| * @param <V> | ||
| * the value type | ||
| * @return the binding result containing the signal binding and a cleanup | ||
| * registration | ||
| */ | ||
| @SuppressWarnings("unchecked") | ||
| static <E extends HasValue.ValueChangeEvent<V>, V> BindResult<V> bindValue( | ||
| Component owner, HasValueAndElement<E, V> target, | ||
| Signal<V> valueSignal, SerializableConsumer<V> writeCallback) { | ||
| Objects.requireNonNull(valueSignal, "Signal cannot be null"); | ||
|
|
||
| SignalBinding<V> binding = new SignalBinding<>(); | ||
| boolean[] fromSignal = { false }; | ||
| V[] previousValue = (V[]) new Object[] { valueSignal.peek() }; | ||
|
|
||
| Registration effectReg = Signal.effect(owner, ctx -> { | ||
| V newValue = valueSignal.get(); | ||
| V oldValue = previousValue[0]; | ||
| try { | ||
| fromSignal[0] = true; | ||
| target.setValue(newValue); | ||
| } finally { | ||
| fromSignal[0] = false; | ||
| } | ||
| if (ctx.isInitialRun() || binding.hasCallbacks()) { | ||
| var bindingContext = new BindingContext<>(ctx.isInitialRun(), | ||
| ctx.isBackgroundChange(), oldValue, newValue, | ||
| target.getElement()); | ||
| binding.setInitialContext(bindingContext); | ||
| if (binding.hasCallbacks()) { | ||
| binding.fireOnChange(bindingContext); | ||
| } | ||
| } | ||
| previousValue[0] = newValue; | ||
| }); | ||
|
|
||
| Registration listenerReg = target.addValueChangeListener(event -> { | ||
| if (!fromSignal[0] && writeCallback != null) { | ||
| writeCallback.accept(target.getValue()); | ||
| } | ||
| }); | ||
|
|
||
| Registration cleanup = () -> { | ||
| effectReg.remove(); | ||
| listenerReg.remove(); | ||
| }; | ||
|
|
||
| return new BindResult<>(binding, cleanup); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tests still pass if I remove this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Correct, the signal's own equality check prevented an actual infinite loop, so existing tests didn't cover the guard. Added now tests that explicitly count calls.