Skip to content

Commit 2f986be

Browse files
mstahvArtur-claude
authored
feat: add setSubtitle method for setting Card subtitle as a string (#8204)
Co-authored-by: Artur <artur@vaadin.com> Co-authored-by: Claude <noreply@anthropic.com>
1 parent 2bb5245 commit 2f986be

File tree

4 files changed

+59
-1
lines changed
  • vaadin-card-flow-parent
    • vaadin-card-flow-integration-tests/src/main/java/com/vaadin/flow/component/card/tests
    • vaadin-card-flow

4 files changed

+59
-1
lines changed

vaadin-card-flow-parent/vaadin-card-flow-integration-tests/src/main/java/com/vaadin/flow/component/card/tests/CardPage.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public CardPage() {
3030
Card card = new Card();
3131

3232
card.setTitle(new Span("Title"));
33-
card.setSubtitle(new Span("Subtitle"));
33+
card.setSubtitle("Subtitle");
3434
card.setMedia(
3535
new Image("https://vaadin.com/images/vaadin-logo.svg", ""));
3636
card.setHeaderPrefix(new Span("Header prefix"));

vaadin-card-flow-parent/vaadin-card-flow/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@
1111
<name>Vaadin Card</name>
1212
<description>Vaadin Card</description>
1313
<dependencies>
14+
<dependency>
15+
<groupId>com.vaadin</groupId>
16+
<artifactId>flow-html-components</artifactId>
17+
<version>${project.version}</version>
18+
</dependency>
1419
<dependency>
1520
<groupId>com.vaadin</groupId>
1621
<artifactId>flow-test-generic</artifactId>

vaadin-card-flow-parent/vaadin-card-flow/src/main/java/com/vaadin/flow/component/card/Card.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import com.vaadin.flow.component.Tag;
3232
import com.vaadin.flow.component.dependency.JsModule;
3333
import com.vaadin.flow.component.dependency.NpmPackage;
34+
import com.vaadin.flow.component.html.Span;
3435
import com.vaadin.flow.component.shared.HasThemeVariant;
3536
import com.vaadin.flow.component.shared.SlotUtils;
3637
import com.vaadin.flow.dom.Element;
@@ -185,6 +186,23 @@ public void setSubtitle(Component subtitle) {
185186
SlotUtils.setSlot(this, SUBTITLE_SLOT_NAME, subtitle);
186187
}
187188

189+
/**
190+
* Sets the card's subtitle. If a {@link #setHeader(Component) header
191+
* component} is set, the subtitle will not be displayed.
192+
* <p>
193+
* Passing {@code null} removes the current subtitle from the card.
194+
*
195+
* @param subtitle
196+
* the subtitle, or {@code null} to remove
197+
*/
198+
public void setSubtitle(String subtitle) {
199+
if (subtitle == null) {
200+
setSubtitle((Component) null);
201+
} else {
202+
setSubtitle(new Span(subtitle));
203+
}
204+
}
205+
188206
/**
189207
* Gets the current subtitle component.
190208
*

vaadin-card-flow-parent/vaadin-card-flow/src/test/java/com/vaadin/flow/component/card/CardTest.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,41 @@ public void setSubtitle_slotAttributeSet() {
139139
setSlotContent_slotAttributeIsSet(Card::setSubtitle, "subtitle");
140140
}
141141

142+
@Test
143+
public void setStringSubtitle_subtitleIsSet() {
144+
var subtitle = "Some Subtitle";
145+
card.setSubtitle(subtitle);
146+
Assert.assertNotNull(card.getSubtitle());
147+
Assert.assertTrue(card.getSubtitle() instanceof Span);
148+
Assert.assertEquals(subtitle, ((Span) card.getSubtitle()).getText());
149+
}
150+
151+
@Test
152+
public void setStringSubtitle_setNullStringSubtitle_subtitleCleared() {
153+
card.setSubtitle("Some Subtitle");
154+
card.setSubtitle((String) null);
155+
Assert.assertNull(card.getSubtitle());
156+
}
157+
158+
@Test
159+
public void setStringSubtitle_setComponentSubtitle_stringSubtitleIsReplaced() {
160+
card.setSubtitle("Some Subtitle");
161+
var newSubtitle = new Div("Other Subtitle");
162+
card.setSubtitle(newSubtitle);
163+
Assert.assertEquals(newSubtitle, card.getSubtitle());
164+
}
165+
166+
@Test
167+
public void setComponentSubtitle_setStringSubtitle_componentSubtitleIsReplaced() {
168+
var componentSubtitle = new Div("Component Subtitle");
169+
card.setSubtitle(componentSubtitle);
170+
var stringSubtitle = "String Subtitle";
171+
card.setSubtitle(stringSubtitle);
172+
Assert.assertTrue(card.getSubtitle() instanceof Span);
173+
Assert.assertEquals(stringSubtitle,
174+
((Span) card.getSubtitle()).getText());
175+
}
176+
142177
@Test
143178
public void mediaNullByDefault() {
144179
Assert.assertNull(card.getMedia());

0 commit comments

Comments
 (0)