handle fileinputs in form

This commit is contained in:
MathMan05 2024-09-09 11:12:20 -05:00
parent 46e617c9fb
commit 7dd6082f24
2 changed files with 54 additions and 4 deletions

View file

@ -691,8 +691,10 @@ class Form {
} }
return select; return select;
} }
addFileInput(label, formName, { required = false } = {}) { fileOptions = new Map();
addFileInput(label, formName, { required, files } = { required: false, files: "multi" }) {
const FI = this.options.addFileInput(label, _ => { }, {}); const FI = this.options.addFileInput(label, _ => { }, {});
this.fileOptions.set(FI, { files });
this.names.set(formName, FI); this.names.set(formName, FI);
if (required) { if (required) {
this.required.add(FI); this.required.add(FI);
@ -760,7 +762,7 @@ class Form {
this.owner.changed(); this.owner.changed();
} }
} }
submit() { async submit() {
const build = {}; const build = {};
for (const key of Object.keys(this.values)) { for (const key of Object.keys(this.values)) {
const thing = this.values[key]; const thing = this.values[key];
@ -785,6 +787,7 @@ class Form {
build[key] = thing; build[key] = thing;
} }
} }
const promises = [];
for (const thing of this.names.keys()) { for (const thing of this.names.keys()) {
if (thing === "") if (thing === "")
continue; continue;
@ -793,8 +796,31 @@ class Form {
build[thing] = input.options[input.value]; build[thing] = input.options[input.value];
continue; continue;
} }
else if (input instanceof FileInput) {
const options = this.fileOptions.get(input);
if (!options) {
throw new Error("FileInput without its options is in this form, this should never happen.");
}
if (options.files === "one") {
if (input.value) {
const reader = new FileReader();
reader.readAsDataURL(input.value[0]);
const promise = new Promise((res) => {
reader.onload = () => {
build[thing] = reader.result;
res();
};
});
promises.push(promise);
}
}
else {
console.error(options.files + " is not currently implemented");
}
}
build[thing] = input.value; build[thing] = input.value;
} }
await Promise.allSettled(promises);
if (this.fetchURL !== "") { if (this.fetchURL !== "") {
fetch(this.fetchURL, { fetch(this.fetchURL, {
method: this.method, method: this.method,

View file

@ -699,8 +699,10 @@ class Form implements OptionsElement<object>{
} }
return select; return select;
} }
addFileInput(label:string,formName:string,{required=false}={}){ readonly fileOptions:Map<FileInput,{files:"one"|"multi"}>=new Map();
addFileInput(label:string,formName:string,{required,files}:{required:boolean,files:"one"|"multi"}={required:false,files:"multi"}){
const FI=this.options.addFileInput(label,_=>{},{}); const FI=this.options.addFileInput(label,_=>{},{});
this.fileOptions.set(FI,{files});
this.names.set(formName,FI); this.names.set(formName,FI);
if(required){ if(required){
this.required.add(FI); this.required.add(FI);
@ -771,7 +773,7 @@ class Form implements OptionsElement<object>{
this.owner.changed(); this.owner.changed();
} }
} }
submit(){ async submit(){
const build={}; const build={};
for(const key of Object.keys(this.values)){ for(const key of Object.keys(this.values)){
const thing=this.values[key]; const thing=this.values[key];
@ -794,15 +796,37 @@ class Form implements OptionsElement<object>{
build[key]=thing; build[key]=thing;
} }
} }
const promises:Promise<void>[]=[];
for(const thing of this.names.keys()){ for(const thing of this.names.keys()){
if(thing==="")continue; if(thing==="")continue;
const input=this.names.get(thing) as OptionsElement<any>; const input=this.names.get(thing) as OptionsElement<any>;
if(input instanceof SelectInput){ if(input instanceof SelectInput){
build[thing]=input.options[input.value]; build[thing]=input.options[input.value];
continue; continue;
}else if(input instanceof FileInput){
const options=this.fileOptions.get(input);
if(!options){
throw new Error("FileInput without its options is in this form, this should never happen.");
}
if(options.files==="one"){
if(input.value){
const reader=new FileReader();
reader.readAsDataURL(input.value[0]);
const promise=new Promise<void>((res)=>{
reader.onload=()=>{
build[thing]=reader.result;
res();
};
})
promises.push(promise);
}
}else{
console.error(options.files+" is not currently implemented")
}
} }
build[thing]=input.value; build[thing]=input.value;
} }
await Promise.allSettled(promises);
if(this.fetchURL!==""){ if(this.fetchURL!==""){
fetch(this.fetchURL,{ fetch(this.fetchURL,{
method: this.method, method: this.method,