FileField.js 4.8 KB
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}}>&#xe612;</a> 
                </div>
            </div> 
        )
    }
}



export default FileField;