-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
[Table] Pass parent width/height props to TruncatedFormat #1550
Conversation
packages/table/src/cell/cell.tsx
Outdated
const modifiedChildren = React.Children.map(this.props.children, (child) => { | ||
if (React.isValidElement(child)) { | ||
return React.cloneElement(child as React.ReactElement<any>, | ||
{parentCellHeight: style.height, parentCellWidth: style.width}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: add spaces: { parentCellHeight: style.height, parentCellWidth: style.width }
packages/table/src/cell/cell.tsx
Outdated
@@ -102,7 +102,15 @@ export class Cell extends React.Component<ICellProps, {}> { | |||
}, | |||
); | |||
|
|||
const content = <div className={textClasses}>{this.props.children}</div>; | |||
const modifiedChildren = React.Children.map(this.props.children, (child) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add comment somewhere explaining that parentCell{Height,Width}
won't be used in the Cell
except in shouldComponentUpdate
|| actualContentWidth > containerWidth | ||
|| actualContentHeight > containerHeight; | ||
|
||
if (isTruncated !== shouldTruncate) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Still need this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@gscshoyru I think we can do away with this check now that this is a pure component.
Check for null to fix testsPreview: documentation | table |
Do we know yet if this helps perf? Or just an exercise still? |
@@ -132,6 +136,11 @@ export class TruncatedFormat extends React.Component<ITruncatedFormatProps, ITru | |||
this.setTruncationState(); | |||
} | |||
|
|||
public shouldComponentUpdate(nextProps: ITruncatedFormatProps, nextState: ITruncatedFormatState) { | |||
return !Utils.shallowCompareKeys(this.props, nextProps) | |||
|| !Utils.shallowCompareKeys(this.state, nextState); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why not an @PureRender
annotation on the class like we usually do?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That... probably should work? @cmslewis ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep do that!
@@ -33,6 +34,9 @@ export interface ITruncatedFormatProps extends IProps { | |||
*/ | |||
detectTruncation?: boolean; | |||
|
|||
parentCellHeight?: string; | |||
parentCellWidth?: string; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
docs? are these props even necessary here? you cast to React.ReactElement<any>
above
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See comments on PR -- this is explicitly necessary for the shouldComponentUpdate -- we don't actually use them, but we need them to keep from rerendering and remeasuring every time, without knowing if the size has changed.
packages/table/src/cell/cell.tsx
Outdated
const content = <div className={textClasses}>{this.props.children}</div>; | ||
const modifiedChildren = React.Children.map(this.props.children, (child) => { | ||
if (React.isValidElement(child)) { | ||
return React.cloneElement(child as React.ReactElement<any>, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use JSX.Element
in place of React.ReactElement<any>
if you don't need props type
packages/table/src/cell/cell.tsx
Outdated
@@ -102,7 +102,15 @@ export class Cell extends React.Component<ICellProps, {}> { | |||
}, | |||
); | |||
|
|||
const content = <div className={textClasses}>{this.props.children}</div>; | |||
const modifiedChildren = React.Children.map(this.props.children, (child) => { | |||
if (React.isValidElement(child)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hmm i didn't know this method existed. is it necessary? does it actually avoid errors? my suspicion is no, and you don't need it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's a typeguard. Without it, child could be react element, or could be text. Hence the return, otherwise.
Change content-width delta 1 26pxPreview: documentation | table |
|
||
// this is where we get our savings from the injected parent-size props. | ||
// if they haven't changed between updates, then no need to remeasure! | ||
if (!detectTruncation |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doesn't quite work -- the parent heights and widths may not have changed, but the content could have. You still need to remeasure if anything changed. We can't really get savings this way.
@@ -14,7 +14,7 @@ import * as Classes from "../../common/classes"; | |||
|
|||
// amount in pixels that the content div width changes when truncated vs when | |||
// not truncated. Note: could be modified by styles | |||
const CONTENT_DIV_WIDTH_DELTA = 25; | |||
const CONTENT_DIV_WIDTH_DELTA = 26; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is also wrong, it comes from the right property on the div. There's a comment in sass, I'll put one here too.
Undo some of Chris's stuff (sorry Chris!), address commentsPreview: documentation | table |
@@ -152,21 +165,38 @@ export class TruncatedFormat extends React.Component<ITruncatedFormatProps, ITru | |||
} | |||
|
|||
private setTruncationState() { | |||
if (!this.props.detectTruncation || this.props.showPopover !== TruncatedPopoverMode.WHEN_TRUNCATED) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Whoops, @gscshoyru we'll still want these checks
|| actualContentWidth > containerWidth | ||
|| actualContentHeight > containerHeight; | ||
|
||
if (isTruncated !== shouldTruncate) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@gscshoyru I think we can do away with this check now that this is a pure component.
Remove no longer necessary if statementPreview: documentation | table |
const modifiedChildren = React.Children.map(this.props.children, (child) => { | ||
if (style != null && React.isValidElement(child)) { | ||
return React.cloneElement(child as React.ReactElement<any>, | ||
{parentCellHeight: style.height, parentCellWidth: style.width}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: proper format would look something like this. but that's why we have prettier
incoming.
return React.cloneElement(child as React.ReactElement<any>, {
parentCellHeight: style.height,
parentCellWidth: style.width,
});
Add back checkPreview: documentation | table |
Changes proposed in this pull request:
Pass cell height and width to child, for use in shouldComponentUpdate
The props aren't used by the element, but its presence means faster redraw for cells with truncated formats.The props are used by the element to avoid re-measuring if the parent size didn't change across updates.Reviewers should focus on:
This is on top of another in-progress PR -- ignore the first two commits.
I'm cloning all the cell children -- does this make sense?