Skip to content

Commit

Permalink
[Input] Fix infinite rendering loop (#11159)
Browse files Browse the repository at this point in the history
  • Loading branch information
oliviertassinari authored Apr 26, 2018
1 parent b8bae94 commit 0da0955
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
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

0 comments on commit 0da0955

Please sign in to comment.