ant-design/components/popconfirm/index.tsx

170 lines
4.5 KiB
TypeScript
Raw Normal View History

import * as React from 'react';
import { polyfill } from 'react-lifecycles-compat';
2018-12-07 20:02:01 +08:00
import Tooltip, { AbstractTooltipProps } from '../tooltip';
2015-11-20 11:05:34 +08:00
import Icon from '../icon';
2017-07-17 09:40:55 +08:00
import Button from '../button';
import { ButtonType, NativeButtonProps } from '../button/button';
import LocaleReceiver from '../locale-provider/LocaleReceiver';
import defaultLocale from '../locale-provider/default';
import { ConfigConsumer, ConfigConsumerProps } from '../config-provider';
2015-11-24 11:21:37 +08:00
2016-11-30 16:43:35 +08:00
export interface PopconfirmProps extends AbstractTooltipProps {
title: React.ReactNode;
disabled?: boolean;
onConfirm?: (e?: React.MouseEvent<HTMLElement>) => void;
onCancel?: (e?: React.MouseEvent<HTMLElement>) => void;
okText?: React.ReactNode;
okType?: ButtonType;
cancelText?: React.ReactNode;
okButtonProps?: NativeButtonProps;
cancelButtonProps?: NativeButtonProps;
2018-07-06 23:48:37 +08:00
icon?: React.ReactNode;
onVisibleChange?: (visible: boolean, e?: React.MouseEvent<HTMLElement>) => void;
}
export interface PopconfirmState {
visible?: boolean;
}
export interface PopconfirmLocale {
okText: string;
cancelText: string;
}
class Popconfirm extends React.Component<PopconfirmProps, PopconfirmState> {
static defaultProps = {
2016-04-07 15:02:22 +08:00
transitionName: 'zoom-big',
placement: 'top' as PopconfirmProps['placement'],
trigger: 'click' as PopconfirmProps['trigger'],
okType: 'primary' as PopconfirmProps['okType'],
2018-09-08 15:46:26 +08:00
icon: <Icon type="exclamation-circle" theme="filled" />,
disabled: false,
2016-07-13 11:14:24 +08:00
};
static getDerivedStateFromProps(nextProps: PopconfirmProps) {
if ('visible' in nextProps) {
return { visible: nextProps.visible };
} else if ('defaultVisible' in nextProps) {
return { visible: nextProps.defaultVisible };
}
return null;
}
private tooltip: any;
2017-07-07 20:26:08 +08:00
constructor(props: PopconfirmProps) {
super(props);
this.state = {
visible: props.visible,
2015-06-24 19:20:47 +08:00
};
}
2017-07-07 20:26:08 +08:00
getPopupDomNode() {
return this.tooltip.getPopupDomNode();
2017-07-07 20:26:08 +08:00
}
onConfirm = (e: React.MouseEvent<HTMLButtonElement>) => {
this.setVisible(false, e);
2016-10-24 12:04:26 +08:00
const { onConfirm } = this.props;
2016-10-24 12:04:26 +08:00
if (onConfirm) {
onConfirm.call(this, e);
2016-10-24 12:04:26 +08:00
}
2018-12-07 20:02:01 +08:00
};
onCancel = (e: React.MouseEvent<HTMLButtonElement>) => {
this.setVisible(false, e);
2016-10-24 12:04:26 +08:00
const { onCancel } = this.props;
2016-10-24 12:04:26 +08:00
if (onCancel) {
onCancel.call(this, e);
2016-10-24 12:04:26 +08:00
}
2018-12-07 20:02:01 +08:00
};
onVisibleChange = (visible: boolean) => {
const { disabled } = this.props;
if (disabled) {
return;
}
2015-12-04 14:58:19 +08:00
this.setVisible(visible);
2018-12-07 20:02:01 +08:00
};
setVisible(visible: boolean, e?: React.MouseEvent<HTMLButtonElement>) {
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;
2016-10-24 12:04:26 +08:00
if (onVisibleChange) {
onVisibleChange(visible, e);
2016-10-24 12:04:26 +08:00
}
}
saveTooltip = (node: any) => {
this.tooltip = node;
2018-12-07 20:02:01 +08:00
};
renderOverlay = (prefixCls: string, popconfirmLocale: PopconfirmLocale) => {
2018-12-07 20:02:01 +08:00
const {
okButtonProps,
cancelButtonProps,
title,
cancelText,
okText,
okType,
icon,
} = this.props;
return (
<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`}>
2018-07-06 23:48:37 +08:00
{icon}
2016-03-10 16:22:52 +08:00
<div className={`${prefixCls}-message-title`}>{title}</div>
</div>
<div className={`${prefixCls}-buttons`}>
<Button onClick={this.onCancel} size="small" {...cancelButtonProps}>
{cancelText || popconfirmLocale.cancelText}
</Button>
<Button onClick={this.onConfirm} type={okType} size="small" {...okButtonProps}>
{okText || popconfirmLocale.okText}
</Button>
</div>
2015-06-24 19:20:47 +08:00
</div>
</div>
);
2018-12-07 20:02:01 +08:00
};
renderConfirm = ({ getPrefixCls }: ConfigConsumerProps) => {
const { prefixCls: customizePrefixCls, placement, ...restProps } = this.props;
const prefixCls = getPrefixCls('popover', customizePrefixCls);
const overlay = (
2018-12-07 20:02:01 +08:00
<LocaleReceiver componentName="Popconfirm" defaultLocale={defaultLocale.Popconfirm}>
{(popconfirmLocale: PopconfirmLocale) => this.renderOverlay(prefixCls, popconfirmLocale)}
</LocaleReceiver>
);
2015-06-24 19:20:47 +08:00
return (
<Tooltip
{...restProps}
prefixCls={prefixCls}
placement={placement}
onVisibleChange={this.onVisibleChange}
visible={this.state.visible}
overlay={overlay}
ref={this.saveTooltip}
/>
2015-06-24 19:20:47 +08:00
);
2018-12-07 20:02:01 +08:00
};
render() {
2018-12-07 20:02:01 +08:00
return <ConfigConsumer>{this.renderConfirm}</ConfigConsumer>;
}
}
polyfill(Popconfirm);
export default Popconfirm;