|
| 1 | +use std::collections::HashMap; |
| 2 | + |
| 3 | +use gloo::file::callbacks::FileReader; |
| 4 | +use web_sys::HtmlInputElement; |
| 5 | +use yew::prelude::*; |
| 6 | + |
| 7 | +#[derive(Clone, PartialEq)] |
| 8 | +pub struct FileUpload { |
| 9 | + pub name: String, |
| 10 | + pub mime_type: String, |
| 11 | + pub data: Vec<u8>, |
| 12 | +} |
| 13 | + |
| 14 | +pub struct FileInput { |
| 15 | + readers: HashMap<String, FileReader>, |
| 16 | +} |
| 17 | + |
| 18 | +pub enum Msg { |
| 19 | + Loaded(FileUpload), |
| 20 | + Submit(Option<web_sys::FileList>), |
| 21 | +} |
| 22 | + |
| 23 | +#[derive(Properties, PartialEq)] |
| 24 | +pub struct Props { |
| 25 | + pub accept: AttrValue, |
| 26 | + #[prop_or(false)] |
| 27 | + pub multiple: bool, |
| 28 | + pub onload: Callback<FileUpload>, |
| 29 | + #[prop_or(AttrValue::Static("Drop Files Here"))] |
| 30 | + pub label: AttrValue, |
| 31 | +} |
| 32 | + |
| 33 | +impl Component for FileInput { |
| 34 | + type Message = Msg; |
| 35 | + type Properties = Props; |
| 36 | + |
| 37 | + fn create(_ctx: &Context<Self>) -> Self { |
| 38 | + Self { |
| 39 | + readers: HashMap::default(), |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + fn update(&mut self, ctx: &Context<Self>, msg: Self::Message) -> bool { |
| 44 | + match msg { |
| 45 | + Msg::Loaded(file) => { |
| 46 | + self.readers.remove(&file.name); |
| 47 | + ctx.props().onload.emit(file); |
| 48 | + } |
| 49 | + Msg::Submit(files) => { |
| 50 | + if let Some(files) = files { |
| 51 | + for file in gloo::file::FileList::from(files).iter() { |
| 52 | + let link = ctx.link().clone(); |
| 53 | + let name = file.name().clone(); |
| 54 | + let mime_type = file.raw_mime_type(); |
| 55 | + let task = { |
| 56 | + gloo::file::callbacks::read_as_bytes(&file, move |res| { |
| 57 | + link.send_message(Msg::Loaded(FileUpload { |
| 58 | + data: res.expect("failed to read file"), |
| 59 | + mime_type, |
| 60 | + name, |
| 61 | + })) |
| 62 | + }) |
| 63 | + }; |
| 64 | + self.readers.insert(file.name(), task); |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + true |
| 70 | + } |
| 71 | + |
| 72 | + fn view(&self, ctx: &Context<Self>) -> Html { |
| 73 | + let noop_drag = Callback::from(|e: DragEvent| { |
| 74 | + e.prevent_default(); |
| 75 | + }); |
| 76 | + 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 | + /> |
| 92 | + </label> |
| 93 | + } |
| 94 | + } |
| 95 | +} |
0 commit comments