Skip to content

Commit

Permalink
Force resize calcs based on new optional width prop
Browse files Browse the repository at this point in the history
  • Loading branch information
bongione authored and pablosichert committed Aug 13, 2018
1 parent ff64118 commit bd6791a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/Truncate.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,16 @@ export default class Truncate extends Component {
PropTypes.number
]),
trimWhitespace: PropTypes.bool,
width: PropTypes.number,
onTruncate: PropTypes.func
};

static defaultProps = {
children: '',
ellipsis: '…',
lines: 1,
trimWhitespace: false
trimWhitespace: false,
width: 0
};

state = {};
Expand Down Expand Up @@ -62,6 +64,11 @@ export default class Truncate extends Component {
if (this.props.children !== prevProps.children) {
this.forceUpdate();
}

// If the width prop has changed, recalculate size of contents
if (this.props.width !== prevProps.width) {
this.calcTargetWidth();
}
}

componentWillUnmount() {
Expand Down
18 changes: 18 additions & 0 deletions test/Truncate.js
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,24 @@ describe('<Truncate />', () => {
}
});

it('should recalculate when the width property changes', () => {
const calcTargetWidth = sinon.spy(Truncate.prototype, 'calcTargetWidth');

try {
const container = document.createElement('div');

render(<Truncate width={100} />, container);

const numCalled = calcTargetWidth.callCount;

render(<Truncate width={200} />, container);

expect(calcTargetWidth, 'was called times', numCalled + 1);
} finally {
Truncate.prototype.calcTargetWidth.restore();
}
});

it('should clean up all event listeners on window when unmounting', () => {
const events = new Set();

Expand Down

0 comments on commit bd6791a

Please sign in to comment.