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

[Input] Fix infinite rendering loop #11159

Merged
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
9 changes: 6 additions & 3 deletions packages/material-ui/src/Input/Textarea.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,14 @@ class Textarea extends React.Component {
this.syncHeightWithShadow();
}, 166); // Corresponds to 10 frames at 60 Hz.

syncHeightWithShadow(props = this.props) {
syncHeightWithShadow() {
const props = this.props;
if (!this.shadow || !this.singlelineShadow) {
return;
}

// The component is controlled, we need to update the shallow value.
if (typeof this.props.value !== 'undefined') {
if (typeof props.value !== 'undefined') {
this.shadow.value = props.value == null ? '' : String(props.value);
}

Expand All @@ -103,7 +104,9 @@ class Textarea extends React.Component {

newHeight = Math.max(newHeight, lineHeight);

if (this.state.height !== newHeight) {
// Need a large enough different to update the height.
// This prevents infinite rendering loop.
if (Math.abs(this.state.height - newHeight) > 1) {
this.setState({
height: newHeight,
});
Expand Down
4 changes: 2 additions & 2 deletions packages/material-ui/src/Input/Textarea.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,10 @@ describe('<Textarea />', () => {

it('should respect the rowsMax property', () => {
const instance = wrapper.instance();
const rowsMax = 2;
const rowsMax = 4;
const lineHeight = 19;
instance.singlelineShadow = { scrollHeight: lineHeight };
instance.shadow = { scrollHeight: lineHeight * 3 };
instance.shadow = { scrollHeight: lineHeight * 5 };
wrapper.setProps({ rowsMax });
assert.strictEqual(wrapper.state().height, lineHeight * rowsMax);
});
Expand Down