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

Fix strict mode warnings for Collapse component #1992

Merged
merged 4 commits into from
Jul 26, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 19 additions & 11 deletions src/Collapse.js
Expand Up @@ -29,11 +29,7 @@ const propTypes = {
navbar: PropTypes.bool,
/** Change underlying component's CSS base class name */
cssModule: PropTypes.object,
innerRef: PropTypes.oneOfType([
PropTypes.func,
PropTypes.string,
PropTypes.object,
]),
innerRef: PropTypes.shape({ current: PropTypes.object }),
};

const defaultProps = {
Expand Down Expand Up @@ -66,40 +62,51 @@ class Collapse extends Component {
dimension: null,
};

this.nodeRef = props.innerRef || React.createRef();

['onEntering', 'onEntered', 'onExit', 'onExiting', 'onExited'].forEach(
(name) => {
this[name] = this[name].bind(this);
},
);
}

onEntering(node, isAppearing) {
onEntering(_, isAppearing) {
const node = this.getNode();
this.setState({ dimension: this.getDimension(node) });
this.props.onEntering(node, isAppearing);
}

onEntered(node, isAppearing) {
onEntered(_, isAppearing) {
const node = this.getNode();
this.setState({ dimension: null });
this.props.onEntered(node, isAppearing);
}

onExit(node) {
onExit() {
const node = this.getNode();
this.setState({ dimension: this.getDimension(node) });
this.props.onExit(node);
}

onExiting(node) {
onExiting() {
const node = this.getNode();
// getting this variable triggers a reflow
const _unused = this.getDimension(node); // eslint-disable-line no-unused-vars
this.setState({ dimension: 0 });
this.props.onExiting(node);
}

onExited(node) {
onExited() {
const node = this.getNode();
this.setState({ dimension: null });
this.props.onExited(node);
}

getNode() {
return this.nodeRef.current;
}

getDimension(node) {
return this.props.horizontal ? node.scrollWidth : node.scrollHeight;
}
Expand All @@ -125,6 +132,7 @@ class Collapse extends Component {
<Transition
{...transitionProps}
in={isOpen}
nodeRef={this.nodeRef}
onEntering={this.onEntering}
onEntered={this.onEntered}
onExit={this.onExit}
Expand All @@ -151,7 +159,7 @@ class Collapse extends Component {
{...childProps}
style={{ ...childProps.style, ...style }}
className={classes}
ref={this.props.innerRef}
ref={this.nodeRef}
>
{children}
</Tag>
Expand Down
11 changes: 11 additions & 0 deletions src/__tests__/Collapse.spec.js
Expand Up @@ -32,6 +32,17 @@ describe('Collapse', () => {
expect(wrapper.find('p').text()).toBe('hello');
});

it('works with strict mode', () => {
const spy = jest.spyOn(console, 'error');
wrapper = mount(
<React.StrictMode>
<Collapse />
</React.StrictMode>,
);
expect(wrapper.instance()).toBeTruthy();
expect(spy).not.toHaveBeenCalled();
});

it('should have default isOpen value', () => {
wrapper = shallow(<Collapse />);
expect(wrapper.instance().props.isOpen).toEqual(false);
Expand Down