lineWithFileButtonComponent.tsx 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import * as React from "react";
  2. import { DataStorage } from 'babylonjs/Misc/dataStorage';
  3. interface ILineWithFileButtonComponentProps {
  4. title: string;
  5. closed?: boolean;
  6. label: string;
  7. iconImage: any;
  8. onIconClick: (file: File) => void;
  9. accept: string;
  10. uploadName?: string;
  11. }
  12. export class LineWithFileButtonComponent extends React.Component<ILineWithFileButtonComponentProps, { isExpanded: boolean }> {
  13. private uploadRef: React.RefObject<HTMLInputElement>
  14. constructor(props: ILineWithFileButtonComponentProps) {
  15. super(props);
  16. let initialState = DataStorage.ReadBoolean(this.props.title, !this.props.closed);
  17. this.state = { isExpanded: initialState };
  18. this.uploadRef = React.createRef();
  19. }
  20. onChange(evt: any) {
  21. var files: File[] = evt.target.files;
  22. if (files && files.length) {
  23. this.props.onIconClick(files[0]);
  24. }
  25. evt.target.value = "";
  26. }
  27. switchExpandedState(): void {
  28. const newState = !this.state.isExpanded;
  29. DataStorage.WriteBoolean(this.props.title, newState);
  30. this.setState({ isExpanded: newState });
  31. }
  32. render() {
  33. return (
  34. <div className="nonDraggableLine withButton">
  35. {this.props.label}
  36. <div className="icon" title={this.props.title}>
  37. <img className="img" src={this.props.iconImage}/>
  38. </div>
  39. <div className="buttonLine" title={this.props.title}>
  40. <label htmlFor={this.props.uploadName ? this.props.uploadName : "file-upload"} className="file-upload"/>
  41. <input ref={this.uploadRef} id={this.props.uploadName ? this.props.uploadName : "file-upload"} type="file" accept={this.props.accept} onChange={evt => this.onChange(evt)} />
  42. </div>
  43. </div>
  44. );
  45. }
  46. }