ant-design/components/popconfirm/index.tsx

167 lines
4.5 KiB
TypeScript
Raw Normal View History

import * as React from 'react';
import ExclamationCircleFilled from '@ant-design/icons/ExclamationCircleFilled';
2018-12-07 20:02:01 +08:00
import Tooltip, { AbstractTooltipProps } from '../tooltip';
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/default';
import { ConfigConsumer, ConfigConsumerProps } from '../config-provider';
import { getRenderPropValue, RenderFunction } from '../_util/getRenderPropValue';
2015-11-24 11:21:37 +08:00
2016-11-30 16:43:35 +08:00
export interface PopconfirmProps extends AbstractTooltipProps {
title: React.ReactNode | RenderFunction;
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'],
use ant design icons 4.0 (#18217) * feat: use @ant-design/icons@4.0 * feat: use createFromIconfontCN to make site works * feat: update doc for Icon * feat: use icon in component Alert * feat: use icon in component Avatar * feat: use icon in component Breadcrumb * feat: use icon in component Button * feat: use icon in component Cascader * feat: use icon in component Collapse * feat: use icon in component Datepicker * feat: use icon in component Dropdown * feat: use icon in component Form * feat: use icon in component Input * feat: use icon in component InputNumber * feat: use icon in component Layout * feat: use icon in component Mention * feat: use icon in component Message * feat: use icon in component Modal * feat: use icon in component Notification * feat: use icon in component PageHeader * feat: use icon in component Pagination * feat: use icon in component Popconfirm * feat: use icon in component Progress * feat: use icon in component Rate * feat: use icon in component Result * feat: use icon in component Select * feat: use icon in component Step * feat: use icon in component Switch * feat: use icon in component Table * feat: use icon in component Tab * feat: use icon in component Tag * feat: handle rest component which using Icon * fix: remove unused vars * feat: use latest alpha ant design icons * fix: failed test in uploadlist.test.js * test: update snapshot for icons * doc: add Icon for site * doc: use @ant-design/icons in site * chore: use latest icons * fix: tslint issue * fix: test cases * fix: types for react * fix: lint rules for import orders * fix: use @ant-design/icons@4.0.0-alpha.5 to avoid insert css in server render * fix: eslint error in demo/**.md * inject antd icons * update snapshot * fix site * doc: update docs * fix: code snippets icon in site * feat: use latest @ant-design/icons * fix: icon props in message
2019-08-13 14:07:17 +08:00
icon: <ExclamationCircleFilled />,
disabled: false,
2016-07-13 11:14:24 +08:00
};
static getDerivedStateFromProps(nextProps: PopconfirmProps) {
if ('visible' in nextProps) {
return { visible: nextProps.visible };
2019-08-05 18:38:10 +08:00
}
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>) {
2019-08-05 18:38:10 +08:00
const { props } = this;
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 className={`${prefixCls}-inner-content`}>
<div className={`${prefixCls}-message`}>
{icon}
<div className={`${prefixCls}-message-title`}>{getRenderPropValue(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>
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>;
}
}
export default Popconfirm;