Attachments.js
2.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import React,{PropTypes} from 'react';
import {Button,Row,Col,ProgressBar} from 'react-bootstrap';
import cx from 'classnames';
import s from './Uploader.scss';
import Uploader from '../../components/widget/Uploader';
import UploaderResult from '../widget/UploaderResult';
import Attachment from '../../components/widget/Attachment';
export class Attachments extends React.Component {
constructor(props) {
super(props);
this.uploadResult = this.uploadResult.bind(this);
this.onUpload = this.onUpload.bind(this);
this.onDelete = this.onDelete.bind(this);
this.state={uploadResult:{}};
}
static propTypes = {
attachments: PropTypes.array,
type: PropTypes.string,
readOnly: PropTypes.bool,
onChange: PropTypes.func
}
static defaultProps(){
readOnly:false
}
uploadResult(result){
console.log(result);
this.setState({
uploaderResult:result
})
}
onDelete(index){
const {onChange, attachments} = this.props;
const {uploader_file}=this.refs;
const file=attachments[index];
if(file)
uploader_file.removeFile(file);
// console.log(index);
let updatedOnes = attachments.concat();
updatedOnes.splice(index,1);
console.log(updatedOnes);
onChange(updatedOnes);
}
onUpload(newAttachments){
let uploadOnes = [];
let serverFiles=[];
const {onChange, attachments} = this.props;
console.log(newAttachments);
console.log(attachments);
newAttachments.map((attachment)=>{
if(attachment.status==5){
attachment.status='5';
uploadOnes.push(attachment);
}
});
if(attachments)
attachments.map((att,i)=>{
if(att.status=='active'){
serverFiles.push(att);
}
});
if(uploadOnes.length>0){
uploadOnes = uploadOnes.concat(serverFiles);
onChange(uploadOnes);
}
}
renderExisted(){
const {attachments, readOnly} = this.props;
console.log(attachments);
if(attachments&&attachments.length>0){
return(
<div>
{attachments.map((attachment,index)=>{
return (
<Attachment readOnly={readOnly} key={index} index={index} attachment={attachment} onDelete={this.onDelete}></Attachment>)
})
}
</div>
);
}
else
return null;
}
render(){
const {readOnly, type, attachments}=this.props,self=this;
const {uploaderResult} = this.state;
return (
<div>
{this.renderExisted()}
{!readOnly&&
<div>
<Uploader ref='uploader_file' type={type} onChange={this.onUpload} backResult={this.uploadResult}/>
<UploaderResult result={uploaderResult}/>
</div>
}
</div>
)
}
}
export default Attachments;