-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
index.js
43 lines (39 loc) · 1.34 KB
/
index.js
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
import React from 'react';
import propTypes from './AttachmentPickerPropTypes';
/**
* This component renders a function as a child and
* returns a "show attachment picker" method that takes
* a callback. This is the web/mWeb/desktop version since
* on a Browser we must append a hidden input to the DOM
* and listen to onChange event.
*/
class AttachmentPicker extends React.Component {
render() {
return (
<>
<input
hidden
type="file"
ref={el => this.fileInput = el}
onChange={(e) => {
const file = e.target.files[0];
if (file) {
file.uri = URL.createObjectURL(file);
this.onPicked(file);
}
// Cleanup after selecting a file to start from a fresh state
this.fileInput.value = null;
}}
/>
{this.props.children({
openPicker: ({onPicked}) => {
this.onPicked = onPicked;
this.fileInput.click();
},
})}
</>
);
}
}
AttachmentPicker.propTypes = propTypes;
export default AttachmentPicker;