Skip to content

Commit 1148417

Browse files
yegor-babiystepovat
authored andcommitted
Throws warning: List doesn't get rendered when items are loaded async and select component is hidden (#145)
* Throws warning: List doesn't get rendered when items are loaded async and select component is hidden * fix test logic
1 parent 00043df commit 1148417

File tree

5 files changed

+37
-31
lines changed

5 files changed

+37
-31
lines changed

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,6 @@
9494
"classnames": "2.2.5",
9595
"jss": "^9.8.7",
9696
"prop-types": "^15.6.2",
97-
"react-height": "^3.0.0",
9897
"react-icons": "^2.2.7",
9998
"react-jss": "^8.6.1",
10099
"react-overflow-tooltip": "^2.0.1",

src/components/with_responsive_height.js

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import React, { PureComponent } from "react";
22
import PropTypes from "prop-types";
3-
import ReactHeight from "react-height";
43

54
const DEFAULT_HEIGHT = 400;
65

@@ -23,20 +22,37 @@ const withResponsiveHeight = WrappedComponent =>
2322
height: DEFAULT_HEIGHT
2423
};
2524

26-
changeHeight = height => {
25+
getClientHeight() {
26+
return this.divRef.clientHeight;
27+
}
28+
29+
componentDidMount() {
30+
const clientHeight = this.getClientHeight();
31+
this.changeHeight(clientHeight);
32+
}
33+
34+
componentDidUpdate() {
35+
const { height } = this.state;
36+
const clientHeight = this.getClientHeight();
37+
if (clientHeight && height !== clientHeight) {
38+
this.changeHeight(clientHeight);
39+
}
40+
}
41+
42+
changeHeight(height) {
2743
this.setState({ height: height - 2 });
28-
};
44+
}
2945

3046
render() {
3147
const { responsiveHeight, ...props } = this.props;
3248
const { height } = this.state;
3349
return (
34-
<ReactHeight
50+
<div
3551
style={{ height: responsiveHeight }}
36-
onHeightReady={this.changeHeight}
52+
ref={element => (this.divRef = element)}
3753
>
3854
<WrappedComponent {...props} height={height} />
39-
</ReactHeight>
55+
</div>
4056
);
4157
}
4258
};

tests/components/__snapshots__/multi_select.spec.js.snap

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4346,11 +4346,7 @@ exports[`MultiSelect Snapshots with generateClassName 1`] = `
43464346
`;
43474347

43484348
exports[`MultiSelect Snapshots with responsiveHeight 1`] = `
4349-
<ReactHeight
4350-
dirty={true}
4351-
getElementHeight={[Function]}
4352-
hidden={false}
4353-
onHeightReady={[Function]}
4349+
<div
43544350
style={
43554351
Object {
43564352
"height": "70%",
@@ -4363,15 +4359,11 @@ exports[`MultiSelect Snapshots with responsiveHeight 1`] = `
43634359
items={Array []}
43644360
selectedItems={Array []}
43654361
/>
4366-
</ReactHeight>
4362+
</div>
43674363
`;
43684364

43694365
exports[`MultiSelect Snapshots without responsiveHeight 1`] = `
4370-
<ReactHeight
4371-
dirty={true}
4372-
getElementHeight={[Function]}
4373-
hidden={false}
4374-
onHeightReady={[Function]}
4366+
<div
43754367
style={
43764368
Object {
43774369
"height": "400px",
@@ -4384,7 +4376,7 @@ exports[`MultiSelect Snapshots without responsiveHeight 1`] = `
43844376
items={Array []}
43854377
selectedItems={Array []}
43864378
/>
4387-
</ReactHeight>
4379+
</div>
43884380
`;
43894381

43904382
exports[`MultiSelect passes properties can remove search 1`] = `

tests/components/multi_select.spec.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,17 @@ describe("MultiSelect", () => {
248248
);
249249
expect(tree).toMatchSnapshot();
250250
});
251+
252+
test("changeHeight will not be called", () => {
253+
const changeHeight = jest.fn();
254+
const getClientHeight = jest.fn(() => 44);
255+
const tree = mount(<MultiSelectWithState />);
256+
const instance = tree.instance();
257+
instance.getClientHeight = getClientHeight;
258+
instance.changeHeight = changeHeight;
259+
instance.componentDidUpdate();
260+
expect(changeHeight).toHaveBeenCalled();
261+
});
251262
});
252263

253264
describe("passes properties", () => {

yarn.lock

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8607,12 +8607,6 @@ promise@^7.1.1:
86078607
dependencies:
86088608
asap "~2.0.3"
86098609

8610-
8611-
version "15.5.8"
8612-
resolved "https://registry.yarnpkg.com/prop-types/-/prop-types-15.5.8.tgz#6b7b2e141083be38c8595aa51fc55775c7199394"
8613-
dependencies:
8614-
fbjs "^0.8.9"
8615-
86168610
prop-types@^15.5.10, prop-types@^15.5.4, prop-types@^15.5.7, prop-types@^15.5.8, prop-types@^15.5.9, prop-types@^15.6.0, prop-types@^15.6.1, prop-types@^15.6.2:
86178611
version "15.6.2"
86188612
resolved "https://registry.yarnpkg.com/prop-types/-/prop-types-15.6.2.tgz#05d5ca77b4453e985d60fc7ff8c859094a497102"
@@ -8934,12 +8928,6 @@ react-fuzzy@^0.5.2:
89348928
fuse.js "^3.0.1"
89358929
prop-types "^15.5.9"
89368930

8937-
react-height@^3.0.0:
8938-
version "3.0.0"
8939-
resolved "https://registry.yarnpkg.com/react-height/-/react-height-3.0.0.tgz#fae322f9da64d3e9e25536f26b77c73954261524"
8940-
dependencies:
8941-
prop-types "15.5.8"
8942-
89438931
react-html-attributes@^1.4.2:
89448932
version "1.4.3"
89458933
resolved "https://registry.yarnpkg.com/react-html-attributes/-/react-html-attributes-1.4.3.tgz#8c36c35fce6b750938d286af428ed1da7625186e"

0 commit comments

Comments
 (0)