ant-design/components/popconfirm/index.tsx

121 lines
2.8 KiB
TypeScript
Raw Normal View History

2016-09-21 11:54:53 +08:00
import React from 'react';
2016-04-07 15:02:22 +08:00
import Tooltip from '../tooltip';
2016-11-30 16:43:35 +08:00
import { AbstractTooltipProps } from '../tooltip';
2015-11-20 11:05:34 +08:00
import Icon from '../icon';
2015-11-03 20:06:44 +08:00
import Button from '../button';
2015-11-24 11:21:37 +08:00
2016-11-30 16:43:35 +08:00
export interface PopconfirmProps extends AbstractTooltipProps {
title: React.ReactNode;
onConfirm?: () => void;
onCancel?: () => void;
okText?: React.ReactNode;
cancelText?: React.ReactNode;
}
2016-07-25 11:35:51 +08:00
export interface PopconfirmContext {
antLocale?: {
Popconfirm?: any,
};
}
export default class Popconfirm extends React.Component<PopconfirmProps, any> {
static defaultProps = {
prefixCls: 'ant-popover',
2016-04-07 15:02:22 +08:00
transitionName: 'zoom-big',
placement: 'top',
trigger: 'click',
2016-07-13 11:14:24 +08:00
};
static contextTypes = {
antLocale: React.PropTypes.object,
2016-07-13 11:14:24 +08:00
};
2016-07-25 11:39:54 +08:00
context: PopconfirmContext;
constructor(props: PopconfirmProps) {
super(props);
this.state = {
visible: props.visible,
2015-06-24 19:20:47 +08:00
};
}
2016-11-30 16:43:35 +08:00
componentWillReceiveProps(nextProps: PopconfirmProps) {
2015-12-04 14:58:19 +08:00
if ('visible' in nextProps) {
this.setState({ visible: nextProps.visible });
}
}
confirm = () => {
2015-12-04 14:58:19 +08:00
this.setVisible(false);
2016-10-24 12:04:26 +08:00
const onConfirm = this.props.onConfirm;
if (onConfirm) {
onConfirm.call(this);
}
}
cancel = () => {
2015-12-04 14:58:19 +08:00
this.setVisible(false);
2016-10-24 12:04:26 +08:00
const onCancel = this.props.onCancel;
if (onCancel) {
onCancel.call(this);
}
}
onVisibleChange = (visible) => {
2015-12-04 14:58:19 +08:00
this.setVisible(visible);
}
2015-12-04 14:58:19 +08:00
setVisible(visible) {
const props = this.props;
if (!('visible' in props)) {
2015-12-04 14:58:19 +08:00
this.setState({ visible });
}
2016-10-24 12:04:26 +08:00
const onVisibleChange = props.onVisibleChange;
2016-10-24 12:04:26 +08:00
if (onVisibleChange) {
onVisibleChange(visible);
}
}
2015-06-24 19:20:47 +08:00
render() {
const { props, context } = this;
2016-12-19 15:19:15 +08:00
const { prefixCls, title, placement, ...restProps } = props;
let { okText, cancelText } = props;
const popconfirmLocale = context.antLocale && context.antLocale.Popconfirm;
if (popconfirmLocale) {
okText = okText || popconfirmLocale.okText;
cancelText = cancelText || popconfirmLocale.cancelText;
2016-03-03 17:43:38 +08:00
}
const overlay = (
<div>
2016-03-17 15:10:49 +08:00
<div className={`${prefixCls}-inner-content`}>
2016-03-10 16:22:52 +08:00
<div className={`${prefixCls}-message`}>
2016-04-16 17:38:30 +08:00
<Icon type="exclamation-circle" />
2016-03-10 16:22:52 +08:00
<div className={`${prefixCls}-message-title`}>{title}</div>
</div>
<div className={`${prefixCls}-buttons`}>
2017-02-04 22:35:33 +08:00
<Button onClick={this.cancel} size="small">{cancelText || '取消'}</Button>
2016-03-03 21:00:38 +08:00
<Button onClick={this.confirm} type="primary" size="small">{okText || '确定'}</Button>
</div>
2015-06-24 19:20:47 +08:00
</div>
</div>
);
2015-06-24 19:20:47 +08:00
return (
<Tooltip
{...restProps}
prefixCls={prefixCls}
placement={placement}
onVisibleChange={this.onVisibleChange}
visible={this.state.visible}
overlay={overlay}
/>
2015-06-24 19:20:47 +08:00
);
}
}