Popconfirm 气泡确认框

点击元素,弹出气泡式的确认框。

何时使用

目标元素的操作需要用户进一步的确认时,在目标元素附近弹出浮层提示,询问用户。

confirm 弹出的全屏居中模态对话框相比,交互形式更轻量。

代码演示

基本

最简单的用法。

Popconfirm气泡确认框 - 图1

  1. import React from 'react';
  2. import ReactDOM from 'react-dom';
  3. import { Popconfirm, message } from 'choerodon-ui';
  4. function confirm(e) {
  5. console.log(e);
  6. message.success('Click on Yes');
  7. }
  8. function cancel(e) {
  9. console.log(e);
  10. message.error('Click on No');
  11. }
  12. ReactDOM.render(
  13. <Popconfirm title="Are you sure delete this task?" onConfirm={confirm} onCancel={cancel} okText="Yes" cancelText="No">
  14. <a href="#">Delete</a>
  15. </Popconfirm>,
  16. document.getElementById('container'));

国际化

使用 okTextcancelText 自定义按钮文字。

Popconfirm气泡确认框 - 图2

  1. import React from 'react';
  2. import ReactDOM from 'react-dom';
  3. import { Popconfirm } from 'choerodon-ui';
  4. ReactDOM.render(
  5. <Popconfirm title="Are you sure?" okText="Yes" cancelText="No">
  6. <a href="#">Delete</a>
  7. </Popconfirm>,
  8. document.getElementById('container'));

位置

位置有十二个方向。如需箭头指向目标元素中心,可以设置 arrowPointAtCenter

Popconfirm气泡确认框 - 图3

  1. import React from 'react';
  2. import ReactDOM from 'react-dom';
  3. import { Popconfirm, message, Button } from 'choerodon-ui';
  4. const text = 'Are you sure delete this task?';
  5. function confirm() {
  6. message.info('Click on Yes.');
  7. }
  8. ReactDOM.render(
  9. <div className="demo">
  10. <div style={{ marginLeft: 70, whiteSpace: 'nowrap' }}>
  11. <Popconfirm placement="topLeft" title={text} onConfirm={confirm} okText="Yes" cancelText="No">
  12. <Button>TL</Button>
  13. </Popconfirm>
  14. <Popconfirm placement="top" title={text} onConfirm={confirm} okText="Yes" cancelText="No">
  15. <Button>Top</Button>
  16. </Popconfirm>
  17. <Popconfirm placement="topRight" title={text} onConfirm={confirm} okText="Yes" cancelText="No">
  18. <Button>TR</Button>
  19. </Popconfirm>
  20. </div>
  21. <div style={{ width: 70, float: 'left' }}>
  22. <Popconfirm placement="leftTop" title={text} onConfirm={confirm} okText="Yes" cancelText="No">
  23. <Button>LT</Button>
  24. </Popconfirm>
  25. <Popconfirm placement="left" title={text} onConfirm={confirm} okText="Yes" cancelText="No">
  26. <Button>Left</Button>
  27. </Popconfirm>
  28. <Popconfirm placement="leftBottom" title={text} onConfirm={confirm} okText="Yes" cancelText="No">
  29. <Button>LB</Button>
  30. </Popconfirm>
  31. </div>
  32. <div style={{ width: 70, marginLeft: 304 }}>
  33. <Popconfirm placement="rightTop" title={text} onConfirm={confirm} okText="Yes" cancelText="No">
  34. <Button>RT</Button>

条件触发

可以判断是否需要弹出。

Popconfirm气泡确认框 - 图4

  1. import React from 'react';
  2. import ReactDOM from 'react-dom';
  3. import { Popconfirm, Switch, message } from 'choerodon-ui';
  4. class App extends React.Component {
  5. state = {
  6. visible: false,
  7. condition: true, // Whether meet the condition, if not show popconfirm.
  8. }
  9. changeCondition = (value) => {
  10. this.setState({ condition: value });
  11. }
  12. confirm = () => {
  13. this.setState({ visible: false });
  14. message.success('Next step.');
  15. }
  16. cancel = () => {
  17. this.setState({ visible: false });
  18. message.error('Click on cancel.');
  19. }
  20. handleVisibleChange = (visible) => {
  21. if (!visible) {
  22. this.setState({ visible });
  23. return;
  24. }
  25. // Determining condition before show the popconfirm.

API

参数说明类型默认值
cancelText取消按钮文字string取消
okText确认按钮文字string确定
okType确认按钮类型stringprimary
title确认框的描述string|ReactNode
onCancel点击取消的回调function(e)
onConfirm点击确认的回调function(e)

更多属性请参考 Tooltip

注意

请确保 Popconfirm 的子元素能接受 onMouseEnteronMouseLeaveonFocusonClick 事件。