Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"presets": ["env", "react", "stage-2"],
"plugins": [
["module-resolver", {
"alias": {
"~": "./",
},
}],
],
}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules/
18 changes: 13 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class Block extends Component {
<TouchableWithoutFeedback
style = {{ flex: 1 }}
delayLongPress = { this.props.delayLongPress }
onLongPress = { () => this.props.inactive || this.props.onLongPress() }
onLongPress = { () => (this.props.inactive || this.props.fixed) || this.props.onLongPress() }
onPress = { () => this.props.inactive || this.props.onPress() }>

<View style={styles.itemImageContainer}>
Expand Down Expand Up @@ -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}
</Block>
Expand Down Expand Up @@ -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))
Expand All @@ -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
}
Expand All @@ -188,19 +191,24 @@ 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,
{
toValue: this._getActiveBlock().origin,
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
Expand Down Expand Up @@ -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({
Expand Down Expand Up @@ -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
Expand Down
13 changes: 10 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,22 @@
],
"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"
},
"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"
}
}