Skip to content

Instantly share code, notes, and snippets.

@davidpett
Last active June 26, 2018 14:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davidpett/38c3a99f43499c8e7df31a92645f0a41 to your computer and use it in GitHub Desktop.
Save davidpett/38c3a99f43499c8e7df31a92645f0a41 to your computer and use it in GitHub Desktop.
import { View } from 'react-primitives'
import React, { Component } from 'react'
import Children from 'react-children-utilities'
import { DragItem, DropArea } from '../drag-drop'
export default class DragDrop extends Component {
render() {
const { activeDropArea, items, isDragging, position } = this.state
const children = Children.deepMap(this.props.children, (child, index) => {
if (child.type === DragItem) {
return React.cloneElement(child, {
onActivate: () => this.addItem(child),
onDeactivate: () => this.removeItem(child),
})
} else if (child.type === DropArea) {
return React.cloneElement(child, {
isActive: activeDropArea === child.key,
isDragging,
onDragOver: () => this.setState({ activeDropArea: child.key }),
onDragOut: () => this.setState({ activeDropArea: null }),
position,
})
} else {
return child
}
})
return (
<View style={{ flex: 1 }} {...this._panResponder.panHandlers}>
{isDragging && renderIcon(items.length, position)}
{children}
</View>
)
}
}
import React, { Component } from 'react'
import { View } from 'react-primitives'
export default class DropArea extends Component {
render() {
let { isOver } = this.props
return (
<View style={{ flex: 1 }}>
{this.props.children(isOver)}
</View>
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment