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 for #8 including test reproducing the problem. #13

Merged
merged 4 commits into from
Sep 1, 2019
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
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,9 @@ typings/

# compiled
dist

# I don't see this committed, so putting in ignore
package-lock.json

# vim swap files
**/*.swp
9 changes: 0 additions & 9 deletions license

This file was deleted.

9 changes: 9 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class EllipisWithTooltip extends React.Component {
this.state = {
hasOverflowingChildren: false,
text: undefined,
prevPropsChildren: props.children,
};
this.updateOverflow = this.updateOverflow.bind(this);
}
Expand All @@ -35,6 +36,14 @@ class EllipisWithTooltip extends React.Component {
}
}

static getDerivedStateFromProps(props, state) {
if (props.children === state.prevPropsChildren) return null;
return {
hasOverflowingChildren: false,
prevPropsChildren: props.children,
};
}

render() {
const { hasOverflowingChildren, text } = this.state;
const {
Expand Down
37 changes: 37 additions & 0 deletions src/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,41 @@ describe('Ellipsis', () => {

expect(toJson(wrapper)).toMatchSnapshot();
});

it('should update text when props change', () => {
const wrapper = setup('this is some long text');
let domElement = wrapper.find('#a div').getDOMNode();

expect(wrapper.state().hasOverflowingChildren).toEqual(false);
Object.defineProperty(domElement, 'clientWidth', {
get() {
return 300;
},
});
Object.defineProperty(domElement, 'scrollWidth', {
get() {
return 400;
},
});
wrapper.find('#a div').simulate('mouseEnter');
expect(wrapper.state().hasOverflowingChildren).toEqual(true);

expect(wrapper.state().text).toEqual('this is some long text');
wrapper.setProps({ children: 'this changed' });
expect(wrapper.state().hasOverflowingChildren).toEqual(false);
domElement = wrapper.find('#a div').getDOMNode();
Object.defineProperty(domElement, 'clientWidth', {
get() {
return 300;
},
});
Object.defineProperty(domElement, 'scrollWidth', {
get() {
return 400;
},
});
wrapper.find('#a div').simulate('mouseEnter');
expect(wrapper.state().hasOverflowingChildren).toEqual(true);
expect(wrapper.state().text).toEqual('this changed');
});
});