Skip to content

Commit e715440

Browse files
authored
feat: Add HasHelper::bindHelperText (#23245)
* feat: Add HasHelper::bindHelperText Fixes #23243 * Use SignalUnitTest
1 parent ee4aa23 commit e715440

2 files changed

Lines changed: 108 additions & 0 deletions

File tree

‎flow-server/src/main/java/com/vaadin/flow/component/HasHelper.java‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import java.util.Optional;
1919

2020
import com.vaadin.flow.dom.Element;
21+
import com.vaadin.signals.Signal;
2122

2223
/**
2324
* Mixin interface for field components that have helper text as property and
@@ -70,6 +71,37 @@ default void setHelperText(String helperText) {
7071
getElement().setProperty("helperText", helperText);
7172
}
7273

74+
/**
75+
* Binds a signal's value to the component's helper text so that the helper
76+
* text is updated when the signal's value is updated.
77+
* <p>
78+
* Passing {@code null} as the {@code signal} removes any existing binding
79+
* for the given helper text. When unbinding, the current helper text is
80+
* left unchanged.
81+
* <p>
82+
* While a binding for the helper text is active, any attempt to set the
83+
* text manually throws {@link com.vaadin.signals.BindingActiveException}.
84+
* The same happens when trying to bind a new Signal while one is already
85+
* bound.
86+
* <p>
87+
* Bindings are lifecycle-aware and only active while this component is in
88+
* the attached state; they are deactivated while the component is in the
89+
* detached state.
90+
*
91+
* @param helperTextSignal
92+
* the signal to bind or <code>null</code> to unbind any existing
93+
* binding
94+
* @throws com.vaadin.signals.BindingActiveException
95+
* thrown when there is already an existing binding
96+
* @see #setHelperText(String)
97+
* @see Element#bindProperty(String, Signal)
98+
*
99+
* @since 25.1
100+
*/
101+
default void bindHelperText(Signal<String> helperTextSignal) {
102+
getElement().bindProperty("helperText", helperTextSignal);
103+
}
104+
73105
/**
74106
* Adds the given component into helper slot of component, replacing any
75107
* existing helper component. It adds the component adjacent to the field
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
* Copyright 2000-2026 Vaadin Ltd.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
package com.vaadin.flow.component;
17+
18+
import org.junit.Test;
19+
20+
import com.vaadin.flow.dom.SignalsUnitTest;
21+
import com.vaadin.signals.BindingActiveException;
22+
import com.vaadin.signals.ValueSignal;
23+
24+
import static org.junit.Assert.assertEquals;
25+
import static org.junit.Assert.assertThrows;
26+
27+
public class HasHelperBindHelperTextTest extends SignalsUnitTest {
28+
29+
@Tag("div")
30+
public static class HasHelperComponent extends Component
31+
implements HasHelper {
32+
}
33+
34+
@Test
35+
public void bindHelperText_updatesPropertyOnSignalChange() {
36+
HasHelperComponent c = new HasHelperComponent();
37+
UI.getCurrent().add(c);
38+
39+
ValueSignal<String> signal = new ValueSignal<>("");
40+
c.bindHelperText(signal);
41+
42+
signal.value("help-1");
43+
assertEquals("help-1", c.getElement().getProperty("helperText"));
44+
45+
signal.value("help-2");
46+
assertEquals("help-2", c.getElement().getProperty("helperText"));
47+
}
48+
49+
@Test
50+
public void bindHelperText_setHelperTextWhileBindingActive_throws() {
51+
HasHelperComponent c = new HasHelperComponent();
52+
UI.getCurrent().add(c);
53+
54+
ValueSignal<String> signal = new ValueSignal<>("initial");
55+
c.bindHelperText(signal);
56+
57+
assertThrows(BindingActiveException.class,
58+
() -> c.setHelperText("manual"));
59+
}
60+
61+
@Test
62+
public void bindHelperText_unbindWithNull_stopsUpdates() {
63+
HasHelperComponent c = new HasHelperComponent();
64+
UI.getCurrent().add(c);
65+
66+
ValueSignal<String> signal = new ValueSignal<>("a");
67+
c.bindHelperText(signal);
68+
assertEquals("a", c.getElement().getProperty("helperText"));
69+
70+
c.bindHelperText(null);
71+
signal.value("b");
72+
73+
// After unbinding, value should remain as before
74+
assertEquals("a", c.getElement().getProperty("helperText"));
75+
}
76+
}

0 commit comments

Comments
 (0)