Skip to content

Commit

Permalink
feat: configure min and max rows in text area (#6828)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
sissbruecker and vursen authored Nov 20, 2024
1 parent 96dabb7 commit 890ffe3
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,59 @@ public String getPattern() {
return getElement().getProperty("pattern");
}

/**
* The minimum number of rows to show.
*
* @return the minimum number of rows
*/
public int getMinRows() {
return getElement().getProperty("minRows", 2);
}

/**
* Sets the minimum number of rows to show. Default is two rows.
*
* @param minRows
* the minimum number of rows to show
*/
public void setMinRows(int minRows) {
getElement().setProperty("minRows", minRows);
}

/**
* Maximum number of rows to expand to before the component starts
* scrolling.
*
* @return the maximum number of rows, or {@code null} if the maximum has
* not been set
*/
public Integer getMaxRows() {
String maxRows = getElement().getProperty("maxRows");
if (maxRows != null && !maxRows.isEmpty()) {
return Integer.parseInt(maxRows);
}

return null;
}

/**
* Sets the maximum number of rows to expand to before the component starts
* scrolling. This effectively sets a max-height on the {@code input-field}
* part. By default, the value is {@code null}, which means the component
* grows with the content without constraints.
*
* @param maxRows
* the maximum number of rows, or {@code null} to remove the
* maximum
*/
public void setMaxRows(Integer maxRows) {
if (maxRows != null) {
getElement().setProperty("maxRows", maxRows);
} else {
getElement().removeProperty("maxRows");
}
}

@Override
public String getEmptyValue() {
return "";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,4 +215,41 @@ public void implementsInputField() {
Assert.assertTrue(
field instanceof InputField<AbstractField.ComponentValueChangeEvent<TextArea, String>, String>);
}

@Test
public void getMinRows_defaultValue() {
TextArea field = new TextArea();

Assert.assertEquals(2, field.getMinRows());
}

@Test
public void setMinRows() {
TextArea field = new TextArea();
field.setMinRows(5);

Assert.assertEquals(5, field.getMinRows());
Assert.assertEquals(5, field.getElement().getProperty("minRows", 0));
}

@Test
public void getMaxRows_defaultValue() {
TextArea field = new TextArea();

Assert.assertNull(field.getMaxRows());
}

@Test
public void setMaxRows() {
TextArea field = new TextArea();
field.setMaxRows(5);

Assert.assertEquals(5, (int) field.getMaxRows());
Assert.assertEquals(5, field.getElement().getProperty("maxRows", 0));

field.setMaxRows(null);

Assert.assertNull(field.getMaxRows());
Assert.assertNull(field.getElement().getProperty("maxRows"));
}
}

0 comments on commit 890ffe3

Please sign in to comment.