@@ -9,20 +9,24 @@ export default class FileInput extends React.Component {
9
9
super ( props ) ;
10
10
this . state = {
11
11
errText : "" ,
12
+ fileAdded : false ,
13
+ fileName : "" ,
14
+ fileNames : [ ]
12
15
}
13
16
}
14
17
15
18
handleOnChange = ( ev ) => {
16
- this . setState ( { errText : "" } ) ;
19
+ this . setState ( { errText : "" } ) ;
17
20
18
21
let files = [ ] ;
19
22
let error ;
20
23
21
24
const done = after ( ev . target . files . length , ( ) => {
22
25
// this.refs.file.getDOMNode().value = "";
23
26
if ( error ) {
24
- this . setState ( { errText : error } ) ;
27
+ this . setState ( { errText : error } ) ;
25
28
} else if ( this . props . onChange ) {
29
+ this . setState ( { fileAdded : true } )
26
30
this . props . onChange (
27
31
map ( files , "value" ) ,
28
32
map ( files , "data" )
@@ -37,7 +41,12 @@ export default class FileInput extends React.Component {
37
41
if ( vals . length !== 2 ) {
38
42
error = "Invalid file data" ;
39
43
} else {
40
- files . push ( { value : file . name , data : vals [ 1 ] } ) ;
44
+ files . push ( { value : file . name , data : vals [ 1 ] } ) ;
45
+ if ( this . props . multiple ) {
46
+ this . setState ( { fileNames : files . map ( file => file . value ) } )
47
+ } else {
48
+ this . setState ( { fileName : files [ 0 ] . value } )
49
+ }
41
50
}
42
51
done ( ) ;
43
52
} ;
@@ -46,32 +55,43 @@ export default class FileInput extends React.Component {
46
55
}
47
56
48
57
render ( ) {
49
-
50
58
let label ;
51
- if ( this . props . label ) {
52
- label = this . props . label ;
53
- } else if ( this . props . multiple ) {
54
- label = "Choose files" ;
55
- } else {
56
- label = "Choose file" ;
57
- }
59
+ this . props . label ? label = this . props . label : this . props . multiple
60
+ ? label = "Upload files" : label = "Upload a file" ;
61
+
58
62
59
63
return (
60
- < span >
61
- < span className = { `${ this . props . readonly ? "readonly" : "" } ${ this . props . disabled ? "disabled" : "" } ` } >
62
- < p className = "sub-header-color field-section-sub-header u-marginTop--small u-marginBottom--small" > { label } </ p >
63
- < input
64
- ref = { ( file ) => this . file = file }
65
- type = "file"
66
- name = { this . props . name }
67
- className = "form-control"
68
- onChange = { this . handleOnChange }
69
- readOnly = { this . props . readOnly }
70
- multiple = { this . props . multiple }
71
- disabled = { this . props . disabled } />
72
- </ span >
64
+ < div >
65
+ < div className = { `${ this . props . readonly ? "readonly" : "" } ${ this . props . disabled ? "disabled" : "" } ` } >
66
+ < p className = "sub-header-color field-section-sub-header u-marginTop--small u-marginBottom--small u-marginTop--15" > { label } </ p >
67
+ < div className = "flex flex-row" >
68
+ < div className = { `${ this . state . fileAdded ? "file-uploaded" : "custom-file-upload" } ` } >
69
+ < input
70
+ ref = { ( file ) => this . file = file }
71
+ type = "file"
72
+ name = { this . props . name }
73
+ className = "inputfile"
74
+ id = { `${ this . props . name } selector` }
75
+ onChange = { this . handleOnChange }
76
+ readOnly = { this . props . readOnly }
77
+ multiple = { true }
78
+ disabled = { this . props . disabled }
79
+ />
80
+ < label htmlFor = { `${ this . props . name } selector` } className = "u-position--relative" >
81
+ < span className = { `icon clickable ${ this . state . fileAdded ? "u-smallCheckGreen" : "u-ovalIcon" } u-marginRight--normal u-top--3` } > </ span >
82
+ { this . state . fileAdded ? `${ this . props . title } file selected` : `Browse files for ${ this . props . title } ` }
83
+ </ label >
84
+ </ div >
85
+ { this . state . fileAdded ?
86
+ < div className = "u-color--tuna u-marginLeft--normal" > File uploaded:
87
+ < p className = "Form-label-subtext" > { this . props . multiple ? this . state . fileNames . join ( "," ) : this . state . fileName } </ p >
88
+ < p className = "Form-label-subtext" > { this . props . getFilenamesText } </ p >
89
+ </ div >
90
+ : null }
91
+ </ div >
92
+ </ div >
73
93
< small className = "text-danger" > { this . state . errText } </ small >
74
- </ span >
94
+ </ div >
75
95
) ;
76
96
}
77
- }
97
+ }
0 commit comments