From bd6791a9f769ad8050e59cd0811c260267e079b2 Mon Sep 17 00:00:00 2001 From: Paolo Ferdinando Bongiovanni Date: Thu, 7 Dec 2017 10:28:00 +0000 Subject: [PATCH] Force resize calcs based on new optional width prop --- src/Truncate.js | 9 ++++++++- test/Truncate.js | 18 ++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/Truncate.js b/src/Truncate.js index f4ce134f..d20d78dc 100644 --- a/src/Truncate.js +++ b/src/Truncate.js @@ -10,6 +10,7 @@ export default class Truncate extends Component { PropTypes.number ]), trimWhitespace: PropTypes.bool, + width: PropTypes.number, onTruncate: PropTypes.func }; @@ -17,7 +18,8 @@ export default class Truncate extends Component { children: '', ellipsis: '…', lines: 1, - trimWhitespace: false + trimWhitespace: false, + width: 0 }; state = {}; @@ -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() { diff --git a/test/Truncate.js b/test/Truncate.js index 06ef4139..d44074d6 100644 --- a/test/Truncate.js +++ b/test/Truncate.js @@ -447,6 +447,24 @@ describe('', () => { } }); + it('should recalculate when the width property changes', () => { + const calcTargetWidth = sinon.spy(Truncate.prototype, 'calcTargetWidth'); + + try { + const container = document.createElement('div'); + + render(, container); + + const numCalled = calcTargetWidth.callCount; + + render(, 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();