Skip to content

Commit 890ffe3

Browse files
sissbrueckervursen
andauthored
feat: configure min and max rows in text area (#6828)
* feat: configure min and max rows in text area * Update vaadin-text-field-flow-parent/vaadin-text-field-flow/src/main/java/com/vaadin/flow/component/textfield/TextArea.java Co-authored-by: Sergey Vinogradov <[email protected]> --------- Co-authored-by: Sergey Vinogradov <[email protected]>
1 parent 96dabb7 commit 890ffe3

File tree

2 files changed

+90
-0
lines changed
  • vaadin-text-field-flow-parent/vaadin-text-field-flow/src

2 files changed

+90
-0
lines changed

vaadin-text-field-flow-parent/vaadin-text-field-flow/src/main/java/com/vaadin/flow/component/textfield/TextArea.java

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,59 @@ public String getPattern() {
399399
return getElement().getProperty("pattern");
400400
}
401401

402+
/**
403+
* The minimum number of rows to show.
404+
*
405+
* @return the minimum number of rows
406+
*/
407+
public int getMinRows() {
408+
return getElement().getProperty("minRows", 2);
409+
}
410+
411+
/**
412+
* Sets the minimum number of rows to show. Default is two rows.
413+
*
414+
* @param minRows
415+
* the minimum number of rows to show
416+
*/
417+
public void setMinRows(int minRows) {
418+
getElement().setProperty("minRows", minRows);
419+
}
420+
421+
/**
422+
* Maximum number of rows to expand to before the component starts
423+
* scrolling.
424+
*
425+
* @return the maximum number of rows, or {@code null} if the maximum has
426+
* not been set
427+
*/
428+
public Integer getMaxRows() {
429+
String maxRows = getElement().getProperty("maxRows");
430+
if (maxRows != null && !maxRows.isEmpty()) {
431+
return Integer.parseInt(maxRows);
432+
}
433+
434+
return null;
435+
}
436+
437+
/**
438+
* Sets the maximum number of rows to expand to before the component starts
439+
* scrolling. This effectively sets a max-height on the {@code input-field}
440+
* part. By default, the value is {@code null}, which means the component
441+
* grows with the content without constraints.
442+
*
443+
* @param maxRows
444+
* the maximum number of rows, or {@code null} to remove the
445+
* maximum
446+
*/
447+
public void setMaxRows(Integer maxRows) {
448+
if (maxRows != null) {
449+
getElement().setProperty("maxRows", maxRows);
450+
} else {
451+
getElement().removeProperty("maxRows");
452+
}
453+
}
454+
402455
@Override
403456
public String getEmptyValue() {
404457
return "";

vaadin-text-field-flow-parent/vaadin-text-field-flow/src/test/java/com/vaadin/flow/component/textfield/tests/TextAreaTest.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,4 +215,41 @@ public void implementsInputField() {
215215
Assert.assertTrue(
216216
field instanceof InputField<AbstractField.ComponentValueChangeEvent<TextArea, String>, String>);
217217
}
218+
219+
@Test
220+
public void getMinRows_defaultValue() {
221+
TextArea field = new TextArea();
222+
223+
Assert.assertEquals(2, field.getMinRows());
224+
}
225+
226+
@Test
227+
public void setMinRows() {
228+
TextArea field = new TextArea();
229+
field.setMinRows(5);
230+
231+
Assert.assertEquals(5, field.getMinRows());
232+
Assert.assertEquals(5, field.getElement().getProperty("minRows", 0));
233+
}
234+
235+
@Test
236+
public void getMaxRows_defaultValue() {
237+
TextArea field = new TextArea();
238+
239+
Assert.assertNull(field.getMaxRows());
240+
}
241+
242+
@Test
243+
public void setMaxRows() {
244+
TextArea field = new TextArea();
245+
field.setMaxRows(5);
246+
247+
Assert.assertEquals(5, (int) field.getMaxRows());
248+
Assert.assertEquals(5, field.getElement().getProperty("maxRows", 0));
249+
250+
field.setMaxRows(null);
251+
252+
Assert.assertNull(field.getMaxRows());
253+
Assert.assertNull(field.getElement().getProperty("maxRows"));
254+
}
218255
}

0 commit comments

Comments
 (0)