DioxusLabs / dioxus

Fullstack GUI library for web, desktop, mobile, and more.
https://dioxuslabs.com
Apache License 2.0
18.5k stars 704 forks source link

impl HasFileData for FormData #2346

Closed rogusdev closed 1 week ago

rogusdev commented 2 weeks ago

This allows me to write a function that can accept both Event<DragData> and Event<FormData> as Event<T: HasFileData> -- without this PR, DragData has impl HasFileData such that I can do event.files() but while FormData can do event.files() it is because that is explicitly a part of FormData, not with an impl HasFileData so each event would need to be handled separately. Now they can be handled together.

I am tempted to remove the impl FormData ... fn files() (linked above) and let this new impl HasFileData take over, but I am nervous that this might break something somewhere. Please correct me if I am wrong and I will remove the old impl.

Sample fn for purpose:

async fn handle_upload<T: HasFileData>(
    evt: Event<T>,
) {
    if let Some(file_engine) = evt.files() {
        for filename in file_engine.files() {
            if let Some(bytes) = file_engine.read_file(&filename).await {
                // ...
            }
        }
    }
}