SlideBox.js 3.2 KB
/**
 * Created by nap on 2017/2/28.
 */

import React from 'react';
import l from './SlideBox.les';
import cx from 'classnames';
import {Spin} from 'antd';
import {connect} from 'react-redux';

class SlideBox extends React.Component{
   
    constructor(props){
        super(props);

        this.state = {
            show:false,
            remove:false
        };

        this.handleClose = this.handleClose.bind(this);
    }

    handleClose(){
        this.handleRemove();
        this.props.onClose && this.props.onClose();
    }

    handleRemove(){
        this.setState({show:false});
        setTimeout(()=>{
            this.setState({remove:true});
        },300);
    }

    handleShow(){
        this.setState({remove:false});
        setTimeout(()=>{
            this.setState({
                show:true
            });
        },10);
    }

    componentWillReceiveProps(props){
        if(props.show){
            this.handleShow();
        }else{
            this.handleRemove();
        }
    }

    componentDidMount(){
        const {show} = this.props;

        if(show){
            this.setState({
                remove:!show
            });
            this.handleShow();
        }else{
            this.setState({
                remove:!show
            });
            this.handleRemove();
        }
    }

    
    render(){

        const {show,remove} = this.state;
        const {loading = false,status = 0} = this.props;
        if (remove){
            return null;
        }

        return (
            <div className={cx(l.slideBox,{[l.show]:show})}>
                <div className={cx(l.slide)} style={{width:this.props.width || 684}}>
                    <div className={cx(l.title)}>
                        <div className={cx(l.btn_close)} onClick={this.handleClose}>
                            <span className="upvi-icon">&#xe623;</span>
                        </div>
                        {this.props.title || null}
                    </div>
                    <div className={cx(l.context)}>
                        {
                            loading ? (
                                status == 'loading' ?
                                    <div className={cx(l.loading)}>
                                        <div className={cx(l.icon)}>
                                            <Spin size="large"/>
                                        </div>
                                    </div>
                                    : status == 'error' ? (
                                            <div className={cx(l.loading)}>
                                                <div className={cx(l.icon)}>
                                                    <p>加载失败</p>
                                                </div>
                                            </div>
                                        ) : null
                            ) : (this.props.children || null)
                        }
                    </div>
                </div>
            </div>
        );
    }
}



export default connect(state=>{
    const {environment:{showSlideLoading,slideLoadingStatus}} = state;
    return {loading:showSlideLoading,status:slideLoadingStatus};
})(SlideBox);