@@ -13,99 +13,113 @@ const propTypes = {
13
13
htmlFor : PropTypes . string ,
14
14
cssModule : PropTypes . object ,
15
15
onChange : PropTypes . func ,
16
- children : PropTypes . oneOfType ( [ PropTypes . node , PropTypes . array , PropTypes . func ] ) ,
16
+ children : PropTypes . oneOfType ( [
17
+ PropTypes . node ,
18
+ PropTypes . array ,
19
+ PropTypes . func
20
+ ] ) ,
17
21
innerRef : PropTypes . oneOfType ( [
18
22
PropTypes . object ,
19
23
PropTypes . string ,
20
- PropTypes . func ,
24
+ PropTypes . func
21
25
] )
22
26
} ;
23
27
24
28
class CustomFileInput extends React . Component {
25
- constructor ( props ) {
26
- super ( props ) ;
29
+ constructor ( props ) {
30
+ super ( props ) ;
27
31
28
- this . state = {
29
- files :null ,
30
- } ;
32
+ this . state = {
33
+ files : null
34
+ } ;
31
35
32
- this . onChange = this . onChange . bind ( this ) ;
33
- }
34
-
35
- onChange ( e ) {
36
- let input = e . target ;
37
- let { onChange} = this . props ;
38
- let files = this . getSelectedFiles ( input ) ;
36
+ this . onChange = this . onChange . bind ( this ) ;
37
+ }
39
38
40
- if ( typeof ( onChange ) === 'function' ) {
41
- onChange ( ...arguments ) ;
42
- }
39
+ onChange ( e ) {
40
+ let input = e . target ;
41
+ let { onChange } = this . props ;
42
+ let files = this . getSelectedFiles ( input ) ;
43
43
44
- this . setState ( { files} )
44
+ if ( typeof onChange === "function" ) {
45
+ onChange ( ...arguments ) ;
45
46
}
46
47
47
- getSelectedFiles ( input ) {
48
- let { multiple } = this . props ;
48
+ this . setState ( { files } ) ;
49
+ }
49
50
50
- if ( multiple && input . files ) {
51
- let files = [ ] . slice . call ( input . files ) ;
51
+ getSelectedFiles ( input ) {
52
+ let { multiple } = this . props ;
52
53
53
- return files . map ( file => file . name ) . join ( ', ' ) ;
54
- }
54
+ if ( multiple && input . files ) {
55
+ let files = [ ] . slice . call ( input . files ) ;
55
56
56
- if ( input . value . indexOf ( 'fakepath' ) !== - 1 ) {
57
- let parts = input . value . split ( '\\' ) ;
57
+ return files . map ( file => file . name ) . join ( ", " ) ;
58
+ }
58
59
59
- return parts [ parts . length - 1 ] ;
60
- }
60
+ if ( input . value . indexOf ( "fakepath" ) !== - 1 ) {
61
+ let parts = input . value . split ( "\\" ) ;
61
62
62
- return input . value ;
63
+ return parts [ parts . length - 1 ] ;
63
64
}
64
65
65
- render ( ) {
66
- const {
67
- className,
68
- label,
69
- valid,
70
- invalid,
71
- cssModule,
72
- children,
73
- bsSize,
74
- innerRef,
75
- htmlFor,
76
- type,
77
- onChange,
78
- dataBrowse,
79
- ...attributes
80
- } = this . props ;
81
-
82
- const customClass = mapToCssModules (
83
- classNames (
84
- className ,
85
- `custom-file` ,
86
- ) ,
87
- cssModule
88
- ) ;
89
-
90
- const validationClassNames = mapToCssModules (
91
- classNames (
92
- invalid && 'is-invalid' ,
93
- valid && 'is-valid' ,
94
- ) ,
95
- cssModule
96
- ) ;
97
-
98
- const labelHtmlFor = htmlFor || attributes . id ;
99
- const { files} = this . state ;
100
-
101
- return (
102
- < div className = { customClass } >
103
- < input type = "file" { ...attributes } ref = { innerRef } className = { classNames ( validationClassNames , mapToCssModules ( 'custom-file-input' , cssModule ) ) } onChange = { this . onChange } />
104
- < label className = { mapToCssModules ( 'custom-file-label' , cssModule ) } htmlFor = { labelHtmlFor } data-browse = { dataBrowse } > { files || label || 'Choose file' } </ label >
105
- { children }
106
- </ div >
107
- ) ;
108
- }
66
+ return input . value ;
67
+ }
68
+
69
+ render ( ) {
70
+ const {
71
+ className,
72
+ label,
73
+ valid,
74
+ invalid,
75
+ cssModule,
76
+ children,
77
+ bsSize,
78
+ innerRef,
79
+ htmlFor,
80
+ type,
81
+ onChange,
82
+ dataBrowse,
83
+ hidden,
84
+ ...attributes
85
+ } = this . props ;
86
+
87
+ const customClass = mapToCssModules (
88
+ classNames ( className , `custom-file` ) ,
89
+ cssModule
90
+ ) ;
91
+
92
+ const validationClassNames = mapToCssModules (
93
+ classNames ( invalid && "is-invalid" , valid && "is-valid" ) ,
94
+ cssModule
95
+ ) ;
96
+
97
+ const labelHtmlFor = htmlFor || attributes . id ;
98
+ const { files } = this . state ;
99
+
100
+ return (
101
+ < div className = { customClass } hidden = { hidden || false } >
102
+ < input
103
+ type = "file"
104
+ { ...attributes }
105
+ ref = { innerRef }
106
+ className = { classNames (
107
+ validationClassNames ,
108
+ mapToCssModules ( "custom-file-input" , cssModule )
109
+ ) }
110
+ onChange = { this . onChange }
111
+ />
112
+ < label
113
+ className = { mapToCssModules ( "custom-file-label" , cssModule ) }
114
+ htmlFor = { labelHtmlFor }
115
+ data-browse = { dataBrowse }
116
+ >
117
+ { files || label || "Choose file" }
118
+ </ label >
119
+ { children }
120
+ </ div >
121
+ ) ;
122
+ }
109
123
}
110
124
111
125
CustomFileInput . propTypes = propTypes ;
0 commit comments