ant-design/components/modal/Modal.tsx

103 lines
2.5 KiB
TypeScript
Raw Normal View History

2016-07-07 20:25:03 +08:00
import { PropTypes } from 'react';
import * as React from 'react';
2016-01-12 17:07:24 +08:00
import Dialog from 'rc-dialog';
2016-06-16 22:34:54 +08:00
import addEventListener from 'rc-util/lib/Dom/addEventListener';
2016-01-12 17:07:24 +08:00
import Button from '../button';
function noop() {}
let mousePosition;
let mousePositionEventBinded;
export default class Modal extends React.Component {
static defaultProps = {
prefixCls: 'ant-modal',
onOk: noop,
onCancel: noop,
width: 520,
transitionName: 'zoom',
2016-04-04 17:38:39 +08:00
maskTransitionName: 'fade',
confirmLoading: false,
visible: false,
}
2016-03-03 17:43:38 +08:00
static propTypes = {
prefixCls: PropTypes.string,
onOk: PropTypes.func,
onCancel: PropTypes.func,
okText: PropTypes.node,
cancelText: PropTypes.node,
width: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
confirmLoading: PropTypes.bool,
visible: PropTypes.bool,
align: PropTypes.object,
footer: PropTypes.node,
title: PropTypes.node,
closable: PropTypes.bool,
}
static contextTypes = {
antLocale: React.PropTypes.object,
}
handleCancel = (e) => {
2016-02-02 14:54:34 +08:00
this.props.onCancel(e);
}
2016-01-12 17:07:24 +08:00
handleOk = () => {
2016-01-12 17:07:24 +08:00
this.props.onOk();
}
2016-01-12 17:07:24 +08:00
componentDidMount() {
if (mousePositionEventBinded) {
return;
}
// 只有点击事件支持从鼠标位置动画展开
2016-06-16 22:34:54 +08:00
addEventListener(document.documentElement, 'click', (e) => {
2016-01-12 17:07:24 +08:00
mousePosition = {
x: e.pageX,
2016-05-11 09:32:33 +08:00
y: e.pageY,
2016-01-12 17:07:24 +08:00
};
// 20ms 内发生过点击事件,则从点击位置动画展示
// 否则直接 zoom 展示
// 这样可以兼容非点击方式展开
setTimeout(() => mousePosition = null, 20);
});
mousePositionEventBinded = true;
}
2016-01-12 17:07:24 +08:00
render() {
let props = this.props;
2016-03-03 17:43:38 +08:00
let { okText, cancelText } = props;
2016-03-05 16:39:27 +08:00
if (this.context.antLocale && this.context.antLocale.Modal) {
okText = okText || this.context.antLocale.Modal.okText;
cancelText = cancelText || this.context.antLocale.Modal.cancelText;
2016-03-03 17:43:38 +08:00
}
2016-01-12 17:07:24 +08:00
let defaultFooter = [
<Button key="cancel"
type="ghost"
size="large"
onClick={this.handleCancel}
>
2016-03-07 12:08:52 +08:00
{cancelText || '取消'}
2016-01-12 17:07:24 +08:00
</Button>,
<Button key="confirm"
type="primary"
size="large"
loading={props.confirmLoading}
onClick={this.handleOk}
>
2016-03-07 12:08:52 +08:00
{okText || '确定'}
2016-05-11 09:32:33 +08:00
</Button>,
2016-01-12 17:07:24 +08:00
];
let footer = props.footer || defaultFooter;
2016-01-13 22:22:33 +08:00
return (
<Dialog onClose={this.handleCancel} footer={footer} {...props}
visible={props.visible} mousePosition={mousePosition}
/>
2016-01-13 22:22:33 +08:00
);
2016-01-12 17:07:24 +08:00
}
}