Skip to content

Commit 0cb3315

Browse files
committed
more flexible compontent by allowing children
1 parent 8bd10b5 commit 0cb3315

File tree

2 files changed

+34
-17
lines changed

2 files changed

+34
-17
lines changed

src/wasm/file_input.rs

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,16 @@ pub enum Msg {
2222

2323
#[derive(Properties, PartialEq)]
2424
pub struct Props {
25+
pub onload: Callback<FileUpload>,
26+
#[prop_or(AttrValue::Static(""))]
2527
pub accept: AttrValue,
2628
#[prop_or(false)]
2729
pub multiple: bool,
28-
pub onload: Callback<FileUpload>,
30+
#[prop_or(false)]
31+
pub drag_and_drop: bool,
2932
#[prop_or(AttrValue::Static("Drop Files Here"))]
3033
pub label: AttrValue,
34+
pub children: Option<Html>,
3135
}
3236

3337
impl Component for FileInput {
@@ -70,25 +74,38 @@ impl Component for FileInput {
7074
}
7175

7276
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+
7382
let noop_drag = Callback::from(|e: DragEvent| {
7483
e.prevent_default();
7584
});
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+
7695
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+
}
92109
</label>
93110
}
94111
}

src/wasm/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ impl Component for App {
3636
html! {
3737
<div id="wrapper">
3838
<h1>{ "Process your CGA/EGAs" }</h1>
39-
<FileInput accept="image/*,.bin,.cga,.ega" onload={ctx.link().callback( Msg::Loaded )}/>
39+
<FileInput accept="image/*,.bin,.cga,.ega" onload={ctx.link().callback( Msg::Loaded )} children={None}/>
4040
<div id="preview-area">
4141
{{ self.images.clone() }}
4242
</div>

0 commit comments

Comments
 (0)