fileButtonLineComponent.tsx 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. import * as React from "react";
  2. interface IFileButtonLineComponentProps {
  3. label: string;
  4. onClick: (file: File) => void;
  5. accept: string;
  6. }
  7. export class FileButtonLineComponent extends React.Component<IFileButtonLineComponentProps> {
  8. private static _IDGenerator = 0;
  9. private _id = FileButtonLineComponent._IDGenerator++;
  10. private uploadInputRef: React.RefObject<HTMLInputElement>;
  11. constructor(props: IFileButtonLineComponentProps) {
  12. super(props);
  13. this.uploadInputRef = React.createRef();
  14. }
  15. onChange(evt: any) {
  16. var files: File[] = evt.target.files;
  17. if (files && files.length) {
  18. this.props.onClick(files[0]);
  19. }
  20. evt.target.value = "";
  21. }
  22. render() {
  23. return (
  24. <div className="buttonLine">
  25. <label htmlFor={"file-upload" + this._id} className="file-upload">
  26. {this.props.label}
  27. </label>
  28. <input ref={this.uploadInputRef} id={"file-upload" + this._id} type="file" accept={this.props.accept} onChange={evt => this.onChange(evt)} />
  29. </div>
  30. );
  31. }
  32. }