-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathCustomFileInput.js
114 lines (109 loc) · 3.21 KB
/
CustomFileInput.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
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import React from "react";
// used for making the prop types of this component
import PropTypes from "prop-types";
// @material-ui/core components
import { makeStyles } from "@material-ui/core/styles";
// core components
import CustomInput from "components/CustomInput/CustomInput.js";
import Button from "components/CustomButtons/Button.js";
import styles from "../../jss/material-kit-pro-react/components/customFileInputStyle.js";
const useStyles = makeStyles(styles);
export default function CustomFileInput(props) {
const [fileNames, setFileNames] = React.useState("");
// eslint-disable-next-line
const [files, setFiles] = React.useState(null);
let hiddenFile = React.createRef();
const onFocus = e => {
hiddenFile.current.click(e);
};
// eslint-disable-next-line
const handleSubmit = e => {
e.preventDefault();
// files is the file/image uploaded
// in this function you can save the image (files) on form submit
// you have to call it yourself
};
const addFile = e => {
let fileNames = "";
let files = e.target.files;
for (let i = 0; i < e.target.files.length; i++) {
fileNames = fileNames + e.target.files[i].name;
if (props.multiple && i !== e.target.files.length - 1) {
fileNames = fileNames + ", ";
}
}
setFiles(files);
setFileNames(fileNames);
if(props.onChange) {
props.onChange(files,fileNames);
}
};
const {
id,
endButton,
startButton,
inputProps,
formControlProps,
multiple
} = props;
const classes = useStyles();
if (inputProps && inputProps.type && inputProps.type === "file") {
inputProps.type = "text";
}
let buttonStart;
let buttonEnd;
if (startButton) {
buttonStart = (
<Button {...startButton.buttonProps}>
{startButton.icon !== undefined ? startButton.icon : null}
{startButton.text !== undefined ? startButton.text : null}
</Button>
);
}
if (endButton) {
buttonEnd = (
<Button {...endButton.buttonProps}>
{endButton.icon !== undefined ? endButton.icon : null}
{endButton.text !== undefined ? endButton.text : null}
</Button>
);
}
return (
<div className={classes.inputFileWrapper}>
<input
type="file"
className={classes.inputFile}
multiple={multiple}
ref={hiddenFile}
onChange={addFile}
/>
<CustomInput
id={id}
formControlProps={{
...formControlProps
}}
inputProps={{
...inputProps,
onClick: onFocus,
value: fileNames,
endAdornment: buttonEnd,
startAdornment: buttonStart
}}
/>
</div>
);
}
CustomFileInput.defaultProps = {
multiple: false
};
CustomFileInput.propTypes = {
id: PropTypes.string,
endButton: PropTypes.object,
startButton: PropTypes.object,
inputProps: PropTypes.object,
formControlProps: PropTypes.object,
multiple: PropTypes.bool,
// it is a function from which you can get the file that was uploaded
// more can be read here: https://github.com/creativetimofficial/ct-material-kit-pro-react/issues/64 and here: https://github.com/creativetimofficial/ct-material-kit-pro-react/issues/37
onChange: PropTypes.func
};