23 November 2019

An example of a simple dropzone using Semantic UI. Is achieved by using "react-dropzone", which does not provide theming.

Source code viewer
  1. import Dropzone from 'react-dropzone';
  2.  
  3. class ClaimsAddPrivate extends React.PureComponent {
  4. constructor(props) {
  5. super(props);
  6. this.initialState = {
  7. files: [],
  8. };
  9. this.fileInputCallback = this.fileInputCallback.bind(this);
  10. }
  11.  
  12. fileInputCallback(acceptedFiles) {
  13. acceptedFiles.forEach(file => {
  14. const formData = new FormData();
  15. formData.append('file', file);
  16. fetch('/addFile', {
  17. method: 'POST',
  18. body: formData,
  19. })
  20. .then(response => response.json().then(data => {
  21. this.setState({files: [...this.state.files, data]});
  22. }));
  23. });
  24. }
  25.  
  26. render() {
  27. <Dropzone onDrop={this.fileInputCallback}>
  28. {({getRootProps, getInputProps}) => (
  29. <div className="ui center aligned tertiary blue inverted segment" {...getRootProps()}>
  30. <input {...getInputProps()} />
  31. <Icon name="cloud upload" size="big" />
  32. <div>Drag and drop or upload file</div>
  33. </div>
  34. )}
  35. </Dropzone>
  36. {!_isEmpty(this.state.files) && <List divided verticalAlign="middle">
  37. {this.state.files.map((file, i) =>
  38. <List.Item key={i}>
  39. <List.Icon name="file" />
  40. <List.Content>
  41. <a href={file.url}>{file.name}</a>
  42. <Icon
  43. link
  44. color="red"
  45. name="remove"
  46. onClick={() => this.setState({files: [...this.state.files].splice(i, 1)})} />
  47. </List.Content>
  48. </List.Item>
  49. )}
  50. </List>}
  51. }
  52.  
  53. }
Programming Language: ECMAScript