2017-02-17 00:46:39 +00:00
|
|
|
import React, { PureComponent } from 'react';
|
2018-11-04 06:22:46 +00:00
|
|
|
import Button from 'components/ui/Button';
|
2016-01-11 20:04:57 +00:00
|
|
|
|
2017-02-17 00:46:39 +00:00
|
|
|
export default class FileInput extends PureComponent {
|
2018-10-10 19:23:42 +00:00
|
|
|
static defaultProps = {
|
|
|
|
type: 'text'
|
|
|
|
};
|
|
|
|
|
2018-11-04 06:22:46 +00:00
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
|
2016-01-11 20:04:57 +00:00
|
|
|
this.input = window.document.createElement('input');
|
|
|
|
this.input.setAttribute('type', 'file');
|
|
|
|
|
|
|
|
this.input.addEventListener('change', e => {
|
|
|
|
const file = e.target.files[0];
|
|
|
|
const reader = new FileReader();
|
2018-10-10 19:23:42 +00:00
|
|
|
const { onChange, type } = this.props;
|
2016-01-11 20:04:57 +00:00
|
|
|
|
|
|
|
reader.onload = () => {
|
2018-10-10 19:23:42 +00:00
|
|
|
onChange(file.name, reader.result);
|
2016-01-11 20:04:57 +00:00
|
|
|
};
|
|
|
|
|
2018-10-10 19:23:42 +00:00
|
|
|
switch (type) {
|
|
|
|
case 'binary':
|
|
|
|
reader.readAsArrayBuffer(file);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'text':
|
|
|
|
reader.readAsText(file);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
reader.readAsText(file);
|
|
|
|
}
|
2016-01-11 20:04:57 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
handleClick = () => this.input.click();
|
|
|
|
|
|
|
|
render() {
|
|
|
|
return (
|
2018-11-04 06:22:46 +00:00
|
|
|
<Button className="input-file" onClick={this.handleClick}>
|
2018-04-05 23:46:22 +00:00
|
|
|
{this.props.name}
|
2018-11-04 06:22:46 +00:00
|
|
|
</Button>
|
2016-01-11 20:04:57 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|