From afd2669fb5441835aae931bb57023ae1a3f94d99 Mon Sep 17 00:00:00 2001 From: key Date: Sat, 21 Jul 2018 16:59:51 +0200 Subject: [PATCH] merge prs --- .babelrc | 10 ++++++++++ .gitignore | 1 + index.js | 18 +++++++++++++----- package.json | 13 ++++++++++--- 4 files changed, 34 insertions(+), 8 deletions(-) create mode 100644 .babelrc create mode 100644 .gitignore diff --git a/.babelrc b/.babelrc new file mode 100644 index 0000000..ae6b0b3 --- /dev/null +++ b/.babelrc @@ -0,0 +1,10 @@ +{ + "presets": ["env", "react", "stage-2"], + "plugins": [ + ["module-resolver", { + "alias": { + "~": "./", + }, + }], + ], +} diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c2658d7 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +node_modules/ diff --git a/index.js b/index.js index 4b91802..4703c37 100644 --- a/index.js +++ b/index.js @@ -29,7 +29,7 @@ class Block extends Component { this.props.inactive || this.props.onLongPress() } + onLongPress = { () => (this.props.inactive || this.props.fixed) || this.props.onLongPress() } onPress = { () => this.props.inactive || this.props.onPress() }> @@ -64,6 +64,7 @@ class SortableGrid extends Component { itemWrapperStyle = { this._getItemWrapperStyle(key) } deletionView = { this._getDeletionView(key) } inactive = { item.props.inactive } + fixed = { item.props.fixed } > {item} @@ -154,7 +155,7 @@ class SortableGrid extends Component { if (this.state.activeBlock != null && this._blockPositionsSet()) { if (this.state.deleteModeOn) return this.deleteModeMove({ x: moveX, y: moveY }) - if (dx != 0 || dy != 0) this.initialDragDone = true + if (dx != 0 || dy != 0) this.initialDragDone = true let yChokeAmount = Math.max(0, (this.activeBlockOffset.y + moveY) - (this.state.gridLayout.height - this.blockHeight)) let xChokeAmount = Math.max(0, (this.activeBlockOffset.x + moveX) - (this.state.gridLayout.width - this.blockWidth)) @@ -174,7 +175,9 @@ class SortableGrid extends Component { let blockPosition = block.origin let distance = this._getDistanceTo(blockPosition) - if (distance < closestDistance && distance < this.state.blockWidth) { + if (distance < closestDistance && + distance < this.state.blockWidth && + !this.items[index].props.fixed) { closest = index closestDistance = distance } @@ -188,7 +191,9 @@ class SortableGrid extends Component { closestDistance = distance } }) + if (closest !== this.state.activeBlock) { + // animate the activeBlock closest block to its previous position Animated.timing( this._getBlock(closest).currentPosition, { @@ -196,11 +201,14 @@ class SortableGrid extends Component { duration: this.blockTransitionDuration } ).start() + + // update the position of the closest block let blockPositions = this.state.blockPositions this._getActiveBlock().origin = blockPositions[closest].origin blockPositions[closest].origin = originalPosition this.setState({ blockPositions }) + // update the order array var tempOrderIndex = this.itemOrder[this.state.activeBlock].order this.itemOrder[this.state.activeBlock].order = this.itemOrder[closest].order this.itemOrder[closest].order = tempOrderIndex @@ -288,7 +296,7 @@ class SortableGrid extends Component { } else { this.blockWidth = nativeEvent.layout.width / this.itemsPerRow - this.blockHeight = this.blockWidth + this.blockHeight = this.blockHeight = this.props.itemHeight || this.blockWidth; } if (this.state.gridLayout != nativeEvent.layout) { this.setState({ @@ -575,7 +583,7 @@ class SortableGrid extends Component { { width: this.state.blockWidth, height: this.state.blockHeight, justifyContent: 'center' }, - this._blockPositionsSet() && (this.initialDragDone || this.state.deleteModeOn) && + this._blockPositionsSet() && (this.initialDragDone || this.state.deleteModeOn) && { position: 'absolute', top: this._getBlock(key).currentPosition.getLayout().top, left: this._getBlock(key).currentPosition.getLayout().left diff --git a/package.json b/package.json index c26c613..343dbf7 100644 --- a/package.json +++ b/package.json @@ -7,8 +7,10 @@ ], "version": "2.0.1", "description": "Drag-and-drop sortable grid view for React Native.", - "main": "index.js", - "scripts": {}, + "main": "dist/index.js", + "scripts": { + "build": "babel src --out-dir dist" + }, "repository": { "type": "git", "url": "git+https://github.com/ollija/react-native-sortable-grid.git" @@ -16,6 +18,11 @@ "license": "MIT", "dependencies": {}, "devDependencies": { - "lodash": "^4.16.4" + "lodash": "^4.16.4", + "babel-cli": "^6.26.0", + "babel-plugin-module-resolver": "^3.0.0", + "babel-preset-env": "^1.6.1", + "babel-preset-react": "^6.24.1", + "babel-preset-stage-2": "^6.24.1" } }