1 | import React, { useRef } from 'react';
|
2 | import PropTypes from 'prop-types';
|
3 | import { Button, Input } from 'reactstrap';
|
4 | import FilePicker from './FilePicker';
|
5 |
|
6 | const FilePickerBtn = ({
|
7 | onClick,
|
8 | onChange,
|
9 | multiple,
|
10 | name,
|
11 | allowedFileTypes,
|
12 | maxSize,
|
13 | 'data-testid': testId,
|
14 | ...rest
|
15 | }) => {
|
16 | const input = useRef(null);
|
17 |
|
18 | const onBtnClick = (...args) => {
|
19 | if (input.current) {
|
20 | input.current.click();
|
21 | }
|
22 | if (onClick) {
|
23 | onClick(...args);
|
24 | }
|
25 | };
|
26 |
|
27 | return (
|
28 | <>
|
29 | <div className="d-none">
|
30 | <FilePicker
|
31 | tag={Input}
|
32 | innerRef={input}
|
33 | onChange={onChange}
|
34 | multiple={multiple}
|
35 | name={name}
|
36 | allowedFileTypes={allowedFileTypes}
|
37 | maxSize={maxSize}
|
38 | data-testid={testId}
|
39 | />
|
40 | </div>
|
41 | <Button {...rest} onClick={onBtnClick} />
|
42 | </>
|
43 | );
|
44 | };
|
45 |
|
46 | FilePickerBtn.propTypes = {
|
47 | onClick: PropTypes.func,
|
48 | onChange: PropTypes.func,
|
49 | multiple: PropTypes.bool,
|
50 | name: PropTypes.string,
|
51 | color: PropTypes.string,
|
52 | children: PropTypes.node,
|
53 | allowedFileTypes: PropTypes.arrayOf(PropTypes.string),
|
54 | maxSize: PropTypes.number,
|
55 | 'data-testid': PropTypes.string,
|
56 | };
|
57 |
|
58 | FilePickerBtn.defaultProps = {
|
59 | color: 'primary',
|
60 | children: 'Select File',
|
61 | };
|
62 |
|
63 | export default FilePickerBtn;
|