|
1 | | -import React from "react"; |
| 1 | +import React, { createRef, RefObject } from "react"; |
| 2 | +import * as FileState from "./FileState"; |
2 | 3 |
|
3 | 4 | interface IReactUploaderSkeletonProps { |
4 | | - anyText: string; |
| 5 | + anyText?: string; |
| 6 | + onClick?: () => void; |
| 7 | +} |
| 8 | + |
| 9 | +interface IUploaderFileData { |
| 10 | + id: number | string; |
| 11 | + state: string; |
| 12 | + url?: string; |
| 13 | + fileData?: File; |
| 14 | +} |
| 15 | + |
| 16 | +interface IReactUploaderSkeletonState { |
| 17 | + currentFiles: IUploaderFileData[]; |
5 | 18 | } |
6 | 19 |
|
7 | 20 | class ReactUploaderSkeleton extends React.Component< |
8 | | - IReactUploaderSkeletonProps |
| 21 | + IReactUploaderSkeletonProps, |
| 22 | + IReactUploaderSkeletonState |
9 | 23 | > { |
| 24 | + public static defaultProps = {}; |
| 25 | + |
| 26 | + public readonly state: IReactUploaderSkeletonState = { |
| 27 | + currentFiles: [] |
| 28 | + }; |
| 29 | + |
| 30 | + private fileInputRef: RefObject<HTMLInputElement>; |
| 31 | + |
| 32 | + constructor(props: IReactUploaderSkeletonProps) { |
| 33 | + super(props); |
| 34 | + this.fileInputRef = createRef<HTMLInputElement>(); |
| 35 | + } |
| 36 | + |
| 37 | + public onUploaderClick = () => { |
| 38 | + const { current } = this.fileInputRef; |
| 39 | + if (current) { |
| 40 | + current.click(); |
| 41 | + } |
| 42 | + }; |
| 43 | + |
| 44 | + public onFileChange = (event: React.ChangeEvent<HTMLInputElement>) => { |
| 45 | + const { files } = event.target; |
| 46 | + if (files) { |
| 47 | + for (const fileIndex in files) { |
| 48 | + const currentFile = files[fileIndex]; |
| 49 | + if (currentFile && /image\/.+/.test(currentFile.type)) { |
| 50 | + const fileReader = new FileReader(); |
| 51 | + fileReader.addEventListener("load", () => { |
| 52 | + this.setState(({ currentFiles }) => ({ |
| 53 | + currentFiles: [ |
| 54 | + ...currentFiles, |
| 55 | + { |
| 56 | + url: fileReader.result as string, |
| 57 | + id: currentFile.name, |
| 58 | + state: FileState.WAITING, |
| 59 | + fileData: currentFile |
| 60 | + } |
| 61 | + ] |
| 62 | + })); |
| 63 | + }); |
| 64 | + fileReader.readAsDataURL(files[fileIndex]); |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + }; |
| 69 | + |
10 | 70 | public render() { |
11 | | - return <div>{this.props.anyText}</div>; |
| 71 | + const { currentFiles } = this.state; |
| 72 | + return ( |
| 73 | + <div onClick={this.onUploaderClick} className="rus"> |
| 74 | + {this.props.anyText} |
| 75 | + |
| 76 | + {currentFiles.map(file => ( |
| 77 | + <div key={file.id}> |
| 78 | + {file.fileData && file.fileData.name} |
| 79 | + |
| 80 | + <img alt={file.fileData && file.fileData.name} src={file.url} /> |
| 81 | + </div> |
| 82 | + ))} |
| 83 | + |
| 84 | + <input |
| 85 | + className="rus_input" |
| 86 | + ref={this.fileInputRef} |
| 87 | + onChange={this.onFileChange} |
| 88 | + type="file" |
| 89 | + multiple={true} |
| 90 | + /> |
| 91 | + </div> |
| 92 | + ); |
12 | 93 | } |
13 | 94 | } |
14 | 95 |
|
|
0 commit comments