Fix ReadMore component maxLines prop reactivity (https://github.com/woocommerce/woocommerce-blocks/pull/5673)

when changing property value of maxLines component would not
update until remounted or reloaded with new settings
This commit is contained in:
Tomasz Tunik 2022-01-31 13:56:19 +01:00 committed by GitHub
parent c7aa3a75b9
commit 54ca9e16b0
1 changed files with 23 additions and 0 deletions

View File

@ -111,6 +111,29 @@ class ReadMore extends Component< ReadMoreProps, ReadMoreState > {
}
componentDidMount(): void {
this.setSummary();
}
componentDidUpdate( prevProps: ReadMoreProps ): void {
if (
prevProps.maxLines !== this.props.maxLines ||
prevProps.children !== this.props.children
) {
/**
* if maxLines or content changed we need to reset the state to
* initial values so that summary can be calculated again
*/
this.setState(
{
clampEnabled: null,
summary: '.',
},
this.setSummary
);
}
}
setSummary(): void {
if ( this.props.children ) {
const { maxLines, ellipsis } = this.props;