Compare commits

...

5 Commits
master ... fix

Author SHA1 Message Date
Timur Gibadullin
85c55a8071 fix 2017-07-06 14:05:00 +03:00
Timur Gibadullin
e99beff4b5 fix crash 2017-07-06 13:09:47 +03:00
Timur Gibadullin
54ec7df6ee const -> let 2017-07-06 12:57:00 +03:00
Timur Gibadullin
d4534e3f93 Remove side-effects from renders 2017-06-16 17:18:33 +03:00
Timur Gibadullin
40dbf3e7df update order 2017-06-02 18:51:19 +03:00
3 changed files with 44 additions and 22 deletions

View File

@ -17,7 +17,7 @@ import {
Platform, Platform,
} from 'react-native'; } from 'react-native';
import SortableList from 'react-native-sortable-list'; import SortableList from 'react-native-sortable-list';
import {shuffle} from 'lodash';
const window = Dimensions.get('window'); const window = Dimensions.get('window');
@ -65,6 +65,13 @@ const data = {
}; };
class Basic extends Component { class Basic extends Component {
state = {
order: Object.keys(data),
}
componentDidMount() {
setInterval(() => this.setState({order: shuffle(this.state.order)}), 3000)
}
render() { render() {
return ( return (
<View style={styles.container}> <View style={styles.container}>
@ -73,6 +80,7 @@ class Basic extends Component {
style={styles.list} style={styles.list}
contentContainerStyle={styles.contentContainer} contentContainerStyle={styles.contentContainer}
data={data} data={data}
order={this.state.order}
renderRow={this._renderRow} /> renderRow={this._renderRow} />
</View> </View>
); );
@ -194,7 +202,7 @@ const styles = StyleSheet.create({
marginTop: 7, marginTop: 7,
marginBottom: 12, marginBottom: 12,
borderRadius: 4, borderRadius: 4,
...Platform.select({ ...Platform.select({
ios: { ios: {

View File

@ -6,6 +6,7 @@
"start": "node node_modules/react-native/local-cli/cli.js start" "start": "node node_modules/react-native/local-cli/cli.js start"
}, },
"dependencies": { "dependencies": {
"lodash": "^4.17.4",
"react": "^15.4.0", "react": "^15.4.0",
"react-native": "^0.41.2", "react-native": "^0.41.2",
"react-native-sortable-list": "../.." "react-native-sortable-list": "../.."

View File

@ -47,7 +47,8 @@ export default class SortableList extends Component {
/** /**
* Stores promises of rows layouts. * Stores promises of rows layouts.
*/ */
_rowsLayouts = []; _rowsLayouts = {};
_resolveRowLayout = {};
_contentOffset = {x: 0, y: 0}; _contentOffset = {x: 0, y: 0};
@ -64,23 +65,43 @@ export default class SortableList extends Component {
scrollEnabled: this.props.scrollEnabled scrollEnabled: this.props.scrollEnabled
}; };
componentWillMount() {
this.state.order.forEach((key) => {
this._rowsLayouts[key] = new Promise((resolve) => {
this._resolveRowLayout[key] = resolve;
});
});
if (this.props.renderFooter && !this.props.horizontal) {
this._footerLayout = new Promise((resolve) => {
this._resolveFooterLayout = resolve;
});
}
}
componentDidMount() { componentDidMount() {
this._onUpdateLayouts(); this._onUpdateLayouts();
} }
componentWillReceiveProps(nextProps) { componentWillReceiveProps(nextProps) {
const {data, order} = this.state; const {data, order} = this.state;
const {data: nextData, order: nextOrder} = nextProps; let {data: nextData, order: nextOrder} = nextProps;
if (data && nextData && !shallowEqual(data, nextData)) { if (data && nextData && !shallowEqual(data, nextData)) {
nextOrder = nextOrder || Object.keys(nextData)
uniqueRowKey.id++; uniqueRowKey.id++;
this._rowsLayouts = []; this._rowsLayouts = {};
nextOrder.forEach((key) => {
this._rowsLayouts[key] = new Promise((resolve) => {
this._resolveRowLayout[key] = resolve;
});
});
this.setState({ this.setState({
animated: false, animated: false,
data: nextData, data: nextData,
containerLayout: null, containerLayout: null,
rowsLayouts: null, rowsLayouts: null,
order: nextOrder || Object.keys(nextData) order: nextOrder
}); });
} else if (order && nextOrder && !shallowEqual(order, nextOrder)) { } else if (order && nextOrder && !shallowEqual(order, nextOrder)) {
@ -206,7 +227,6 @@ export default class SortableList extends Component {
return order.map((key, index) => { return order.map((key, index) => {
const style = {[ZINDEX]: 0}; const style = {[ZINDEX]: 0};
const location = {x: 0, y: 0}; const location = {x: 0, y: 0};
let resolveLayout;
if (rowsLayouts) { if (rowsLayouts) {
if (horizontal) { if (horizontal) {
@ -218,8 +238,6 @@ export default class SortableList extends Component {
location.y = nextY; location.y = nextY;
nextY += rowsLayouts[key].height; nextY += rowsLayouts[key].height;
} }
} else {
this._rowsLayouts.push(new Promise((resolve) => (resolveLayout = resolve)));
} }
const active = activeRowKey === key; const active = activeRowKey === key;
@ -239,7 +257,7 @@ export default class SortableList extends Component {
disabled={!sortingEnabled} disabled={!sortingEnabled}
style={style} style={style}
location={location} location={location}
onLayout={!rowsLayouts ? this._onLayoutRow.bind(this, resolveLayout, key) : null} onLayout={!rowsLayouts ? this._onLayoutRow.bind(this, key) : null}
onActivate={this._onActivateRow.bind(this, key, index)} onActivate={this._onActivateRow.bind(this, key, index)}
onPress={this._onPressRow.bind(this, key)} onPress={this._onPressRow.bind(this, key)}
onRelease={this._onReleaseRow.bind(this, key)} onRelease={this._onReleaseRow.bind(this, key)}
@ -262,21 +280,16 @@ export default class SortableList extends Component {
} }
const {footerLayout} = this.state; const {footerLayout} = this.state;
let resolveLayout;
if (!footerLayout) {
this._footerLayout = new Promise((resolve) => (resolveLayout = resolve));
}
return ( return (
<View onLayout={!footerLayout ? this._onLayoutFooter.bind(this, resolveLayout) : null}> <View onLayout={!footerLayout ? this._onLayoutFooter : null}>
{this.props.renderFooter()} {this.props.renderFooter()}
</View> </View>
); );
} }
_onUpdateLayouts() { _onUpdateLayouts() {
Promise.all([this._footerLayout, ...this._rowsLayouts]) Promise.all([this._footerLayout, ...Object.values(this._rowsLayouts)])
.then(([footerLayout, ...rowsLayouts]) => { .then(([footerLayout, ...rowsLayouts]) => {
// Can get correct containers layout only after rowss layouts. // Can get correct containers layout only after rowss layouts.
this._container.measure((x, y, width, height, pageX, pageY) => { this._container.measure((x, y, width, height, pageX, pageY) => {
@ -515,13 +528,13 @@ export default class SortableList extends Component {
this._autoScrollInterval = null; this._autoScrollInterval = null;
} }
_onLayoutRow(resolveLayout, rowKey, {nativeEvent: {layout}}) { _onLayoutRow(rowKey, {nativeEvent: {layout}}) {
resolveLayout({rowKey, layout}); this._resolveRowLayout[rowKey]({rowKey, layout});
} }
_onLayoutFooter(resolveLayout, {nativeEvent: {layout}}) { _onLayoutFooter = ({nativeEvent: {layout}}) => {
resolveLayout(layout); this._resolveFooterLayout(layout);
} };
_onActivateRow = (rowKey, index, e, gestureState, location) => { _onActivateRow = (rowKey, index, e, gestureState, location) => {
this._activeRowLocation = location; this._activeRowLocation = location;