You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
UnitCommitment.jl/web/src/components/Common/Buttons/FileUploadElement.tsx

45 lines
1.2 KiB

/*
* UnitCommitment.jl: Optimization Package for Security-Constrained Unit Commitment
* Copyright (C) 2020-2025, UChicago Argonne, LLC. All rights reserved.
* Released under the modified BSD license. See COPYING.md for more details.
*/
import React, { Component } from "react";
class FileUploadElement extends Component<any> {
private inputRef = React.createRef<HTMLInputElement>();
private callback: (data: any) => void = () => {};
showFilePicker = (callback: (data: any) => void) => {
this.callback = callback;
this.inputRef.current?.click();
};
onFileSelected = (event: React.ChangeEvent<HTMLInputElement>) => {
const file = event.target.files![0];
if (file) {
const reader = new FileReader();
reader.onload = async (e) => {
this.callback(e.target?.result as string);
this.callback = () => {};
};
reader.readAsText(file);
}
event.target.value = "";
};
override render() {
return (
<input
ref={this.inputRef}
type="file"
accept={this.props.accept}
style={{ display: "none" }}
onChange={this.onFileSelected}
/>
);
}
}
export default FileUploadElement;