mirror of https://github.com/sgoudham/carbon.git
Prepare to separate DND to a separate module
parent
fd0fa897c4
commit
131640245c
@ -1,28 +1,60 @@
|
|||||||
import React from 'react'
|
const { createElement, Component } = require('react')
|
||||||
import { DropTarget } from 'react-dnd'
|
const { DropTarget } = require('react-dnd')
|
||||||
import { NativeTypes } from 'react-dnd-html5-backend'
|
const { NativeTypes } = require('react-dnd-html5-backend')
|
||||||
|
|
||||||
const spec = {
|
const spec = {
|
||||||
drop (props, monitor) {
|
drop(props, monitor, component) {
|
||||||
const bundle = monitor.getItem()
|
const bundle = monitor.getItem()
|
||||||
const file = bundle.files[0]
|
Promise.all(
|
||||||
|
bundle.files.map(file => {
|
||||||
const reader = new FileReader()
|
const reader = new FileReader()
|
||||||
reader.onload = event => props.onDrop(event.target.result)
|
return new Promise((resolve, reject) => {
|
||||||
|
reader.onload = event => resolve(event.target.result)
|
||||||
reader.readAsText(file, 'UTF-8');
|
reader.readAsText(file, 'UTF-8');
|
||||||
|
})
|
||||||
|
})
|
||||||
|
).then(contents => {
|
||||||
|
bundle.contents = contents
|
||||||
|
component.setState(state => ({
|
||||||
|
lastContent: contents,
|
||||||
|
history: [bundle, ...state.history]
|
||||||
|
}))
|
||||||
|
props.onDrop(contents)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const collect = (connect, monitor) => ({ connectDropTarget: connect.dropTarget(), isOver: monitor.isOver() })
|
const collect = (connect, monitor) => ({
|
||||||
|
connectDropTarget: connect.dropTarget(),
|
||||||
|
monitor,
|
||||||
|
isOver: monitor.isOver(),
|
||||||
|
canDrop: monitor.canDrop()
|
||||||
|
})
|
||||||
|
|
||||||
const ReadFileDropContainer = props => props.connectDropTarget(
|
class ReadFileDropContainer extends Component {
|
||||||
<div>
|
constructor(props) {
|
||||||
{
|
super(props)
|
||||||
React.Children.only(React.Children.map(
|
|
||||||
props.children,
|
this.state = {
|
||||||
child => React.cloneElement(child, { isOver: props.isOver }),
|
lastContent: null,
|
||||||
)[0])
|
history: []
|
||||||
|
}
|
||||||
}
|
}
|
||||||
</div>
|
render () {
|
||||||
|
return this.props.connectDropTarget(
|
||||||
|
createElement(
|
||||||
|
'div',
|
||||||
|
null,
|
||||||
|
this.props.children({
|
||||||
|
__monitor__: this.props.monitor,
|
||||||
|
isOver: this.props.isOver,
|
||||||
|
canDrop: this.props.canDrop,
|
||||||
|
content: this.state.lastContent,
|
||||||
|
history: this.state.history,
|
||||||
|
})
|
||||||
|
)
|
||||||
)
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export default DropTarget(NativeTypes.FILE, spec, collect)(ReadFileDropContainer)
|
module.exports = exports.default = DropTarget(NativeTypes.FILE, spec, collect)(ReadFileDropContainer)
|
||||||
|
Loading…
Reference in New Issue