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
[Bugfix][SwipeableFlatList] Fix SwipeableFlatList on close #16682
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,7 +35,9 @@ type SwipableListProps = { | |
type Props<ItemT> = SwipableListProps & FlatListProps<ItemT>; | ||
|
||
type State = { | ||
openRowKey: ?string, | ||
extraData: { | ||
openRowKey: ?string, | ||
} | ||
}; | ||
|
||
/** | ||
|
@@ -88,31 +90,37 @@ class SwipeableFlatList<ItemT> extends React.Component<Props<ItemT>, State> { | |
constructor(props: Props<ItemT>, context: any): void { | ||
super(props, context); | ||
this.state = { | ||
openRowKey: null, | ||
extraData: { | ||
openRowKey: null, | ||
} | ||
}; | ||
|
||
this._shouldBounceFirstRowOnMount = this.props.bounceFirstRowOnMount; | ||
|
||
this.onRefFlatList = this.onRefFlatList.bind(this); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why this change? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This code works faster than fat arrow. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @kesha-antonov totally get that -- I meant why .bind(this) in the initializer instead of using class fields (like _onScroll) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ping |
||
this._onClose = this._onClose.bind(this); | ||
} | ||
|
||
render(): React.Node { | ||
return ( | ||
<FlatList | ||
{...this.props} | ||
ref={ref => { | ||
this._flatListRef = ref; | ||
}} | ||
ref={this.onRefFlatList} | ||
onScroll={this._onScroll} | ||
renderItem={this._renderItem} | ||
extraData={this.state.extraData} | ||
/> | ||
); | ||
} | ||
|
||
_onScroll = (e): void => { | ||
onRefFlatList(_flatListRef) { | ||
this._flatListRef = _flatListRef; | ||
} | ||
|
||
_onScroll = (): void => { | ||
// Close any opens rows on ListView scroll | ||
if (this.state.openRowKey) { | ||
this.setState({ | ||
openRowKey: null, | ||
}); | ||
if (this.state.extraData.openRowKey) { | ||
this._onClose(); | ||
} | ||
|
||
this.props.onScroll && this.props.onScroll(e); | ||
|
@@ -136,10 +144,10 @@ class SwipeableFlatList<ItemT> extends React.Component<Props<ItemT>, State> { | |
return ( | ||
<SwipeableRow | ||
slideoutView={slideoutView} | ||
isOpen={key === this.state.openRowKey} | ||
isOpen={key === this.state.extraData.openRowKey} | ||
maxSwipeDistance={this._getMaxSwipeDistance(info)} | ||
onOpen={() => this._onOpen(key)} | ||
onClose={() => this._onClose(key)} | ||
onClose={this._onClose} | ||
shouldBounceOnMount={shouldBounceOnMount} | ||
onSwipeEnd={this._setListViewScrollable} | ||
onSwipeStart={this._setListViewNotScrollable}> | ||
|
@@ -175,13 +183,17 @@ class SwipeableFlatList<ItemT> extends React.Component<Props<ItemT>, State> { | |
|
||
_onOpen(key: any): void { | ||
this.setState({ | ||
openRowKey: key, | ||
extraData: { | ||
openRowKey: key, | ||
} | ||
}); | ||
} | ||
|
||
_onClose(key: any): void { | ||
_onClose(): void { | ||
this.setState({ | ||
openRowKey: null, | ||
extraData: { | ||
openRowKey: 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.
why do we need extra object inside state? isn't it simpler keep it as before but put into flat list
extraData={this.state}
?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.
@kesha-antonov
Following the example in https://facebook.github.io/react-native/docs/flatlist.html