fileButtonLineComponent.tsx 1.2 KB

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