import React from 'react'
import { Modal } from 'antd'


/**
 * @class DeleteBox
 * @extends {React.Component}
 *
 */
class DeleteBox extends React.Component {

    componentDidMount() {
        window.addEventListener('keydown', this.keydown);
    }

    componentWillUnmount() {
        window.removeEventListener('keydown', this.keydown);
    }

    keydown = (e) => {
        if(e.keyCode === 13) { // 按下回车
            this.onOk();
        }
    }

    onOk = () => {
        const { okHandler, hideBox } = this.props;
        typeof okHandler === 'function' && okHandler();
        typeof hideBox === 'function' && hideBox();
    }

    render() {
        const { visibleBox, hideBox, title, text } = this.props
        return (
            <Modal
                title={title || '删除确认'}
                visible={visibleBox}
                onCancel={hideBox}
                onOk={this.onOk}
                destroyOnClose={true}
            >
                <span>{text || '确定要删除该对象吗？'}</span>
            </Modal>
        )
    }
}

export default DeleteBox