-
Notifications
You must be signed in to change notification settings - Fork 107
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #102 from MassiminoilTrace/master
File engine documentation
- Loading branch information
Showing
4 changed files
with
113 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
#![allow(non_snake_case)] | ||
use dioxus::prelude::*; | ||
|
||
// ANCHOR: component | ||
pub fn App(cx: Scope) -> Element { | ||
// ANCHOR: rsx | ||
let filenames: &UseRef<Vec<String>> = use_ref(cx, Vec::new); | ||
cx.render(rsx! { | ||
input { | ||
// tell the input to pick a file | ||
r#type:"file", | ||
// list the accepted extensions | ||
accept: ".txt, .rs", | ||
// pick multiple files | ||
multiple: true, | ||
onchange: |evt| { | ||
if let Some(file_engine) = &evt.files { | ||
let files = file_engine.files(); | ||
for file_name in &files { | ||
filenames.write().push(file_name); | ||
} | ||
} | ||
} | ||
} | ||
}) | ||
// ANCHOR_END: rsx | ||
} | ||
// ANCHOR_END: component |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#![allow(non_snake_case)] | ||
use dioxus::prelude::*; | ||
|
||
// ANCHOR: component | ||
pub fn App(cx: Scope) -> Element { | ||
let files_uploaded: &UseRef<Vec<String>> = use_ref(cx, Vec::new); | ||
|
||
cx.render(rsx! { | ||
input { | ||
r#type:"file", | ||
accept: ".txt, .rs", | ||
multiple: true, | ||
// ANCHOR: onchange_event | ||
onchange: |evt| { | ||
// A helper macro to use hooks in async environments | ||
to_owned![files_uploaded]; | ||
async move { | ||
if let Some(file_engine) = &evt.files { | ||
let files = file_engine.files(); | ||
for file_name in &files { | ||
// Make sure to use async/await when doing heavy I/O operations, | ||
// to not freeze the interface in the meantime | ||
if let Some(file) = file_engine.read_file_to_string(file_name).await{ | ||
files_uploaded.write().push(file); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
// ANCHOR_END: onchange_event | ||
} | ||
}) | ||
} | ||
// ANCHOR_END: component |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
#![allow(non_snake_case)] | ||
use dioxus::prelude::*; | ||
|
||
// ANCHOR: component | ||
pub fn App(cx: Scope) -> Element { | ||
let filenames: &UseRef<Vec<String>> = use_ref(cx, Vec::new); | ||
cx.render(rsx! { | ||
// ANCHOR: rsx | ||
input { | ||
r#type:"file", | ||
// Select a folder by setting the directory attribute | ||
directory: true, | ||
onchange: |evt| { | ||
if let Some(file_engine) = &evt.files { | ||
let files = file_engine.files(); | ||
for file_name in &files { | ||
println!("{}", file_name); | ||
// Do something with the folder path | ||
} | ||
} | ||
} | ||
} | ||
// ANCHOR_END: rsx | ||
}) | ||
} | ||
// ANCHOR_END: component |