-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathindex.tsx
68 lines (63 loc) · 1.39 KB
/
index.tsx
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
/**
* WordPress dependencies
*/
import { useRef } from '@wordpress/element';
/**
* Internal dependencies
*/
import Button from '../button';
import type { WordPressComponentProps } from '../context';
import type { FormFileUploadProps } from './types';
/**
* FormFileUpload is a component that allows users to select files from their local device.
*
* ```jsx
* import { FormFileUpload } from '@wordpress/components';
*
* const MyFormFileUpload = () => (
* <FormFileUpload
* accept="image/*"
* onChange={ ( event ) => console.log( event.currentTarget.files ) }
* >
* Upload
* </FormFileUpload>
* );
* ```
*/
export function FormFileUpload( {
accept,
children,
multiple = false,
onChange,
onClick,
render,
...props
}: WordPressComponentProps< FormFileUploadProps, 'button', false > ) {
const ref = useRef< HTMLInputElement >( null );
const openFileDialog = () => {
ref.current?.click();
};
const ui = render ? (
render( { openFileDialog } )
) : (
<Button onClick={ openFileDialog } { ...props }>
{ children }
</Button>
);
return (
<div className="components-form-file-upload">
{ ui }
<input
type="file"
ref={ ref }
multiple={ multiple }
style={ { display: 'none' } }
accept={ accept }
onChange={ onChange }
onClick={ onClick }
data-testid="form-file-upload-input"
/>
</div>
);
}
export default FormFileUpload;