-
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
[OverflowList] add onOverflow callback prop #2975
Conversation
added some state fields to support this logic. complex stuff as it can happen over several renders.
import { IResizeEntry, ResizeSensor } from "../resize-sensor/resizeSensor"; | ||
|
||
/** @internal - do not expose this type */ |
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.
thoughts on doing this to reduce the exported surface area? it excludes it from the .d.ts
files and required marking the state field similarly (line 84).
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.
🆒
more onOverflow tests & helperPreviews: documentation | landing | table |
this.repartition(false); | ||
if (this.state.direction === OverflowDirection.NONE && this.state.direction !== prevState.direction) { | ||
if ( |
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 could be just one if
*/ | ||
direction: OverflowDirection; | ||
/** Remember the last completed overflow to dedupe `onOverflow` calls during smooth resizing. */ | ||
lastOverflow?: T[]; |
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.
if you make this non-optional you have one fewer check to make below. You don't actually treat the undefined
case in a special way.
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.
undefined is different from empty list. the distinction is actually useful.
more docs, combine ifPreviews: documentation | landing | table |
2 similar comments
more docs, combine ifPreviews: documentation | landing | table |
more docs, combine ifPreviews: documentation | landing | table |
reset lastOverflow with new propsPreviews: documentation | landing | table |
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.
// shrink operation does not require checking last items | ||
(prevState.direction === OverflowDirection.SHRINK || | ||
// if current overflow is different from last completed state | ||
this.state.lastOverflow == null || |
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.
Could we do === undefined
, or is == null
just idiomatic for the codebase?
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.
idiomatic
(prevState.direction === OverflowDirection.SHRINK || | ||
// if current overflow is different from last completed state | ||
this.state.lastOverflow == null || | ||
this.state.lastOverflow.some((x, i) => x !== this.state.overflow[i])) |
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.
Could we just check lastOverflow.length !== overflow.length
to save a little time? Is it possible for a change in overflow to replace items without changing the number of items displayed?
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.
yes it's completely possible for that to happen, though that should be handled by the check in componentWillReceiveProps
. so maybe this check can be made simpler. i'll play with it some more.
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.
worked flawlessly!
import { IResizeEntry, ResizeSensor } from "../resize-sensor/resizeSensor"; | ||
|
||
/** @internal - do not expose this type */ |
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.
🆒
simplify lastOverflow check - just lengthPreviews: documentation | landing | table |
// if a resize operation has just completed (transition to NONE) | ||
direction === OverflowDirection.NONE && | ||
direction !== prevState.direction && | ||
(lastOverflowCount == null || overflow.length !== lastOverflowCount) |
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 never set to null
or undefined
, so the first part will never be true
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.
oh yeah i removed the nullability!
@@ -85,6 +107,8 @@ export class OverflowList<T> extends React.PureComponent<IOverflowListProps<T>, | |||
} | |||
|
|||
public state: IOverflowListState<T> = { | |||
direction: OverflowDirection.NONE, | |||
lastOverflowCount: -1, |
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 -1
? The overflow initially has 0 elements.
@@ -85,6 +107,8 @@ export class OverflowList<T> extends React.PureComponent<IOverflowListProps<T>, | |||
} | |||
|
|||
public state: IOverflowListState<T> = { | |||
direction: OverflowDirection.NONE, |
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.
We initially call repartition in shrinking mode, so maybe this should be SHRINK
(if you want onOverflow
to be called initially, even when the overflow is empty. If you don't, just keep NONE
)
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.
i changed to SHRINK
but it was not called initially in tests, so just sticking with this.
lastOverflowCount defaults to 0Previews: documentation | landing | table |
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.setState({ | ||
this.setState(state => ({ | ||
direction: OverflowDirection.GROW, | ||
// store last overflow if this is the beginning of a resize (for check in `update()`). |
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.
can you call this componentDidUpdate
or remove the backticks and parens so it's clearer what you mean?
fix name in commentPreviews: documentation | landing | table |
fix name in commentPreviews: documentation | landing | table |
Changes proposed in this pull request:
OverflowList
onOverflow
callback prop which receives the overflowed items.Reviewers should focus on:
Screenshot