Compare commits
1 Commits
master
...
improve-pe
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
932437ae58 |
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,4 +1,3 @@
|
||||
.DS_Store
|
||||
*.log
|
||||
node_modules
|
||||
*.idea
|
||||
|
||||
21
LICENSE
21
LICENSE
@ -1,21 +0,0 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2019 Tim Gibadullin
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
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 NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS 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.
|
||||
11
README.md
11
README.md
@ -31,15 +31,8 @@ npm i react-native-sortable-list --save
|
||||
- **contentContainerStyle?** (Object, Array) these styles will be applied to the inner scroll view content container
|
||||
- **innerContainerStyle?** (Object, Array) these styles will be applied to the inner scroll view content container, excluding the header and footer
|
||||
- **horizontal?** (boolean) when true, the SortableList's children are arranged horizontally in a row instead of vertically in a column. The default value is false.
|
||||
- **showsVerticalScrollIndicator** (boolean) when false, the vertical scroll indicator will not be visible. The default value is true.
|
||||
- **showsHorizontalScrollIndicator** (boolean) when false, the horizontal scroll indicator will not be visible. The default value is true.
|
||||
- **sortingEnabled?** (boolean) when false, rows are not sortable. The default value is true.
|
||||
- **scrollEnabled?** (boolean) when false, the content does not scrollable. The default value is true.
|
||||
- **keyboardShouldPersistTaps** (string)<br />
|
||||
Determines when the keyboard should stay visible after a tap.
|
||||
- 'never' (the default), tapping outside of the focused text input when the keyboard is up dismisses the keyboard. When this happens, children won't receive the tap.
|
||||
- 'always', the keyboard will not dismiss automatically, and the scroll view will not catch taps, but children of the scroll view can catch taps.
|
||||
- 'handled', the keyboard will not dismiss automatically when the tap was handled by a children, (or captured by an ancestor.<br/>
|
||||
- **manuallyActivateRows?** (bool) whether you intend to use the `toggleRowActive` method to activate a row or use the out of box solution.
|
||||
- **autoscrollAreaSize?** (number) determines the height for vertical list and the width for horizontal list of the area at the begining and the end of the list that will trigger autoscrolling. Defaults to 60.<br />
|
||||
- **rowActivationTime?** (number) determines time delay in ms before pressed row becomes active. Defaults to 200 ms.<br />
|
||||
@ -61,8 +54,8 @@ Called when rows were reordered, takes an array of rows keys of the next rows or
|
||||
`(key) => void`<br />
|
||||
Called when a row was activated (user long tapped).
|
||||
- **onReleaseRow?** (function)<br />
|
||||
`(key, currentOrder) => void`<br />
|
||||
Called when the active row was released. Returns the key and the new list order.
|
||||
`(key) => void`<br />
|
||||
Called when the active row was released.
|
||||
- **onPressRow?** (function)<br />
|
||||
`(key) => void`<br />
|
||||
Called when a row was pressed.
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "react-native-sortable-list",
|
||||
"version": "0.0.24",
|
||||
"version": "0.0.21",
|
||||
"description": "React Native Sortable List component",
|
||||
"main": "index.js",
|
||||
"repository": {
|
||||
|
||||
14
src/Row.js
14
src/Row.js
@ -1,6 +1,6 @@
|
||||
import React, {Component, cloneElement} from 'react';
|
||||
import PropTypes from 'prop-types';
|
||||
import {Animated, PanResponder, StyleSheet, ViewPropTypes} from 'react-native';
|
||||
import {Animated, PanResponder, StyleSheet} from 'react-native';
|
||||
import {shallowEqual} from './utils';
|
||||
|
||||
export default class Row extends Component {
|
||||
@ -9,7 +9,7 @@ export default class Row extends Component {
|
||||
animated: PropTypes.bool,
|
||||
disabled: PropTypes.bool,
|
||||
horizontal: PropTypes.bool,
|
||||
style: ViewPropTypes.style,
|
||||
style: Animated.View.propTypes.style,
|
||||
location: PropTypes.shape({
|
||||
x: PropTypes.number,
|
||||
y: PropTypes.number,
|
||||
@ -142,11 +142,10 @@ export default class Row extends Component {
|
||||
},
|
||||
});
|
||||
|
||||
componentDidUpdate() {
|
||||
const {animated, location} = this.props;
|
||||
|
||||
if (!this._active && !shallowEqual(this._location, location)) {
|
||||
this._relocate(location, !this._active && animated);
|
||||
componentWillReceiveProps(nextProps) {
|
||||
if (!this._active && !shallowEqual(this._location, nextProps.location)) {
|
||||
const animated = !this._active && nextProps.animated;
|
||||
this._relocate(nextProps.location, animated);
|
||||
}
|
||||
}
|
||||
|
||||
@ -198,7 +197,6 @@ export default class Row extends Component {
|
||||
Animated.timing(this._animatedLocation, {
|
||||
toValue: nextLocation,
|
||||
duration: 300,
|
||||
useNativeDriver: false,
|
||||
}).start(() => {
|
||||
this._isAnimationRunning = false;
|
||||
});
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
import React, {Component} from 'react';
|
||||
import PropTypes from 'prop-types';
|
||||
import {ScrollView, View, StyleSheet, Platform, RefreshControl, ViewPropTypes} from 'react-native';
|
||||
import {shallowEqual, swapArrayElements} from './utils';
|
||||
import {inRange, shallowEqual, swapArrayElements} from './utils';
|
||||
import Row from './Row';
|
||||
|
||||
const AUTOSCROLL_INTERVAL = 100;
|
||||
@ -23,19 +23,10 @@ export default class SortableList extends Component {
|
||||
sortingEnabled: PropTypes.bool,
|
||||
scrollEnabled: PropTypes.bool,
|
||||
horizontal: PropTypes.bool,
|
||||
showsVerticalScrollIndicator: PropTypes.bool,
|
||||
showsHorizontalScrollIndicator: PropTypes.bool,
|
||||
refreshControl: PropTypes.element,
|
||||
autoscrollAreaSize: PropTypes.number,
|
||||
snapToAlignment: PropTypes.string,
|
||||
rowActivationTime: PropTypes.number,
|
||||
manuallyActivateRows: PropTypes.bool,
|
||||
keyboardShouldPersistTaps: PropTypes.oneOf(['never', 'always', 'handled']),
|
||||
scrollEventThrottle: PropTypes.number,
|
||||
decelerationRate: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
|
||||
pagingEnabled: PropTypes.bool,
|
||||
nestedScrollEnabled: PropTypes.bool,
|
||||
disableIntervalMomentum: PropTypes.bool,
|
||||
|
||||
renderRow: PropTypes.func.isRequired,
|
||||
renderHeader: PropTypes.func,
|
||||
@ -44,22 +35,13 @@ export default class SortableList extends Component {
|
||||
onChangeOrder: PropTypes.func,
|
||||
onActivateRow: PropTypes.func,
|
||||
onReleaseRow: PropTypes.func,
|
||||
onScroll: PropTypes.func,
|
||||
};
|
||||
|
||||
static defaultProps = {
|
||||
sortingEnabled: true,
|
||||
scrollEnabled: true,
|
||||
keyboardShouldPersistTaps: 'never',
|
||||
autoscrollAreaSize: 60,
|
||||
snapToAlignment: 'start',
|
||||
manuallyActivateRows: false,
|
||||
showsVerticalScrollIndicator: true,
|
||||
showsHorizontalScrollIndicator: true,
|
||||
scrollEventThrottle: 2,
|
||||
decelerationRate: 'normal',
|
||||
pagingEnabled: false,
|
||||
onScroll: () => {}
|
||||
manuallyActivateRows: false
|
||||
}
|
||||
|
||||
/**
|
||||
@ -88,7 +70,7 @@ export default class SortableList extends Component {
|
||||
scrollEnabled: this.props.scrollEnabled
|
||||
};
|
||||
|
||||
componentDidMount() {
|
||||
componentWillMount() {
|
||||
this.state.order.forEach((key) => {
|
||||
this._rowsLayouts[key] = new Promise((resolve) => {
|
||||
this._resolveRowLayout[key] = resolve;
|
||||
@ -100,20 +82,20 @@ export default class SortableList extends Component {
|
||||
this._resolveHeaderLayout = resolve;
|
||||
});
|
||||
}
|
||||
|
||||
if (this.props.renderFooter && !this.props.horizontal) {
|
||||
this._footerLayout = new Promise((resolve) => {
|
||||
this._resolveFooterLayout = resolve;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
componentDidMount() {
|
||||
this._onUpdateLayouts();
|
||||
}
|
||||
|
||||
componentDidUpdate(prevProps, prevState) {
|
||||
const {data, order, scrollEnabled} = this.state;
|
||||
let {data: nextData, order: nextOrder} = this.props;
|
||||
const {data: prevData} = prevState;
|
||||
componentWillReceiveProps(nextProps) {
|
||||
const {data, order} = this.state;
|
||||
let {data: nextData, order: nextOrder} = nextProps;
|
||||
|
||||
if (data && nextData && !shallowEqual(data, nextData)) {
|
||||
nextOrder = nextOrder || Object.keys(nextData)
|
||||
@ -124,32 +106,26 @@ export default class SortableList extends Component {
|
||||
this._resolveRowLayout[key] = resolve;
|
||||
});
|
||||
});
|
||||
|
||||
if (Object.keys(nextData).length > Object.keys(data).length) {
|
||||
this.setState({
|
||||
animated: false,
|
||||
data: nextData,
|
||||
containerLayout: null,
|
||||
rowsLayouts: null,
|
||||
order: nextOrder
|
||||
});
|
||||
} else {
|
||||
this.setState({
|
||||
data: nextData,
|
||||
order: nextOrder
|
||||
});
|
||||
}
|
||||
this.setState({
|
||||
animated: false,
|
||||
data: nextData,
|
||||
containerLayout: null,
|
||||
rowsLayouts: null,
|
||||
order: nextOrder
|
||||
});
|
||||
|
||||
} else if (order && nextOrder && !shallowEqual(order, nextOrder)) {
|
||||
this.setState({order: nextOrder});
|
||||
}
|
||||
}
|
||||
|
||||
componentDidUpdate(prevProps, prevState) {
|
||||
const {data} = this.state;
|
||||
const {data: prevData} = prevState;
|
||||
|
||||
if (data && prevData && !shallowEqual(data, prevData)) {
|
||||
this._onUpdateLayouts();
|
||||
}
|
||||
if (prevProps.scrollEnabled !== scrollEnabled) {
|
||||
this.setState({scrollEnabled: prevProps.scrollEnabled})
|
||||
}
|
||||
}
|
||||
|
||||
scrollBy({dx = 0, dy = 0, animated = false}) {
|
||||
@ -204,21 +180,7 @@ export default class SortableList extends Component {
|
||||
}
|
||||
|
||||
render() {
|
||||
let {
|
||||
contentContainerStyle,
|
||||
innerContainerStyle,
|
||||
horizontal,
|
||||
style,
|
||||
showsVerticalScrollIndicator,
|
||||
showsHorizontalScrollIndicator,
|
||||
snapToAlignment,
|
||||
scrollEventThrottle,
|
||||
decelerationRate,
|
||||
pagingEnabled,
|
||||
nestedScrollEnabled,
|
||||
disableIntervalMomentum,
|
||||
keyboardShouldPersistTaps,
|
||||
} = this.props;
|
||||
let {contentContainerStyle, innerContainerStyle, horizontal, style} = this.props;
|
||||
const {animated, contentHeight, contentWidth, scrollEnabled} = this.state;
|
||||
const containerStyle = StyleSheet.flatten([style, {opacity: Number(animated)}])
|
||||
innerContainerStyle = [
|
||||
@ -237,22 +199,13 @@ export default class SortableList extends Component {
|
||||
return (
|
||||
<View style={containerStyle} ref={this._onRefContainer}>
|
||||
<ScrollView
|
||||
nestedScrollEnabled={nestedScrollEnabled}
|
||||
disableIntervalMomentum={disableIntervalMomentum}
|
||||
refreshControl={refreshControl}
|
||||
ref={this._onRefScrollView}
|
||||
horizontal={horizontal}
|
||||
contentContainerStyle={contentContainerStyle}
|
||||
scrollEventThrottle={scrollEventThrottle}
|
||||
pagingEnabled={pagingEnabled}
|
||||
decelerationRate={decelerationRate}
|
||||
scrollEventThrottle={2}
|
||||
scrollEnabled={scrollEnabled}
|
||||
keyboardShouldPersistTaps={keyboardShouldPersistTaps}
|
||||
showsHorizontalScrollIndicator={showsHorizontalScrollIndicator}
|
||||
showsVerticalScrollIndicator={showsVerticalScrollIndicator}
|
||||
snapToAlignment={snapToAlignment}
|
||||
onScroll={this._onScroll}
|
||||
>
|
||||
onScroll={this._onScroll}>
|
||||
{this._renderHeader()}
|
||||
<View style={innerContainerStyle}>
|
||||
{this._renderRows()}
|
||||
@ -267,7 +220,6 @@ export default class SortableList extends Component {
|
||||
const {horizontal, rowActivationTime, sortingEnabled, renderRow} = this.props;
|
||||
const {animated, order, data, activeRowKey, releasedRowKey, rowsLayouts} = this.state;
|
||||
|
||||
|
||||
let nextX = 0;
|
||||
let nextY = 0;
|
||||
|
||||
@ -349,11 +301,13 @@ export default class SortableList extends Component {
|
||||
}
|
||||
|
||||
_onUpdateLayouts() {
|
||||
const {horizontal} = this.props;
|
||||
const {order} = this.state;
|
||||
Promise.all([this._headerLayout, this._footerLayout, ...Object.values(this._rowsLayouts)])
|
||||
.then(([headerLayout, footerLayout, ...rowsLayouts]) => {
|
||||
// Can get correct container’s layout only after rows’s layouts.
|
||||
this._container.measure((x, y, width, height, pageX, pageY) => {
|
||||
const rowsLayoutsByKey = {};
|
||||
let rowsLayoutsByKey = {};
|
||||
let contentHeight = 0;
|
||||
let contentWidth = 0;
|
||||
|
||||
@ -362,10 +316,12 @@ export default class SortableList extends Component {
|
||||
contentHeight += layout.height;
|
||||
contentWidth += layout.width;
|
||||
});
|
||||
rowsLayoutsByKey = this._getRowsLocations(rowsLayoutsByKey, order);
|
||||
|
||||
this.setState({
|
||||
containerLayout: {x, y, width, height, pageX, pageY},
|
||||
rowsLayouts: rowsLayoutsByKey,
|
||||
rowsSwapRanges: this._getRowsSwapRanges(rowsLayoutsByKey, order),
|
||||
headerLayout,
|
||||
footerLayout,
|
||||
contentHeight,
|
||||
@ -377,6 +333,54 @@ export default class SortableList extends Component {
|
||||
});
|
||||
}
|
||||
|
||||
_getRowsLocations(_rowsLayouts, order) {
|
||||
const {horizontal} = this.props;
|
||||
const rowsLayouts = {};
|
||||
let nextX = 0;
|
||||
let nextY = 0;
|
||||
|
||||
for (let i = 0, len = order.length; i < len; i++) {
|
||||
const rowKey = order[i];
|
||||
const rowLayout = _rowsLayouts[rowKey];
|
||||
|
||||
rowsLayouts[rowKey] = {
|
||||
...rowLayout,
|
||||
x: nextX,
|
||||
y: nextY,
|
||||
};
|
||||
|
||||
if (horizontal) {
|
||||
nextX += rowLayout.width;
|
||||
} else {
|
||||
nextY += rowLayout.height;
|
||||
}
|
||||
}
|
||||
|
||||
return rowsLayouts;
|
||||
}
|
||||
|
||||
_getRowsSwapRanges(rowsLayouts, order) {
|
||||
const {horizontal} = this.props;
|
||||
const rowsSwapRanges = {};
|
||||
|
||||
for (let i = 0, len = order.length; i < len; i++) {
|
||||
const rowKey = order[i];
|
||||
const rowLayout = rowsLayouts[rowKey];
|
||||
|
||||
rowsSwapRanges[rowKey] = horizontal
|
||||
? {
|
||||
left: [rowLayout.x + rowLayout.width / 3, rowLayout.x + rowLayout.width],
|
||||
right: [rowLayout.x, rowLayout.x + 2 * rowLayout.width / 3],
|
||||
}
|
||||
: {
|
||||
top: [rowLayout.y + rowLayout.height / 3, rowLayout.y + rowLayout.height],
|
||||
bottom: [rowLayout.y, rowLayout.y + 2 * rowLayout.height / 3],
|
||||
};
|
||||
}
|
||||
|
||||
return rowsSwapRanges;
|
||||
}
|
||||
|
||||
_scroll(animated) {
|
||||
this._scrollView.scrollTo({...this._contentOffset, animated});
|
||||
}
|
||||
@ -386,7 +390,8 @@ export default class SortableList extends Component {
|
||||
* swaps them, else shifts rows.
|
||||
*/
|
||||
_setOrderOnMove() {
|
||||
const {activeRowKey, activeRowIndex, order} = this.state;
|
||||
const {activeRowKey, activeRowIndex, order, rowsLayouts} = this.state;
|
||||
const {horizontal} = this.props;
|
||||
|
||||
if (activeRowKey === null || this._autoScrollInterval) {
|
||||
return;
|
||||
@ -416,9 +421,14 @@ export default class SortableList extends Component {
|
||||
nextOrder.splice(rowUnderActiveIndex, 0, activeRowKey);
|
||||
}
|
||||
|
||||
const nextRowsLayouts = this._getRowsLocations(rowsLayouts, nextOrder);
|
||||
const nextRowsSwapRanges = this._getRowsSwapRanges(nextRowsLayouts, nextOrder);
|
||||
|
||||
this.setState({
|
||||
order: nextOrder,
|
||||
activeRowIndex: rowUnderActiveIndex,
|
||||
rowsLayouts: nextRowsLayouts,
|
||||
rowsSwapRanges: nextRowsSwapRanges,
|
||||
}, () => {
|
||||
if (this.props.onChangeOrder) {
|
||||
this.props.onChangeOrder(nextOrder);
|
||||
@ -428,53 +438,114 @@ export default class SortableList extends Component {
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds a row, which was covered with the moving row’s half.
|
||||
* Finds a row, which was covered with the moving row’s third.
|
||||
*/
|
||||
_findRowUnderActiveRow() {
|
||||
const {horizontal} = this.props;
|
||||
const {rowsLayouts, activeRowKey, activeRowIndex, order} = this.state;
|
||||
const movingRowLayout = rowsLayouts[activeRowKey];
|
||||
const rowLeftX = this._activeRowLocation.x
|
||||
const rowRightX = rowLeftX + movingRowLayout.width;
|
||||
const rowTopY = this._activeRowLocation.y;
|
||||
const rowBottomY = rowTopY + movingRowLayout.height;
|
||||
const {rowsLayouts, rowsSwapRanges, activeRowKey, activeRowIndex, order} = this.state;
|
||||
const movingDirection = this._movingDirection;
|
||||
const rowsCount = order.length;
|
||||
const activeRowLayout = rowsLayouts[activeRowKey];
|
||||
const activeRowLeftX = this._activeRowLocation.x
|
||||
const activeRowRightX = this._activeRowLocation.x + activeRowLayout.width;
|
||||
const activeRowTopY = this._activeRowLocation.y;
|
||||
const activeRowBottomY = this._activeRowLocation.y + activeRowLayout.height;
|
||||
|
||||
for (
|
||||
let currentRowIndex = 0, x = 0, y = 0, rowsCount = order.length;
|
||||
currentRowIndex < rowsCount - 1;
|
||||
currentRowIndex++
|
||||
const prevRowIndex = activeRowIndex - 1;
|
||||
const prevRowKey = order[prevRowIndex];
|
||||
const prevRowSwapRages = rowsSwapRanges[prevRowKey]
|
||||
const nextRowIndex = activeRowIndex + 1;
|
||||
const nextRowKey = order[nextRowIndex];
|
||||
const nextRowSwapRages = rowsSwapRanges[nextRowKey]
|
||||
|
||||
if (horizontal
|
||||
? (movingDirection === 1
|
||||
? (
|
||||
(activeRowIndex === 0 || activeRowLeftX > prevRowSwapRages.right[0]) &&
|
||||
(activeRowIndex === rowsCount - 1 || activeRowRightX < nextRowSwapRages.left[0])
|
||||
)
|
||||
: (
|
||||
(activeRowIndex === 0 || activeRowLeftX > prevRowSwapRages.right[1]) &&
|
||||
(activeRowIndex === rowsCount - 1 || activeRowRightX < nextRowSwapRages.left[1])
|
||||
)
|
||||
)
|
||||
: (movingDirection === 1
|
||||
? (
|
||||
(activeRowIndex === 0 || activeRowTopY > prevRowSwapRages.bottom[0]) &&
|
||||
(activeRowIndex === rowsCount - 1 || activeRowBottomY < nextRowSwapRages.top[0])
|
||||
)
|
||||
: (
|
||||
(activeRowIndex === 0 || activeRowTopY > prevRowSwapRages.bottom[1]) &&
|
||||
(activeRowIndex === rowsCount - 1 || activeRowBottomY < nextRowSwapRages.top[1])
|
||||
)
|
||||
)
|
||||
) {
|
||||
const currentRowKey = order[currentRowIndex];
|
||||
const currentRowLayout = rowsLayouts[currentRowKey];
|
||||
const nextRowIndex = currentRowIndex + 1;
|
||||
const nextRowLayout = rowsLayouts[order[nextRowIndex]];
|
||||
return {
|
||||
rowKey: activeRowKey,
|
||||
rowIndex: activeRowIndex,
|
||||
};
|
||||
}
|
||||
|
||||
x += currentRowLayout.width;
|
||||
y += currentRowLayout.height;
|
||||
|
||||
if (currentRowKey !== activeRowKey && (
|
||||
horizontal
|
||||
? ((x - currentRowLayout.width <= rowLeftX || currentRowIndex === 0) && rowLeftX <= x - currentRowLayout.width / 3)
|
||||
: ((y - currentRowLayout.height <= rowTopY || currentRowIndex === 0) && rowTopY <= y - currentRowLayout.height / 3)
|
||||
)) {
|
||||
return {
|
||||
rowKey: order[currentRowIndex],
|
||||
rowIndex: currentRowIndex,
|
||||
if (movingDirection === 1) {
|
||||
if (horizontal
|
||||
? inRange(activeRowRightX, ...nextRowSwapRages.left)
|
||||
: inRange(activeRowBottomY, ...nextRowSwapRages.top)
|
||||
) {
|
||||
return {
|
||||
rowKey: nextRowKey,
|
||||
rowIndex: nextRowIndex,
|
||||
};
|
||||
}
|
||||
|
||||
} else {
|
||||
if (horizontal
|
||||
? (x + nextRowLayout.width / 3 <= rowRightX && (rowRightX <= x + nextRowLayout.width || nextRowIndex === rowsCount - 1))
|
||||
: (y + nextRowLayout.height / 3 <= rowBottomY && (rowBottomY <= y + nextRowLayout.height || nextRowIndex === rowsCount - 1))
|
||||
? inRange(activeRowLeftX, ...prevRowSwapRages.right)
|
||||
: inRange(activeRowTopY, ...prevRowSwapRages.bottom)
|
||||
) {
|
||||
return {
|
||||
rowKey: order[nextRowIndex],
|
||||
rowIndex: nextRowIndex,
|
||||
return {
|
||||
rowKey: prevRowKey,
|
||||
rowIndex: prevRowIndex,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
return {rowKey: activeRowKey, rowIndex: activeRowIndex};
|
||||
// let startIndex = 0;
|
||||
// let endIndex = rowsCount - 1;
|
||||
// let middleIndex;
|
||||
// let it = 0
|
||||
// console.log(movingDirection);
|
||||
// while (startIndex < endIndex) {
|
||||
// middleIndex = Math.floor((endIndex - startIndex) / 2);
|
||||
|
||||
// if (it++ > 10) {
|
||||
// console.log(startIndex, middleIndex, endIndex);
|
||||
// break
|
||||
// }
|
||||
// const middleRowSwapRanges = rowsSwapRanges[middleIndex];
|
||||
|
||||
// if (horizontal) {
|
||||
// if (movingDirection === 1) {
|
||||
// if (inRange(activeRowRightX, ...middleRowSwapRanges.left)) break;
|
||||
// else if (activeRowRightX < middleRowSwapRanges.left[0]) endIndex = middleIndex;
|
||||
// else if (activeRowRightX > middleRowSwapRanges.left[1]) startIndex = middleIndex;
|
||||
// } else {
|
||||
// if (inRange(activeRowLeftX, ...middleRowSwapRanges.right)) break;
|
||||
// else if (activeRowLeftX < middleRowSwapRanges.right[0]) endIndex = middleIndex;
|
||||
// else if (activeRowLeftX > middleRowSwapRanges.right[1]) startIndex = middleIndex;
|
||||
// }
|
||||
// } else {
|
||||
// if (movingDirection === 1) {
|
||||
// if (inRange(activeRowBottomY, ...middleRowSwapRanges.top)) break;
|
||||
// else if (activeRowBottomY < middleRowSwapRanges.top[0]) endIndex = middleIndex;
|
||||
// else if (activeRowBottomY > middleRowSwapRanges.top[1]) startIndex = middleIndex;
|
||||
// } else {
|
||||
// if (inRange(activeRowTopY, ...middleRowSwapRanges.bottom)) break;
|
||||
// else if (activeRowTopY < middleRowSwapRanges.bottom[0]) endIndex = middleIndex;
|
||||
// else if (activeRowTopY > middleRowSwapRanges.bottom[1]) startIndex = middleIndex;
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
// return {rowKey: order[middleIndex], rowIndex: middleIndex};
|
||||
}
|
||||
|
||||
_scrollOnMove(e) {
|
||||
@ -632,7 +703,7 @@ export default class SortableList extends Component {
|
||||
}));
|
||||
|
||||
if (this.props.onReleaseRow) {
|
||||
this.props.onReleaseRow(rowKey, this.state.order);
|
||||
this.props.onReleaseRow(rowKey);
|
||||
}
|
||||
};
|
||||
|
||||
@ -642,9 +713,11 @@ export default class SortableList extends Component {
|
||||
const prevMovingDirection = this._movingDirection;
|
||||
|
||||
this._activeRowLocation = location;
|
||||
this._movingDirection = this.props.horizontal
|
||||
? prevMovingRowX < this._activeRowLocation.x
|
||||
: prevMovingRowY < this._activeRowLocation.y;
|
||||
this._movingDirection = (
|
||||
this.props.horizontal
|
||||
? prevMovingRowX <= this._activeRowLocation.x
|
||||
: prevMovingRowY <= this._activeRowLocation.y
|
||||
) ? 1 : -1;
|
||||
|
||||
this._movingDirectionChanged = prevMovingDirection !== this._movingDirection;
|
||||
this._setOrderOnMove();
|
||||
@ -654,9 +727,8 @@ export default class SortableList extends Component {
|
||||
}
|
||||
};
|
||||
|
||||
_onScroll = (e) => {
|
||||
this._contentOffset = e.nativeEvent.contentOffset;
|
||||
this.props.onScroll(e)
|
||||
_onScroll = ({nativeEvent: {contentOffset}}) => {
|
||||
this._contentOffset = contentOffset;
|
||||
};
|
||||
|
||||
_onRefContainer = (component) => {
|
||||
|
||||
3
src/utils/inRange.js
Normal file
3
src/utils/inRange.js
Normal file
@ -0,0 +1,3 @@
|
||||
export default function inRange(number, start, end) {
|
||||
return start <= number && number <= end;
|
||||
}
|
||||
@ -1,7 +1,3 @@
|
||||
import shallowEqual from './shallowEqual';
|
||||
import swapArrayElements from './swapArrayElements';
|
||||
|
||||
export {
|
||||
shallowEqual,
|
||||
swapArrayElements
|
||||
};
|
||||
export {default as inRange} from './inRange'
|
||||
export {default as shallowEqual} from './shallowEqual';
|
||||
export {default as swapArrayElements} from './swapArrayElements';
|
||||
|
||||
Loading…
Reference in New Issue
Block a user