14
14
// You should have received a copy of the GNU Affero General Public License
15
15
// along with this program. If not, see <http://www.gnu.org/licenses/>.
16
16
17
- import React from "react" ;
17
+ import React , { useState } from "react" ;
18
+ import get from "lodash/get" ;
18
19
import { Grid , InputLabel , Tooltip } from "@material-ui/core" ;
20
+ import IconButton from "@material-ui/core/IconButton" ;
21
+ import AttachFileIcon from "@material-ui/icons/AttachFile" ;
22
+ import CancelIcon from "@material-ui/icons/Cancel" ;
19
23
import HelpIcon from "@material-ui/icons/Help" ;
20
24
import { createStyles , Theme , withStyles } from "@material-ui/core/styles" ;
21
25
import { fieldBasic , tooltipHelper } from "../common/styleLibrary" ;
@@ -24,14 +28,15 @@ import { fileProcess } from "./utils";
24
28
interface InputBoxProps {
25
29
label : string ;
26
30
classes : any ;
27
- onChange : ( e : string ) => void ;
31
+ onChange : ( e : string , i : string ) => void ;
28
32
id : string ;
29
33
name : string ;
30
34
disabled ?: boolean ;
31
35
tooltip ?: string ;
32
36
required ?: boolean ;
33
37
error ?: string ;
34
38
accept ?: string ;
39
+ value ?: string ;
35
40
}
36
41
37
42
const styles = ( theme : Theme ) =>
@@ -41,6 +46,7 @@ const styles = (theme: Theme) =>
41
46
textBoxContainer : {
42
47
flexGrow : 1 ,
43
48
position : "relative" ,
49
+ flexDirection : "column" ,
44
50
} ,
45
51
errorState : {
46
52
color : "#b53b4b" ,
@@ -49,6 +55,27 @@ const styles = (theme: Theme) =>
49
55
top : 7 ,
50
56
right : 7 ,
51
57
} ,
58
+ errorText : {
59
+ margin : "0" ,
60
+ fontSize : "0.75rem" ,
61
+ marginTop : 3 ,
62
+ textAlign : "left" ,
63
+ fontFamily : "Lato,sans-serif" ,
64
+ fontWeight : 400 ,
65
+ lineHeight : "1.66" ,
66
+ color : "#dc1f2e" ,
67
+ } ,
68
+ valueString : {
69
+ maxWidth : 350 ,
70
+ whiteSpace : "nowrap" ,
71
+ overflow : "hidden" ,
72
+ textOverflow : "ellipsis" ,
73
+ marginTop : 2 ,
74
+ } ,
75
+ fileReselect : {
76
+ display : "flex" ,
77
+ alignItems : "center" ,
78
+ } ,
52
79
} ) ;
53
80
54
81
const FileSelector = ( {
@@ -62,7 +89,10 @@ const FileSelector = ({
62
89
required,
63
90
error = "" ,
64
91
accept = "" ,
92
+ value = "" ,
65
93
} : InputBoxProps ) => {
94
+ const [ showFileSelector , setShowSelector ] = useState ( false ) ;
95
+
66
96
return (
67
97
< React . Fragment >
68
98
< Grid
@@ -92,19 +122,62 @@ const FileSelector = ({
92
122
) }
93
123
</ InputLabel >
94
124
) }
95
- < div className = { classes . textBoxContainer } >
96
- < input
97
- type = "file"
98
- name = { name }
99
- onChange = { ( e ) => {
100
- fileProcess ( e , ( data : any ) => {
101
- onChange ( data ) ;
102
- } ) ;
103
- } }
104
- accept = { accept }
105
- required
106
- />
107
- </ div >
125
+
126
+ { showFileSelector || value === "" ? (
127
+ < div className = { classes . textBoxContainer } >
128
+ < input
129
+ type = "file"
130
+ name = { name }
131
+ onChange = { ( e ) => {
132
+ const fileName = get ( e , "target.files[0].name" , "" ) ;
133
+ fileProcess ( e , ( data : any ) => {
134
+ onChange ( data , fileName ) ;
135
+ } ) ;
136
+ } }
137
+ accept = { accept }
138
+ required = { required }
139
+ disabled = { disabled }
140
+ />
141
+
142
+ { value !== "" && (
143
+ < IconButton
144
+ color = "primary"
145
+ aria-label = "upload picture"
146
+ component = "span"
147
+ onClick = { ( ) => {
148
+ setShowSelector ( false ) ;
149
+ } }
150
+ disableRipple = { false }
151
+ disableFocusRipple = { false }
152
+ >
153
+ < CancelIcon />
154
+ </ IconButton >
155
+ ) }
156
+
157
+ { error !== "" && (
158
+ < React . Fragment >
159
+ < br />
160
+ < span className = { classes . errorText } > { error } </ span >
161
+ </ React . Fragment >
162
+ ) }
163
+ </ div >
164
+ ) : (
165
+ < div className = { classes . fileReselect } >
166
+ < div className = { classes . valueString } > { value } </ div >
167
+ < IconButton
168
+ color = "primary"
169
+ aria-label = "upload picture"
170
+ component = "span"
171
+ onClick = { ( ) => {
172
+ setShowSelector ( true ) ;
173
+ } }
174
+ disableRipple = { false }
175
+ disableFocusRipple = { false }
176
+ >
177
+ < AttachFileIcon />
178
+ </ IconButton >
179
+ </ div >
180
+ ) }
108
181
</ Grid >
109
182
</ React . Fragment >
110
183
) ;
0 commit comments