Skip to content

Commit

Permalink
Merge pull request #15 from lionng429/react-16-support
Browse files Browse the repository at this point in the history
React 16 support
  • Loading branch information
lionng429 authored Mar 26, 2018
2 parents d3ed6d9 + e9cb8e3 commit 825e1dc
Show file tree
Hide file tree
Showing 11 changed files with 610 additions and 524 deletions.
2 changes: 1 addition & 1 deletion .babelrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"presets": ['es2015', 'react', 'stage-0']
"presets": ['es2015', 'react']
}
20 changes: 19 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
{
"extends": "airbnb"
"env": {
"browser": true,
"node": true,
"es6": true
},
"parserOptions": {
"ecmaVersion": 6,
"sourceType": "module",
"ecmaFeatures": {
"jsx": true
}
},
"plugins": [
"react"
],
"extends": [
"eslint:recommended",
"plugin:react/recommended"
]
}
307 changes: 156 additions & 151 deletions examples/basic/client.js
Original file line number Diff line number Diff line change
@@ -1,162 +1,167 @@
var React = require('react');
var ReactDOM = require('react-dom');
var FileUploader = require('../../lib');
var _ = require('lodash');

var MyComponent = React.createClass({
getInitialState: function() {
return {
isPanelOpen: false,
isDragOver: false,
files: []
}
},

openPanel: function() {
this.setState({ isPanelOpen: true });
},

closePanel: function() {
this.setState({ isPanelOpen: false });
},

onDragOver: function(e) {
// your codes here:
// if you want to check if the files are dragged over
// a specific DOM node
//if (e.target === node) {
// this.setState({
// isDragOver: true
// });
//}
},

onFileDrop: function({ target }, files) {
let node = ReactDOM.findDOMNode(this.refs.uploadPanel);
if (target != node) {
return false;
}

files.map(function(_file) {
if (_file.size > 1000 * 1000) {
_file.status = FileUploader.status.FAILED;
_file.error = 'file size exceeded maximum'
}
});
import React, { Component } from 'react';
import ReactDOM from 'react-dom';
import _ from 'lodash';
import * as FileUploader from '../../src/index';

class MyComponent extends Component {
constructor(props) {
super(props);

this.state = {
isPanelOpen: false,
isDragOver: false,
files: [],
};

this.uploadPanel = undefined;
this.openPanel = this.openPanel.bind(this);
this.closePanel = this.closePanel.bind(this);
this.onDragOver = this.onDragOver.bind(this);
this.onFileDrop = this.onFileDrop.bind(this);
this.onFileProgress = this.onFileProgress.bind(this);
this.onFileUpdate = this.onFileUpdate.bind(this);
this.setUploadPanelRef = this.setUploadPanelRef.bind(this);
}

openPanel() {
this.setState({ isPanelOpen: true });
}

closePanel() {
this.setState({ isPanelOpen: false });
}

onDragOver(e) {
// your codes here:
// if you want to check if the files are dragged over
// a specific DOM node
const node = ReactDOM.findDOMNode(this.uploadPanel);

if (this.state.isDragOver !== (e.target === node)) {
this.setState({
isDragOver: e.target === node,
});
}
}

this.setState({
files: this.state.files.concat(files)
});
onFileDrop({ target }, files) {
const node = ReactDOM.findDOMNode(this.uploadPanel);

// if you want to close the panel upon file drop
this.closePanel();
},
if (target !== node) {
this.closePanel();
return false;
}

onFileProgress: function(file) {
var files = this.state.files;
files.forEach(item => {
if (item.size > 1000 * 1000) {
item.status = FileUploader.status.FAILED;
item.error = 'file size exceeded maximum';
}
});

files.map(function(_file) {
if (_file.id === file.id) {
_file = file;
}
this.setState({
files: this.state.files.concat(files),
});

// if you want to close the panel upon file drop
this.closePanel();
}

onFileProgress(file) {
const { files = [] } = this.state;
const newFiles = files.map(item => item.id === file.id ? file : item);

this.setState({
files: files
files: newFiles,
});
},
}

onFileUpdate: function(file) {
var files = this.state.files;
onFileUpdate(file) {
const { files = [] } = this.state;
const newFiles = files.map(item => item.id === file.id ? file : item);

files.map(function(_file) {
if (_file.id === file.id) {
_file = file;
}
});

this.setState({
files: files
});
},

_getStatusString: function(status) {
switch (status) {
case -1:
return 'failed';
break;

case 0:
return 'pending';
break;

case 1:
return 'uploading';
break;

case 2:
return 'uploaded';
break;
}
},

render: function() {
var _this = this;

return (
<div>
<h1>{ this.props.title }</h1>
<p>You can upload files with size with 1 MB at maximum</p>
<FileUploader.Receiver
ref="uploadPanel"
customClass="upload-panel"
isOpen={this.state.isPanelOpen}
onDragEnter={this.openPanel}
onDragOver={this.onDragOver}
onDragLeave={this.closePanel}
onFileDrop={this.onFileDrop} >
<p>
{
!this.state.isDragOver ? 'Drop here' : 'Files detected'
}
</p>
</FileUploader.Receiver>
<div>
<p>Upload List</p>
<FileUploader.UploadManager
customClass="upload-list"
files={this.state.files}
uploadUrl="/upload"
onUploadStart={this.onFileUpdate}
onUploadProgress={_.debounce(this.onFileProgress, 150)}
onUploadEnd={this.onFileUpdate}
>
{
this.state.files.map(function(file, index) {
return (
<FileUploader.UploadHandler key={index} file={file} autoStart={true}>
<dl>
<dh>{ file.name }</dh>
<dd>
<span className="file__id">{ file.id } </span>
<span className="file__type">{ file.type } </span>
<span className="file__size">{ file.size / 1000 / 1000 } MB</span>
<span className="file__progress">{ file.progress }%</span>
<span className="file__status">
{_this._getStatusString(file.status)}
</span>
<span className="file__error">{ file.error }</span>
</dd>
</dl>
</FileUploader.UploadHandler>
)
})
}
</FileUploader.UploadManager>
</div>
</div>
);
}
});
this.setState({
files: newFiles,
});
}

setUploadPanelRef(ref) {
this.uploadPanel = ref;
}

getStatusString(status) {
switch (status) {
case -1:
return 'failed';

case 0:
return 'pending';

case 1:
return 'uploading';

ReactDOM.render(<MyComponent title="react-file-uploader" />, document.getElementById('app'));
case 2:
return 'uploaded';

default:
return '';
}
}

render() {
return (
<div>
<h1>{ this.props.title }</h1>
<p>You can upload files with size with 1 MB at maximum</p>
<FileUploader.Receiver
ref={this.setUploadPanelRef}
customClass="upload-panel"
isOpen={this.state.isPanelOpen}
onDragEnter={this.openPanel}
onDragOver={this.onDragOver}
onDragLeave={this.closePanel}
onFileDrop={this.onFileDrop}
>
<p>
{
!this.state.isDragOver ? 'Drop here' : 'Files detected'
}
</p>
</FileUploader.Receiver>
<div>
<p>Upload List</p>
<FileUploader.UploadManager
customClass="upload-list"
files={this.state.files}
uploadUrl="/upload"
onUploadStart={this.onFileUpdate}
onUploadProgress={_.debounce(this.onFileProgress, 150)}
onUploadEnd={this.onFileUpdate}
>
{
this.state.files.map((file, index) => (
<FileUploader.UploadHandler key={index} file={file} autoStart>
<dl>
<dt>{file.name}</dt>
<dd>
<span className="file__id">{file.id} </span>
<span className="file__type">{file.type} </span>
<span className="file__size">{file.size / 1000 / 1000} MB</span>
<span className="file__progress">{file.progress}%</span>
<span className="file__status">
{this.getStatusString(file.status)}
</span>
<span className="file__error">{file.error}</span>
</dd>
</dl>
</FileUploader.UploadHandler>
))
}
</FileUploader.UploadManager>
</div>
</div>
);
}
}

ReactDOM.render(<MyComponent title="react-file-uploader" />, document.getElementById('app'));
6 changes: 3 additions & 3 deletions examples/basic/package.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
{
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"build": "browserify client.js -o bundle.js -t [ babelify --presets [ es2015 react] ]"
"build": "browserify client.js -o bundle.js -t [ babelify --presets [ es2015 react ] ]"
},
"dependencies": {
"connect-multiparty": "^2.0.0",
"express": "^4.13.3",
"forever": "^0.15.1",
"lodash": "^4.13.1",
"react": "^0.14.3",
"react-dom": "^0.14.3"
"react": "^16.0.0",
"react-dom": "^16.0.0"
},
"devDependencies": {
"babel-preset-es2015": "^6.1.18",
Expand Down
Loading

0 comments on commit 825e1dc

Please sign in to comment.