Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// Copyright 2015-2018 Parity Technologies (UK) Ltd.
// This file is part of Parity.
//
// SPDX-License-Identifier: BSD-3-Clause
import React from 'react';
import Dropzone from 'react-dropzone';
import PropTypes from 'prop-types';
export class InputFile extends React.PureComponent {
static propTypes = {
label: PropTypes.string.isRequired,
onChangeFile: PropTypes.func.isRequired,
required: PropTypes.bool,
value: PropTypes.any
};
state = {
file: {
name: '',
size: 0
}
};
onDrop = files => {
const { onChangeFile } = this.props;
files.forEach(file => {
const reader = new FileReader();
reader.onabort = () => {
// ignore
};
reader.onerror = () => {
// ignore
};
reader.onload = evt => {
const data = evt.target.result;
onChangeFile && onChangeFile(evt);
this.setState({
file: {
name: file.name,
size: data.length
}
});
};
reader.readAsText(file);
});
};
render () {
const acceptedFormats = ['application/json', 'text/plain'].join(', ');
const { label } = this.props;
const { file } = this.state;
return (
<Dropzone
accept={acceptedFormats}
className='form_field'
disabled={false}
multiple={false}
onDrop={this.onDrop}
>
<div className='form_field_value'>
<label className='label'> {label} </label>
<div className='dropzone -md'>
{!file ? 'Drag and drop the file here' : file.name}
</div>
</div>
</Dropzone>
);
}
}