Popconfirm气泡确认框

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

何时使用

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

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

代码演示

Popconfirm 气泡确认框 - 图1

基本

最简单的用法。

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

Popconfirm 气泡确认框 - 图2

位置

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

  1. import { Popconfirm, message, Button } from 'choerodon-ui';
  2. const text = 'Are you sure delete this task?';
  3. function confirm() {
  4. message.info('Click on Yes.');
  5. }
  6. ReactDOM.render(
  7. <div className="demo">
  8. <div style={{ marginLeft: 70, whiteSpace: 'nowrap' }}>
  9. <Popconfirm placement="topLeft" title={text} onConfirm={confirm} okText="Yes" cancelText="No">
  10. <Button>TL</Button>
  11. </Popconfirm>
  12. <Popconfirm placement="top" title={text} onConfirm={confirm} okText="Yes" cancelText="No">
  13. <Button>Top</Button>
  14. </Popconfirm>
  15. <Popconfirm placement="topRight" title={text} onConfirm={confirm} okText="Yes" cancelText="No">
  16. <Button>TR</Button>
  17. </Popconfirm>
  18. </div>
  19. <div style={{ width: 70, float: 'left' }}>
  20. <Popconfirm placement="leftTop" title={text} onConfirm={confirm} okText="Yes" cancelText="No">
  21. <Button>LT</Button>
  22. </Popconfirm>
  23. <Popconfirm placement="left" title={text} onConfirm={confirm} okText="Yes" cancelText="No">
  24. <Button>Left</Button>
  25. </Popconfirm>
  26. <Popconfirm placement="leftBottom" title={text} onConfirm={confirm} okText="Yes" cancelText="No">
  27. <Button>LB</Button>
  28. </Popconfirm>
  29. </div>
  30. <div style={{ width: 70, marginLeft: 304 }}>
  31. <Popconfirm placement="rightTop" title={text} onConfirm={confirm} okText="Yes" cancelText="No">
  32. <Button>RT</Button>
  33. </Popconfirm>
  34. <Popconfirm placement="right" title={text} onConfirm={confirm} okText="Yes" cancelText="No">
  35. <Button>Right</Button>
  36. </Popconfirm>
  37. <Popconfirm placement="rightBottom" title={text} onConfirm={confirm} okText="Yes" cancelText="No">
  38. <Button>RB</Button>
  39. </Popconfirm>
  40. </div>
  41. <div style={{ marginLeft: 70, clear: 'both', whiteSpace: 'nowrap' }}>
  42. <Popconfirm placement="bottomLeft" title={text} onConfirm={confirm} okText="Yes" cancelText="No">
  43. <Button>BL</Button>
  44. </Popconfirm>
  45. <Popconfirm placement="bottom" title={text} onConfirm={confirm} okText="Yes" cancelText="No">
  46. <Button>Bottom</Button>
  47. </Popconfirm>
  48. <Popconfirm placement="bottomRight" title={text} onConfirm={confirm} okText="Yes" cancelText="No">
  49. <Button>BR</Button>
  50. </Popconfirm>
  51. </div>
  52. </div>,
  53. mountNode);

Popconfirm 气泡确认框 - 图3

国际化

使用 okTextcancelText 自定义按钮文字。

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

Popconfirm 气泡确认框 - 图4

条件触发

可以判断是否需要弹出。

  1. import { Popconfirm, Switch, message } from 'choerodon-ui';
  2. class App extends React.Component {
  3. state = {
  4. visible: false,
  5. condition: true, // Whether meet the condition, if not show popconfirm.
  6. }
  7. changeCondition = (value) => {
  8. this.setState({ condition: value });
  9. }
  10. confirm = () => {
  11. this.setState({ visible: false });
  12. message.success('Next step.');
  13. }
  14. cancel = () => {
  15. this.setState({ visible: false });
  16. message.error('Click on cancel.');
  17. }
  18. handleVisibleChange = (visible) => {
  19. if (!visible) {
  20. this.setState({ visible });
  21. return;
  22. }
  23. // Determining condition before show the popconfirm.
  24. console.log(this.state.condition);
  25. if (this.state.condition) {
  26. this.confirm(); // next step
  27. } else {
  28. this.setState({ visible }); // show the popconfirm
  29. }
  30. }
  31. render() {
  32. return (
  33. <div>
  34. <Popconfirm
  35. title="Are you sure delete this task?"
  36. visible={this.state.visible}
  37. onVisibleChange={this.handleVisibleChange}
  38. onConfirm={this.confirm}
  39. onCancel={this.cancel}
  40. okText="Yes"
  41. cancelText="No"
  42. >
  43. <a href="#">Delete a task</a>
  44. </Popconfirm>
  45. <br />
  46. <br />
  47. Whether directly execute:<Switch defaultChecked onChange={this.changeCondition} />
  48. </div>
  49. );
  50. }
  51. }
  52. ReactDOM.render(<App />, mountNode);

API

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

更多属性请参考 Tooltip

注意

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