-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from lionng429/react-16-support
React 16 support
- Loading branch information
Showing
11 changed files
with
610 additions
and
524 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
{ | ||
"presets": ['es2015', 'react', 'stage-0'] | ||
"presets": ['es2015', 'react'] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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')); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.