|
|
@ -5,7 +5,7 @@ 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 { ConfigContext } from '../config-provider'; |
|
|
|
import { getRenderPropValue, RenderFunction } from '../_util/getRenderPropValue'; |
|
|
|
|
|
|
|
export interface PopconfirmProps extends AbstractTooltipProps { |
|
|
@ -31,92 +31,57 @@ export interface PopconfirmLocale { |
|
|
|
cancelText: string; |
|
|
|
} |
|
|
|
|
|
|
|
class Popconfirm extends React.Component<PopconfirmProps, PopconfirmState> { |
|
|
|
static defaultProps = { |
|
|
|
transitionName: 'zoom-big', |
|
|
|
placement: 'top' as PopconfirmProps['placement'], |
|
|
|
trigger: 'click' as PopconfirmProps['trigger'], |
|
|
|
okType: 'primary' as PopconfirmProps['okType'], |
|
|
|
icon: <ExclamationCircleFilled />, |
|
|
|
disabled: false, |
|
|
|
}; |
|
|
|
const Popconfirm = React.forwardRef<unknown, PopconfirmProps>((props, ref) => { |
|
|
|
const [visible, setVisible] = React.useState(props.visible); |
|
|
|
|
|
|
|
static getDerivedStateFromProps(nextProps: PopconfirmProps) { |
|
|
|
if ('visible' in nextProps) { |
|
|
|
return { visible: nextProps.visible }; |
|
|
|
} |
|
|
|
if ('defaultVisible' in nextProps) { |
|
|
|
return { visible: nextProps.defaultVisible }; |
|
|
|
React.useEffect(() => { |
|
|
|
if ('visible' in props) { |
|
|
|
setVisible(props.visible); |
|
|
|
} |
|
|
|
return null; |
|
|
|
} |
|
|
|
|
|
|
|
private tooltip: any; |
|
|
|
}, [props.visible]); |
|
|
|
|
|
|
|
constructor(props: PopconfirmProps) { |
|
|
|
super(props); |
|
|
|
React.useEffect(() => { |
|
|
|
if ('defaultVisible' in props) { |
|
|
|
setVisible(props.defaultVisible); |
|
|
|
} |
|
|
|
}, [props.defaultVisible]); |
|
|
|
|
|
|
|
this.state = { |
|
|
|
visible: props.visible, |
|
|
|
}; |
|
|
|
} |
|
|
|
const settingVisible = (value: boolean, e?: React.MouseEvent<HTMLButtonElement>) => { |
|
|
|
if (!('visible' in props)) { |
|
|
|
setVisible(value); |
|
|
|
} |
|
|
|
|
|
|
|
getPopupDomNode() { |
|
|
|
return this.tooltip.getPopupDomNode(); |
|
|
|
} |
|
|
|
if (props.onVisibleChange) { |
|
|
|
props.onVisibleChange(value, e); |
|
|
|
} |
|
|
|
}; |
|
|
|
|
|
|
|
onConfirm = (e: React.MouseEvent<HTMLButtonElement>) => { |
|
|
|
this.setVisible(false, e); |
|
|
|
const onConfirm = (e: React.MouseEvent<HTMLButtonElement>) => { |
|
|
|
settingVisible(false, e); |
|
|
|
|
|
|
|
const { onConfirm } = this.props; |
|
|
|
if (onConfirm) { |
|
|
|
onConfirm.call(this, e); |
|
|
|
if (props.onConfirm) { |
|
|
|
props.onConfirm.call(this, e); |
|
|
|
} |
|
|
|
}; |
|
|
|
|
|
|
|
onCancel = (e: React.MouseEvent<HTMLButtonElement>) => { |
|
|
|
this.setVisible(false, e); |
|
|
|
const onCancel = (e: React.MouseEvent<HTMLButtonElement>) => { |
|
|
|
settingVisible(false, e); |
|
|
|
|
|
|
|
const { onCancel } = this.props; |
|
|
|
if (onCancel) { |
|
|
|
onCancel.call(this, e); |
|
|
|
if (props.onCancel) { |
|
|
|
props.onCancel.call(this, e); |
|
|
|
} |
|
|
|
}; |
|
|
|
|
|
|
|
onVisibleChange = (visible: boolean) => { |
|
|
|
const { disabled } = this.props; |
|
|
|
const onVisibleChange = (value: boolean) => { |
|
|
|
const { disabled } = props; |
|
|
|
if (disabled) { |
|
|
|
return; |
|
|
|
} |
|
|
|
this.setVisible(visible); |
|
|
|
settingVisible(value); |
|
|
|
}; |
|
|
|
|
|
|
|
setVisible(visible: boolean, e?: React.MouseEvent<HTMLButtonElement>) { |
|
|
|
const { props } = this; |
|
|
|
if (!('visible' in props)) { |
|
|
|
this.setState({ visible }); |
|
|
|
} |
|
|
|
|
|
|
|
const { onVisibleChange } = props; |
|
|
|
if (onVisibleChange) { |
|
|
|
onVisibleChange(visible, e); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
saveTooltip = (node: any) => { |
|
|
|
this.tooltip = node; |
|
|
|
}; |
|
|
|
|
|
|
|
renderOverlay = (prefixCls: string, popconfirmLocale: PopconfirmLocale) => { |
|
|
|
const { |
|
|
|
okButtonProps, |
|
|
|
cancelButtonProps, |
|
|
|
title, |
|
|
|
cancelText, |
|
|
|
okText, |
|
|
|
okType, |
|
|
|
icon, |
|
|
|
} = this.props; |
|
|
|
const renderOverlay = (prefixCls: string, popconfirmLocale: PopconfirmLocale) => { |
|
|
|
const { okButtonProps, cancelButtonProps, title, cancelText, okText, okType, icon } = props; |
|
|
|
return ( |
|
|
|
<div className={`${prefixCls}-inner-content`}> |
|
|
|
<div className={`${prefixCls}-message`}> |
|
|
@ -124,10 +89,10 @@ class Popconfirm extends React.Component<PopconfirmProps, PopconfirmState> { |
|
|
|
<div className={`${prefixCls}-message-title`}>{getRenderPropValue(title)}</div> |
|
|
|
</div> |
|
|
|
<div className={`${prefixCls}-buttons`}> |
|
|
|
<Button onClick={this.onCancel} size="small" {...cancelButtonProps}> |
|
|
|
<Button onClick={onCancel} size="small" {...cancelButtonProps}> |
|
|
|
{cancelText || popconfirmLocale.cancelText} |
|
|
|
</Button> |
|
|
|
<Button onClick={this.onConfirm} type={okType} size="small" {...okButtonProps}> |
|
|
|
<Button onClick={onConfirm} type={okType} size="small" {...okButtonProps}> |
|
|
|
{okText || popconfirmLocale.okText} |
|
|
|
</Button> |
|
|
|
</div> |
|
|
@ -135,32 +100,37 @@ class Popconfirm extends React.Component<PopconfirmProps, PopconfirmState> { |
|
|
|
); |
|
|
|
}; |
|
|
|
|
|
|
|
renderConfirm = ({ getPrefixCls }: ConfigConsumerProps) => { |
|
|
|
const { prefixCls: customizePrefixCls, placement, ...restProps } = this.props; |
|
|
|
const prefixCls = getPrefixCls('popover', customizePrefixCls); |
|
|
|
|
|
|
|
const overlay = ( |
|
|
|
<LocaleReceiver componentName="Popconfirm" defaultLocale={defaultLocale.Popconfirm}> |
|
|
|
{(popconfirmLocale: PopconfirmLocale) => this.renderOverlay(prefixCls, popconfirmLocale)} |
|
|
|
</LocaleReceiver> |
|
|
|
); |
|
|
|
|
|
|
|
return ( |
|
|
|
<Tooltip |
|
|
|
{...restProps} |
|
|
|
prefixCls={prefixCls} |
|
|
|
placement={placement} |
|
|
|
onVisibleChange={this.onVisibleChange} |
|
|
|
visible={this.state.visible} |
|
|
|
overlay={overlay} |
|
|
|
ref={this.saveTooltip} |
|
|
|
/> |
|
|
|
); |
|
|
|
}; |
|
|
|
|
|
|
|
render() { |
|
|
|
return <ConfigConsumer>{this.renderConfirm}</ConfigConsumer>; |
|
|
|
} |
|
|
|
} |
|
|
|
const { getPrefixCls } = React.useContext(ConfigContext); |
|
|
|
|
|
|
|
const { prefixCls: customizePrefixCls, placement, ...restProps } = props; |
|
|
|
const prefixCls = getPrefixCls('popover', customizePrefixCls); |
|
|
|
|
|
|
|
const overlay = ( |
|
|
|
<LocaleReceiver componentName="Popconfirm" defaultLocale={defaultLocale.Popconfirm}> |
|
|
|
{(popconfirmLocale: PopconfirmLocale) => renderOverlay(prefixCls, popconfirmLocale)} |
|
|
|
</LocaleReceiver> |
|
|
|
); |
|
|
|
|
|
|
|
return ( |
|
|
|
<Tooltip |
|
|
|
{...restProps} |
|
|
|
prefixCls={prefixCls} |
|
|
|
placement={placement} |
|
|
|
onVisibleChange={onVisibleChange} |
|
|
|
visible={visible} |
|
|
|
overlay={overlay} |
|
|
|
ref={ref as any} |
|
|
|
/> |
|
|
|
); |
|
|
|
}); |
|
|
|
|
|
|
|
Popconfirm.defaultProps = { |
|
|
|
transitionName: 'zoom-big', |
|
|
|
placement: 'top' as PopconfirmProps['placement'], |
|
|
|
trigger: 'click' as PopconfirmProps['trigger'], |
|
|
|
okType: 'primary' as PopconfirmProps['okType'], |
|
|
|
icon: <ExclamationCircleFilled />, |
|
|
|
disabled: false, |
|
|
|
}; |
|
|
|
|
|
|
|
export default Popconfirm; |
|
|
|