You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
carbon/components/ReadFileDropContainer.js

65 lines
1.6 KiB
JavaScript

const { createElement, Component } = require('react')
const { DropTarget } = require('react-dnd')
const { NativeTypes } = require('react-dnd-html5-backend')
const spec = {
drop(props, monitor, component) {
7 years ago
const {files} = monitor.getItem()
Promise.all(
7 years ago
files
7 years ago
.filter(props.filter || (i => i))
.map(file => {
const reader = new FileReader()
return new Promise((resolve, reject) => {
7 years ago
reader.onload = event => {
file.content = event.target.result
resolve(file)
}
7 years ago
reader.readAsText(file, 'UTF-8');
})
})
7 years ago
).then(files => {
component.setState(state => ({
7 years ago
lastContent: files,
history: [files, ...state.history]
}))
7 years ago
props.onDrop(files)
})
}
}
const collect = (connect, monitor) => ({
connectDropTarget: connect.dropTarget(),
monitor,
isOver: monitor.isOver(),
canDrop: monitor.canDrop()
})
class ReadFileDropContainer extends Component {
constructor(props) {
super(props)
this.state = {
lastContent: null,
history: []
}
}
render () {
return this.props.connectDropTarget(
createElement(
'div',
null,
this.props.children({
__monitor__: this.props.monitor,
isOver: this.props.isOver,
canDrop: this.props.canDrop,
7 years ago
files: this.state.lastContent,
history: this.state.history,
})
)
)
}
}
module.exports = exports.default = DropTarget(NativeTypes.FILE, spec, collect)(ReadFileDropContainer)