Skip to content
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

feat: configure min and max rows in text area #6828

Merged
merged 5 commits into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Copy link
Contributor Author

@sissbruecker sissbruecker Nov 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Feels a bit strange that one property has a default value and does not allow null, while the other one doesn't have a default and does allow null. Though that mirrors what we have in the WC.

I guess we could change minRows to be nullable as well and document that it then uses 2 rows by default, though I'm not sure if that's better than having this inconsistency. Any opinions?

Copy link
Contributor

@vursen vursen Nov 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess Integer.Max_VALUE or a negative number could be an alternative to null, but I would personally prefer null.

Copy link
Contributor

@vursen vursen Nov 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably not very relevant either, I would assume the min/max rows configuration to usually be static.

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"));
}
}