-
Notifications
You must be signed in to change notification settings - Fork 24.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Summary: - Updated SwipeableListView to be much more performant by checking `rowHasChanged` more vigorously - New `SwipeableListViewDataSource` used to mask implementation details from caller Reviewed By: fkgozali Differential Revision: D3272172 fbshipit-source-id: 02f66ed7fce7d587118ad7d82b20f8e78db44b7b
- Loading branch information
Showing
4 changed files
with
231 additions
and
125 deletions.
There are no files selected for viewing
115 changes: 115 additions & 0 deletions
115
Libraries/Experimental/SwipeableRow/SwipeableListView.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
/** | ||
* Copyright (c) 2013-present, Facebook, Inc. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
* | ||
* The examples provided by Facebook are for non-commercial testing and | ||
* evaluation purposes only. | ||
* | ||
* Facebook reserves all rights not expressly granted. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS | ||
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NON INFRINGEMENT. IN NO EVENT SHALL | ||
* FACEBOOK BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN | ||
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | ||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * | ||
* | ||
* @providesModule SwipeableListView | ||
* @flow | ||
*/ | ||
'use strict'; | ||
|
||
const ListView = require('ListView'); | ||
const React = require('React'); | ||
const SwipeableListViewDataSource = require('SwipeableListViewDataSource'); | ||
const SwipeableRow = require('SwipeableRow'); | ||
|
||
const {PropTypes} = React; | ||
|
||
/** | ||
* A container component that renders multiple SwipeableRow's in a ListView | ||
* implementation. | ||
*/ | ||
const SwipeableListView = React.createClass({ | ||
statics: { | ||
getNewDataSource(): Object { | ||
return new SwipeableListViewDataSource({ | ||
getRowData: (data, sectionID, rowID) => data[rowID], | ||
This comment has been minimized.
Sorry, something went wrong. |
||
getSectionHeaderData: (data, sectionID) => data[sectionID], | ||
sectionHeaderHasChanged: (s1, s2) => s1 !== s2, | ||
rowHasChanged: (row1, row2) => row1 !== row2, | ||
}); | ||
}, | ||
}, | ||
|
||
propTypes: { | ||
dataSource: PropTypes.object.isRequired, // SwipeableListViewDataSource | ||
maxSwipeDistance: PropTypes.number, | ||
// Callback method to render the swipeable view | ||
renderRow: PropTypes.func.isRequired, | ||
// Callback method to render the view that will be unveiled on swipe | ||
renderQuickActions: PropTypes.func.isRequired, | ||
}, | ||
|
||
getDefaultProps(): Object { | ||
return { | ||
renderQuickActions: () => null, | ||
}; | ||
}, | ||
|
||
getInitialState(): Object { | ||
return { | ||
dataSource: this.props.dataSource.getDataSource(), | ||
}; | ||
}, | ||
|
||
componentWillReceiveProps(nextProps: Object): void { | ||
if ('dataSource' in nextProps && this.state.dataSource !== nextProps.dataSource) { | ||
this.setState({ | ||
dataSource: nextProps.dataSource.getDataSource(), | ||
}); | ||
} | ||
}, | ||
|
||
render(): ReactElement { | ||
return ( | ||
<ListView | ||
{...this.props} | ||
dataSource={this.state.dataSource} | ||
renderRow={this._renderRow} | ||
/> | ||
); | ||
}, | ||
|
||
_renderRow(rowData: Object, sectionID: string, rowID: string): ReactElement { | ||
const slideoutView = this.props.renderQuickActions(rowData, sectionID, rowID); | ||
|
||
// If renderRowSlideout is unspecified or returns falsey, don't allow swipe | ||
if (!slideoutView) { | ||
return this.props.renderRow(rowData, sectionID, rowID); | ||
} | ||
|
||
return ( | ||
<SwipeableRow | ||
slideoutView={slideoutView} | ||
isOpen={rowData.id === this.props.dataSource.getOpenRowID()} | ||
maxSwipeDistance={this.props.maxSwipeDistance} | ||
key={rowID} | ||
onOpen={() => this._onOpen(rowData.id)}> | ||
{this.props.renderRow(rowData, sectionID, rowID)} | ||
</SwipeableRow> | ||
); | ||
}, | ||
|
||
_onOpen(rowID: string): void { | ||
this.setState({ | ||
dataSource: this.props.dataSource.setOpenRowID(rowID), | ||
}); | ||
}, | ||
}); | ||
|
||
module.exports = SwipeableListView; |
103 changes: 103 additions & 0 deletions
103
Libraries/Experimental/SwipeableRow/SwipeableListViewDataSource.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
/** | ||
* Copyright (c) 2013-present, Facebook, Inc. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
* | ||
* The examples provided by Facebook are for non-commercial testing and | ||
* evaluation purposes only. | ||
* | ||
* Facebook reserves all rights not expressly granted. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS | ||
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NON INFRINGEMENT. IN NO EVENT SHALL | ||
* FACEBOOK BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN | ||
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | ||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * | ||
* | ||
* @providesModule SwipeableListViewDataSource | ||
*/ | ||
'use strict'; | ||
|
||
const ListViewDataSource = require('ListViewDataSource'); | ||
|
||
/** | ||
* Data source wrapper around ListViewDataSource to allow for tracking of | ||
* which row is swiped open and close opened row(s) when another row is swiped | ||
* open. | ||
* | ||
* See https://github.com/facebook/react-native/pull/5602 for why | ||
* ListViewDataSource is not subclassed. | ||
*/ | ||
class SwipeableListViewDataSource { | ||
_previousOpenRowID: string; | ||
_openRowID: string; | ||
|
||
_dataBlob: any; | ||
_dataSource: ListViewDataSource; | ||
|
||
rowIdentities: Array<Array<string>>; | ||
sectionIdentities: Array<string>; | ||
|
||
constructor(params: Object) { | ||
this._dataSource = new ListViewDataSource({ | ||
getRowData: params.getRowData, | ||
getSectionHeaderData: params.getSectionHeaderData, | ||
rowHasChanged: (row1, row2) => { | ||
/** | ||
* Row needs to be re-rendered if its swiped open/close status is | ||
* changed, or its data blob changed. | ||
*/ | ||
return ( | ||
(row1.id !== this._previousOpenRowID && row2.id === this._openRowID) || | ||
(row1.id === this._previousOpenRowID && row2.id !== this._openRowID) || | ||
params.rowHasChanged(row1, row2) | ||
); | ||
}, | ||
sectionHeaderHasChanged: params.sectionHeaderHasChanged, | ||
}); | ||
} | ||
|
||
cloneWithRowsAndSections( | ||
dataBlob: any, | ||
sectionIdentities: ?Array<string>, | ||
rowIdentities: ?Array<Array<string>> | ||
): SwipeableListViewDataSource { | ||
this._dataSource = this._dataSource.cloneWithRowsAndSections( | ||
dataBlob, | ||
sectionIdentities, | ||
rowIdentities | ||
); | ||
|
||
this._dataBlob = dataBlob; | ||
this.rowIdentities = this._dataSource.rowIdentities; | ||
this.sectionIdentities = this._dataSource.sectionIdentities; | ||
|
||
return this; | ||
} | ||
|
||
// For the actual ListView to use | ||
getDataSource(): ListViewDataSource { | ||
return this._dataSource; | ||
} | ||
|
||
getOpenRowID(): ?string { | ||
return this._openRowID; | ||
} | ||
|
||
setOpenRowID(rowID: string): ListViewDataSource { | ||
this._previousOpenRowID = this._openRowID; | ||
this._openRowID = rowID; | ||
|
||
return this._dataSource.cloneWithRowsAndSections( | ||
this._dataBlob, | ||
this.sectionIdentities, | ||
this.rowIdentities | ||
); | ||
} | ||
} | ||
|
||
module.exports = SwipeableListViewDataSource; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
117 changes: 0 additions & 117 deletions
117
Libraries/Experimental/SwipeableRow/SwipeableRowListView.js
This file was deleted.
Oops, something went wrong.
@fred2028 based on how
ListViewDataSource
treats section and row IDs shouldn't this default todata[sectionID][rowID]
instead?