/**
 * @file DownloadField component
 * @author liangxiaojun(liangxiaojun@derbysoft.com)
 */

import React, {Component} from 'react';
import PropTypes from 'prop-types';

class DownloadField extends Component {

    constructor(props, ...restArgs) {

        super(props, ...restArgs);

        this.state = {
            key: 0
        };

    }

    download = () => {
        this.setState({
            key: this.state.key + 1
        });
    };

    loadedHandler = e => {

        const {onLoad} = this.props,
            iframeEl = this.refs.iframe;

        iframeEl && onLoad && onLoad(
            e, iframeEl.contentDocument ?
                iframeEl.contentDocument.body.innerText
                :
                undefined
        );

    };

    shouldComponentUpdate(nextProps, nextState) {
        if (nextState.key !== this.state.key) {
            return true;
        }
        return false;
    }

    render() {

        const {url} = this.props,
            {key} = this.state;

        return key > 0 ?
            <iframe key={key}
                    ref="iframe"
                    className="download-field"
                    onLoad={this.loadedHandler}
                    onError={this.loadedHandler}
                    src={key > 0 ? url : null}></iframe>
            :
            null;

    }
}

DownloadField.propTypes = {

    /**
     * Download url.
     */
    url: PropTypes.string,

    /**
     * Loaded callback.
     */
    onLoad: PropTypes.func

};

DownloadField.defaultProps = {
    url: null
};

export default DownloadField;
@@ -319,14 +319,26 @@
     
-key: 0
+downloading: false
 %0A   
@@ -416,31 +416,25 @@
     
-key: this.state.key + 1
+downloading: true
 %0A   
@@ -474,24 +474,103 @@
 r = e =%3E %7B%0A%0A
+        this.setState(%7B%0A            downloading: false%0A        %7D, () =%3E %7B%0A%0A    
         cons
@@ -598,32 +598,36 @@
 ps,%0A            
+    
 iframeEl = this.
@@ -640,32 +640,36 @@
 frame;%0A%0A        
+   
+ 
 iframeEl && onLo
@@ -682,16 +682,20 @@
 onLoad(%0A
+    
         
@@ -736,32 +736,36 @@
                 
+ 
+   
 iframeEl.content
@@ -796,34 +796,42 @@
                 
-:%0A
+    :%0A    
                 
@@ -848,16 +848,33 @@
         
+  
+  );%0A%0A        %7D
 );%0A%0A    
@@ -949,19 +949,27 @@
 xtState.
-key
+downloading
  !== thi
@@ -976,19 +976,27 @@
 s.state.
-key
+downloading
 ) %7B%0A    
@@ -1118,19 +1118,27 @@
        %7B
-key
+downloading
 %7D = this
@@ -1161,23 +1161,27 @@
  return 
-key %3E 0
+downloading
  ?%0A     
@@ -1199,38 +1199,8 @@
 ame 
-key=%7Bkey%7D%0A                    
 ref=
@@ -1381,15 +1381,19 @@
 rc=%7B
-key %3E 0
+downloading
  ? u

update DownloadField