/**
 * @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 = {
            downloading: false
        };

    }

    download = () => {
        this.setState({
            downloading: true
        });
    };

    loadedHandler = e => {

        this.setState({
            downloading: false
        }, () => {

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

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

        });

    };

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

    render() {

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

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

    }
}

DownloadField.propTypes = {

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

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

};

DownloadField.defaultProps = {
    url: null
};

export default DownloadField;
@@ -111,16 +111,27 @@
 omponent
+, createRef
 %7D from '
@@ -287,24 +287,60 @@
 restArgs);%0A%0A
+        this.iframe = createRef();%0A%0A
         this
@@ -501,29 +501,28 @@
 %7D;%0A%0A    
-loadedHandler
+handleLoaded
  = e =%3E 
@@ -676,19 +676,37 @@
 his.
-refs.
 iframe
+ && this.iframe.current
 ;%0A%0A 
@@ -745,33 +745,16 @@
  onLoad(
-%0A                
 e, ifram
@@ -775,20 +775,16 @@
 ument ?%0A
-    
         
@@ -843,32 +843,26 @@
             
-    :%0A  
+:%0A
             
@@ -865,18 +865,16 @@
         
-  
 undefine
@@ -874,29 +874,16 @@
 ndefined
-%0A            
 );%0A%0A    
@@ -1225,16 +1225,21 @@
 ref=
-%22
+%7Bthis.
 iframe
-%22
+%7D
 %0A   
@@ -1311,37 +1311,36 @@
 nLoad=%7Bthis.
-loadedHandler
+handleLoaded
 %7D%0A          
@@ -1367,21 +1367,20 @@
 his.
-loadedHandler
+handleLoaded
 %7D%0A  
@@ -1724,8 +1724,9 @@
 adField;
+%0A

update DownloadField