FileField.js
4.8 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import React,{PropTypes} from 'react';
import ReactDOM from 'react-dom';
import cx from 'classnames';
import s from '../FormRender.scss';
import UploadWrap from '../UploadWrap';
import Uploader from '../Uploader';
import UploaderResult from '../UploaderResult';
import Attachment from '../Attachment';
class FileField extends React.Component {
constructor(props) {
super(props);
this.uploadResult=this.uploadResult.bind(this);
this.changeFiles=this.changeFiles.bind(this);
this.fileChange=this.fileChange.bind(this);
this.fileUploading=this.fileUploading.bind(this);
this.fileCallBack=this.fileCallBack.bind(this);
this.cleanFile=this.cleanFile.bind(this);
this.fileUpload=this.fileUpload.bind(this);
this.state={
file:null,
files:[],
uploaderResult:{}
}
}
static propTypes = {
formField:PropTypes.object,
field:PropTypes.object,
validateState:PropTypes.string,
fieldConfig:PropTypes.object,
inlineFlag:PropTypes.bool
}
static defaultProps={
inlineFlag:false,
validateState:''
}
cleanFile(e){
const {file_upload_wrap}=this.refs;
const {field}=this.props
file_upload_wrap.removeAllFile();
this.setState({
file:null,
error:''
})
field.onChange('');
e.stopPropagation();
}
componentWillReceiveProps(nextProps){
}
fileUpload(){
const {file_upload_wrap}=this.refs;
file_upload_wrap.upload();
this.setState({
fileSubmit:true,
error:''
})
}
fileChange(file){
this.fileUpload();
this.setState({
file:file,
error:''
});
}
fileUploading(file){
this.setState({
file:file
})
}
fileCallBack(info){
const {field}=this.props,self=this;
const {file}=this.state;
if(info){
const filePath=info.path.substring(0,info.path.lastIndexOf("/")+1)
const temp=[{
id:file.id,
file_id:info.uuid,
name:file.name,
type:file.type,
size:file.size,
bucket:info.bucket,
path:filePath,
requestId:'',
status:file.status+""
}]
field.onChange(JSON.stringify(temp));
}
}
changeFiles(files){
const {field}=this.props;
const {uploaderResult}=this.state;
this.setState({
files:files
})
if(files&&files.length<1){
field.onChange('');
}
if(uploaderResult&&uploaderResult.queued==0&&files.length>0){
field.onChange(JSON.stringify(files));
}
}
uploadResult(result){
this.setState({
uploaderResult:result
})
}
renderExisted(){
const {files}=this.state,self=this;
return(
<div>
{files.map((file,i)=>{
return (<Attachment readOnly={false} key={i} index={i} attachment={file} onDelete={this.onDelete}></Attachment>)
})}
</div>
)
}
render(){
const {formField,validateState,inlineFlag,field,type,fieldConfig}=this.props,self=this;
const {uploaderResult,file}=this.state;
const options=JSON.parse(formField.options);
return(
<div className={cx(s.field_wrap,'form-group',{
'has-success':validateState=='success',
'has-warning':validateState=='warning',
'has-error':validateState=='error',
'row':inlineFlag
})}>
<label className={cx('control-label',{
'col-sm-3':inlineFlag
})}>{formField.display_name}</label>
<div className={cx(s.input_wrap,{
'col-sm-9':inlineFlag
})} >
<UploadWrap type={fieldConfig.type||'form'} style={{height:'60px'}} ref='file_upload_wrap' onChange={this.fileChange} backResult={this.fileUploading} endCallBack={this.fileCallBack}>
<input className={cx(s.file_form_control,'form-control')} type="text" placeholder='请选择文件' readOnly={true} value={file&&file.name}/>
<span className={cx(s.input_help,s.upload_field_help,'help-block')}><small>{field.touched?field.error:''}</small></span>
</UploadWrap>
<a onClick={this.cleanFile} className={cx('kr_icon')} href="javascript:;" style={{position:'absolute',top:'10px',right:'10px',zIndex:10}}></a>
</div>
</div>
)
}
}
export default FileField;