@@ -22,12 +22,16 @@ pub enum Msg {
22
22
23
23
#[ derive( Properties , PartialEq ) ]
24
24
pub struct Props {
25
+ pub onload : Callback < FileUpload > ,
26
+ #[ prop_or( AttrValue :: Static ( "" ) ) ]
25
27
pub accept : AttrValue ,
26
28
#[ prop_or( false ) ]
27
29
pub multiple : bool ,
28
- pub onload : Callback < FileUpload > ,
30
+ #[ prop_or( false ) ]
31
+ pub drag_and_drop : bool ,
29
32
#[ prop_or( AttrValue :: Static ( "Drop Files Here" ) ) ]
30
33
pub label : AttrValue ,
34
+ pub children : Option < Html > ,
31
35
}
32
36
33
37
impl Component for FileInput {
@@ -70,25 +74,38 @@ impl Component for FileInput {
70
74
}
71
75
72
76
fn view ( & self , ctx : & Context < Self > ) -> Html {
77
+ let label_text = & ctx. props ( ) . label ;
78
+ let children = ctx. props ( ) . children . clone ( ) ;
79
+ let accept = & ctx. props ( ) . accept ;
80
+ let multiple = ctx. props ( ) . multiple ;
81
+
73
82
let noop_drag = Callback :: from ( |e : DragEvent | {
74
83
e. prevent_default ( ) ;
75
84
} ) ;
85
+ let onchange = ctx. link ( ) . callback ( |e : Event | {
86
+ e. prevent_default ( ) ;
87
+ let input: HtmlInputElement = e. target_unchecked_into ( ) ;
88
+ Msg :: Submit ( input. files ( ) )
89
+ } ) ;
90
+ let ondrop = ctx. link ( ) . callback ( |event : DragEvent | {
91
+ event. prevent_default ( ) ;
92
+ Msg :: Submit ( event. data_transfer ( ) . unwrap ( ) . files ( ) )
93
+ } ) ;
94
+
76
95
html ! {
77
- <label class="drop-container" ondragover={ & noop_drag} ondragenter={ & noop_drag}
78
- ondrop={ ctx. link( ) . callback( |event: DragEvent | {
79
- event. prevent_default( ) ;
80
- Msg :: Submit ( event. data_transfer( ) . unwrap( ) . files( ) )
81
- } ) }
82
- ><i>{ ctx. props( ) . label. clone( ) } </i>
83
- <input
84
- type ="file"
85
- accept="{ ctx.props().accept }"
86
- multiple={ ctx. props( ) . multiple }
87
- onchange={ ctx. link( ) . callback( |e: Event | {
88
- let input: HtmlInputElement = e. target_unchecked_into( ) ;
89
- Msg :: Submit ( input. files( ) )
90
- } ) }
91
- />
96
+ <label>
97
+ if ctx. props( ) . drag_and_drop {
98
+ <i class="drop-container" { ondrop } onchange={ & onchange } ondragover={ & noop_drag } ondragenter={ & noop_drag } >
99
+ { label_text. clone( ) }
100
+ </i>
101
+ } else {
102
+ <i>{ label_text. clone( ) } </i>
103
+ }
104
+ if children. is_none( ) {
105
+ <input type ="file" { accept } { multiple } { onchange } />
106
+ } else {
107
+ { children }
108
+ }
92
109
</label>
93
110
}
94
111
}
0 commit comments