2016-05-11 07:24:08 +00:00
|
|
|
/**
|
2017-03-24 21:36:07 +00:00
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
2016-05-11 07:24:08 +00:00
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* @providesModule SwipeableListView
|
|
|
|
* @flow
|
|
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const ListView = require('ListView');
|
|
|
|
const React = require('React');
|
|
|
|
const SwipeableListViewDataSource = require('SwipeableListViewDataSource');
|
|
|
|
const SwipeableRow = require('SwipeableRow');
|
|
|
|
|
|
|
|
const {PropTypes} = React;
|
|
|
|
|
2017-04-05 17:16:30 +00:00
|
|
|
type DefaultProps = {
|
|
|
|
bounceFirstRowOnMount: boolean,
|
|
|
|
renderQuickActions: Function,
|
|
|
|
};
|
|
|
|
|
2016-09-22 16:48:49 +00:00
|
|
|
type Props = {
|
|
|
|
bounceFirstRowOnMount: boolean,
|
|
|
|
dataSource: SwipeableListViewDataSource,
|
2016-11-14 19:56:09 +00:00
|
|
|
maxSwipeDistance: number | (rowData: any, sectionID: string, rowID: string) => number,
|
2017-02-23 23:00:41 +00:00
|
|
|
onScroll?: ?Function,
|
2016-09-22 16:48:49 +00:00
|
|
|
renderRow: Function,
|
|
|
|
renderQuickActions: Function,
|
|
|
|
};
|
|
|
|
|
|
|
|
type State = {
|
|
|
|
dataSource: Object,
|
|
|
|
};
|
|
|
|
|
2016-05-11 07:24:08 +00:00
|
|
|
/**
|
|
|
|
* A container component that renders multiple SwipeableRow's in a ListView
|
2016-06-07 18:55:38 +00:00
|
|
|
* implementation. This is designed to be a drop-in replacement for the
|
|
|
|
* standard React Native `ListView`, so use it as if it were a ListView, but
|
|
|
|
* with extra props, i.e.
|
|
|
|
*
|
|
|
|
* let ds = SwipeableListView.getNewDataSource();
|
|
|
|
* ds.cloneWithRowsAndSections(dataBlob, ?sectionIDs, ?rowIDs);
|
|
|
|
* // ..
|
|
|
|
* <SwipeableListView renderRow={..} renderQuickActions={..} {..ListView props} />
|
|
|
|
*
|
|
|
|
* SwipeableRow can be used independently of this component, but the main
|
|
|
|
* benefit of using this component is
|
|
|
|
*
|
|
|
|
* - It ensures that at most 1 row is swiped open (auto closes others)
|
|
|
|
* - It can bounce the 1st row of the list so users know it's swipeable
|
|
|
|
* - More to come
|
2016-05-11 07:24:08 +00:00
|
|
|
*/
|
2017-04-05 17:16:30 +00:00
|
|
|
class SwipeableListView extends React.Component<DefaultProps, Props, State> {
|
2016-09-22 16:48:49 +00:00
|
|
|
props: Props;
|
|
|
|
state: State;
|
|
|
|
|
2016-10-16 11:11:59 +00:00
|
|
|
_listViewRef: ?React.Element<any> = null;
|
2016-09-22 16:48:49 +00:00
|
|
|
_shouldBounceFirstRowOnMount: boolean = false;
|
2016-07-26 08:00:02 +00:00
|
|
|
|
|
|
|
static getNewDataSource(): Object {
|
|
|
|
return new SwipeableListViewDataSource({
|
|
|
|
getRowData: (data, sectionID, rowID) => data[sectionID][rowID],
|
|
|
|
getSectionHeaderData: (data, sectionID) => data[sectionID],
|
|
|
|
rowHasChanged: (row1, row2) => row1 !== row2,
|
2016-09-22 16:48:49 +00:00
|
|
|
sectionHeaderHasChanged: (s1, s2) => s1 !== s2,
|
2016-07-26 08:00:02 +00:00
|
|
|
});
|
|
|
|
}
|
2016-05-12 18:48:39 +00:00
|
|
|
|
2016-07-26 08:00:02 +00:00
|
|
|
static propTypes = {
|
2016-06-07 18:55:38 +00:00
|
|
|
/**
|
|
|
|
* To alert the user that swiping is possible, the first row can bounce
|
|
|
|
* on component mount.
|
|
|
|
*/
|
2016-06-07 16:07:30 +00:00
|
|
|
bounceFirstRowOnMount: PropTypes.bool.isRequired,
|
2016-06-07 18:55:38 +00:00
|
|
|
/**
|
|
|
|
* Use `SwipeableListView.getNewDataSource()` to get a data source to use,
|
|
|
|
* then use it just like you would a normal ListView data source
|
|
|
|
*/
|
2016-05-17 03:21:47 +00:00
|
|
|
dataSource: PropTypes.instanceOf(SwipeableListViewDataSource).isRequired,
|
2016-06-07 18:55:38 +00:00
|
|
|
// Maximum distance to open to after a swipe
|
2016-11-14 19:56:09 +00:00
|
|
|
maxSwipeDistance: PropTypes.oneOfType([
|
|
|
|
PropTypes.number,
|
|
|
|
PropTypes.func,
|
|
|
|
]).isRequired,
|
2016-05-11 07:24:08 +00:00
|
|
|
// 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,
|
2016-07-26 08:00:02 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static defaultProps = {
|
|
|
|
bounceFirstRowOnMount: false,
|
|
|
|
renderQuickActions: () => null,
|
|
|
|
};
|
2016-05-11 07:24:08 +00:00
|
|
|
|
2016-09-22 16:48:49 +00:00
|
|
|
constructor(props: Props, context: any): void {
|
|
|
|
super(props, context);
|
2016-05-11 07:24:08 +00:00
|
|
|
|
2016-06-07 16:07:30 +00:00
|
|
|
this._shouldBounceFirstRowOnMount = this.props.bounceFirstRowOnMount;
|
2016-09-22 16:48:49 +00:00
|
|
|
this.state = {
|
|
|
|
dataSource: this.props.dataSource,
|
|
|
|
};
|
2016-07-26 08:00:02 +00:00
|
|
|
}
|
2016-06-07 16:07:30 +00:00
|
|
|
|
2016-09-22 16:48:49 +00:00
|
|
|
componentWillReceiveProps(nextProps: Props): void {
|
|
|
|
if (this.state.dataSource.getDataSource() !== nextProps.dataSource.getDataSource()) {
|
2016-05-11 07:24:08 +00:00
|
|
|
this.setState({
|
2016-05-17 03:21:47 +00:00
|
|
|
dataSource: nextProps.dataSource,
|
2016-05-11 07:24:08 +00:00
|
|
|
});
|
|
|
|
}
|
2016-07-26 08:00:02 +00:00
|
|
|
}
|
2016-05-11 07:24:08 +00:00
|
|
|
|
2016-10-16 11:11:59 +00:00
|
|
|
render(): React.Element<any> {
|
2016-05-11 07:24:08 +00:00
|
|
|
return (
|
|
|
|
<ListView
|
|
|
|
{...this.props}
|
2016-05-12 18:48:39 +00:00
|
|
|
ref={(ref) => {
|
|
|
|
this._listViewRef = ref;
|
|
|
|
}}
|
2016-05-17 03:21:47 +00:00
|
|
|
dataSource={this.state.dataSource.getDataSource()}
|
2016-09-22 16:48:49 +00:00
|
|
|
onScroll={this._onScroll}
|
2016-05-11 07:24:08 +00:00
|
|
|
renderRow={this._renderRow}
|
|
|
|
/>
|
|
|
|
);
|
2016-07-26 08:00:02 +00:00
|
|
|
}
|
2016-05-11 07:24:08 +00:00
|
|
|
|
2016-10-04 18:41:19 +00:00
|
|
|
_onScroll = (e): void => {
|
2016-09-22 16:48:49 +00:00
|
|
|
// Close any opens rows on ListView scroll
|
|
|
|
if (this.props.dataSource.getOpenRowID()) {
|
|
|
|
this.setState({
|
|
|
|
dataSource: this.state.dataSource.setOpenRowID(null),
|
|
|
|
});
|
|
|
|
}
|
2016-10-04 18:41:19 +00:00
|
|
|
this.props.onScroll && this.props.onScroll(e);
|
2016-09-22 16:48:49 +00:00
|
|
|
}
|
|
|
|
|
2016-05-17 03:21:47 +00:00
|
|
|
/**
|
|
|
|
* This is a work-around to lock vertical `ListView` scrolling on iOS and
|
|
|
|
* mimic Android behaviour. Locking vertical scrolling when horizontal
|
|
|
|
* scrolling is active allows us to significantly improve framerates
|
|
|
|
* (from high 20s to almost consistently 60 fps)
|
|
|
|
*/
|
2016-09-22 16:48:49 +00:00
|
|
|
_setListViewScrollable(value: boolean): void {
|
|
|
|
if (this._listViewRef && typeof this._listViewRef.setNativeProps === 'function') {
|
2016-05-17 03:21:47 +00:00
|
|
|
this._listViewRef.setNativeProps({
|
|
|
|
scrollEnabled: value,
|
|
|
|
});
|
|
|
|
}
|
2016-09-22 16:48:49 +00:00
|
|
|
}
|
2016-05-17 03:21:47 +00:00
|
|
|
|
2016-05-12 18:48:39 +00:00
|
|
|
// Passing through ListView's getScrollResponder() function
|
2016-09-22 16:48:49 +00:00
|
|
|
getScrollResponder(): ?Object {
|
|
|
|
if (this._listViewRef && typeof this._listViewRef.getScrollResponder === 'function') {
|
2016-05-12 18:48:39 +00:00
|
|
|
return this._listViewRef.getScrollResponder();
|
|
|
|
}
|
2016-09-22 16:48:49 +00:00
|
|
|
}
|
2016-05-12 18:48:39 +00:00
|
|
|
|
2016-11-14 19:56:09 +00:00
|
|
|
// This enables rows having variable width slideoutView.
|
2017-02-23 23:00:41 +00:00
|
|
|
_getMaxSwipeDistance(rowData: Object, sectionID: string, rowID: string): number {
|
2016-11-14 19:56:09 +00:00
|
|
|
if (typeof this.props.maxSwipeDistance === 'function') {
|
|
|
|
return this.props.maxSwipeDistance(rowData, sectionID, rowID);
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.props.maxSwipeDistance;
|
2017-02-23 23:00:41 +00:00
|
|
|
}
|
2016-11-14 19:56:09 +00:00
|
|
|
|
2016-10-16 11:11:59 +00:00
|
|
|
_renderRow = (rowData: Object, sectionID: string, rowID: string): React.Element<any> => {
|
2016-05-11 07:24:08 +00:00
|
|
|
const slideoutView = this.props.renderQuickActions(rowData, sectionID, rowID);
|
|
|
|
|
2016-12-08 07:47:35 +00:00
|
|
|
// If renderQuickActions is unspecified or returns falsey, don't allow swipe
|
2016-05-11 07:24:08 +00:00
|
|
|
if (!slideoutView) {
|
|
|
|
return this.props.renderRow(rowData, sectionID, rowID);
|
|
|
|
}
|
|
|
|
|
2016-06-07 16:07:30 +00:00
|
|
|
let shouldBounceOnMount = false;
|
|
|
|
if (this._shouldBounceFirstRowOnMount) {
|
|
|
|
this._shouldBounceFirstRowOnMount = false;
|
|
|
|
shouldBounceOnMount = rowID === this.props.dataSource.getFirstRowID();
|
|
|
|
}
|
|
|
|
|
2016-05-11 07:24:08 +00:00
|
|
|
return (
|
|
|
|
<SwipeableRow
|
|
|
|
slideoutView={slideoutView}
|
|
|
|
isOpen={rowData.id === this.props.dataSource.getOpenRowID()}
|
2016-11-14 19:56:09 +00:00
|
|
|
maxSwipeDistance={this._getMaxSwipeDistance(rowData, sectionID, rowID)}
|
2016-05-11 07:24:08 +00:00
|
|
|
key={rowID}
|
2016-05-17 03:21:47 +00:00
|
|
|
onOpen={() => this._onOpen(rowData.id)}
|
2017-02-02 12:47:49 +00:00
|
|
|
onClose={() => this._onClose(rowData.id)}
|
2016-05-17 03:21:47 +00:00
|
|
|
onSwipeEnd={() => this._setListViewScrollable(true)}
|
2016-06-07 16:07:30 +00:00
|
|
|
onSwipeStart={() => this._setListViewScrollable(false)}
|
|
|
|
shouldBounceOnMount={shouldBounceOnMount}>
|
2016-05-11 07:24:08 +00:00
|
|
|
{this.props.renderRow(rowData, sectionID, rowID)}
|
|
|
|
</SwipeableRow>
|
|
|
|
);
|
2016-07-26 08:00:02 +00:00
|
|
|
};
|
2016-05-11 07:24:08 +00:00
|
|
|
|
2016-09-22 16:48:49 +00:00
|
|
|
_onOpen(rowID: string): void {
|
2016-05-11 07:24:08 +00:00
|
|
|
this.setState({
|
2016-05-17 03:21:47 +00:00
|
|
|
dataSource: this.state.dataSource.setOpenRowID(rowID),
|
2016-05-11 07:24:08 +00:00
|
|
|
});
|
2016-09-22 16:48:49 +00:00
|
|
|
}
|
2017-02-02 12:47:49 +00:00
|
|
|
|
|
|
|
_onClose(rowID: string): void {
|
|
|
|
this.setState({
|
|
|
|
dataSource: this.state.dataSource.setOpenRowID(null),
|
|
|
|
});
|
|
|
|
}
|
2016-07-26 08:00:02 +00:00
|
|
|
}
|
2016-05-11 07:24:08 +00:00
|
|
|
|
|
|
|
module.exports = SwipeableListView;
|