Popconfirm

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

何时使用

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

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

代码演示

Popconfirm气泡确认框 - 图1

基本

最简单的用法。

  1. <template>
  2. <a-popconfirm title="Are you sure delete this task?" @confirm="confirm" @cancel="cancel" okText="Yes" cancelText="No">
  3. <a href="#">Delete</a>
  4. </a-popconfirm>
  5. </template>
  6. <script>
  7. export default {
  8. methods: {
  9. confirm (e) {
  10. console.log(e)
  11. this.$message.success('Click on Yes')
  12. },
  13. cancel (e) {
  14. console.log(e)
  15. this.$message.error('Click on No')
  16. },
  17. },
  18. }
  19. </script>

Popconfirm气泡确认框 - 图2

国际化

使用 okTextcancelText 自定义按钮文字。

  1. <template>
  2. <a-popconfirm title="Are you sure?" okText="Yes" cancelText="No">
  3. <a href="#">Delete</a>
  4. </a-popconfirm>
  5. </template>

Popconfirm气泡确认框 - 图3

位置

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

  1. <template>
  2. <div id="components-a-popconfirm-demo-placement">
  3. <div :style="{ marginLeft: `${buttonWidth}px`, whiteSpace: 'nowrap' }">
  4. <a-popconfirm placement="topLeft" okText="Yes" cancelText="No" @confirm="confirm">
  5. <template slot="title">
  6. <p>{{text}}</p>
  7. <p>{{text}}</p>
  8. </template>
  9. <a-button>TL</a-button>
  10. </a-popconfirm>
  11. <a-popconfirm placement="top" okText="Yes" cancelText="No" @confirm="confirm">
  12. <template slot="title">
  13. <p>{{text}}</p>
  14. <p>{{text}}</p>
  15. </template>
  16. <a-button>Top</a-button>
  17. </a-popconfirm>
  18. <a-popconfirm placement="topRight" okText="Yes" cancelText="No" @confirm="confirm">
  19. <template slot="title">
  20. <p>{{text}}</p>
  21. <p>{{text}}</p>
  22. </template>
  23. <a-button>TR</a-button>
  24. </a-popconfirm>
  25. </div>
  26. <div :style="{ width: `${buttonWidth}px`, float: 'left' }">
  27. <a-popconfirm placement="leftTop" okText="Yes" cancelText="No" @confirm="confirm">
  28. <template slot="title">
  29. <p>{{text}}</p>
  30. <p>{{text}}</p>
  31. </template>
  32. <a-button>LT</a-button>
  33. </a-popconfirm>
  34. <a-popconfirm placement="left" okText="Yes" cancelText="No" @confirm="confirm">
  35. <template slot="title">
  36. <p>{{text}}</p>
  37. <p>{{text}}</p>
  38. </template>
  39. <a-button>Left</a-button>
  40. </a-popconfirm>
  41. <a-popconfirm placement="leftBottom" okText="Yes" cancelText="No" @confirm="confirm">
  42. <template slot="title">
  43. <p>{{text}}</p>
  44. <p>{{text}}</p>
  45. </template>
  46. <a-button>LB</a-button>
  47. </a-popconfirm>
  48. </div>
  49. <div :style="{ width: `${buttonWidth}px`, marginLeft: `${buttonWidth * 4 + 24 }px`}">
  50. <a-popconfirm placement="rightTop" okText="Yes" cancelText="No" @confirm="confirm">
  51. <template slot="title">
  52. <p>{{text}}</p>
  53. <p>{{text}}</p>
  54. </template>
  55. <a-button>RT</a-button>
  56. </a-popconfirm>
  57. <a-popconfirm placement="right" okText="Yes" cancelText="No" @confirm="confirm">
  58. <template slot="title">
  59. <p>{{text}}</p>
  60. <p>{{text}}</p>
  61. </template>
  62. <a-button>Right</a-button>
  63. </a-popconfirm>
  64. <a-popconfirm placement="rightBottom" okText="Yes" cancelText="No" @confirm="confirm">
  65. <template slot="title">
  66. <p>{{text}}</p>
  67. <p>{{text}}</p>
  68. </template>
  69. <a-button>RB</a-button>
  70. </a-popconfirm>
  71. </div>
  72. <div :style="{ marginLeft: `${buttonWidth}px`, clear: 'both', whiteSpace: 'nowrap' }">
  73. <a-popconfirm placement="bottomLeft" okText="Yes" cancelText="No" @confirm="confirm">
  74. <template slot="title">
  75. <p>{{text}}</p>
  76. <p>{{text}}</p>
  77. </template>
  78. <a-button>BL</a-button>
  79. </a-popconfirm>
  80. <a-popconfirm placement="bottom" okText="Yes" cancelText="No" @confirm="confirm">
  81. <template slot="title">
  82. <p>{{text}}</p>
  83. <p>{{text}}</p>
  84. </template>
  85. <a-button>Bottom</a-button>
  86. </a-popconfirm>
  87. <a-popconfirm placement="bottomRight" okText="Yes" cancelText="No" @confirm="confirm">
  88. <template slot="title">
  89. <p>{{text}}</p>
  90. <p>{{text}}</p>
  91. </template>
  92. <a-button>BR</a-button>
  93. </a-popconfirm>
  94. </div>
  95. </div>
  96. </template>
  97. <script>
  98. import { message } from 'ant-design-vue'
  99. export default {
  100. data () {
  101. return {
  102. buttonWidth: 70,
  103. text: 'Are you sure to delete this task?',
  104. }
  105. },
  106. methods: {
  107. confirm () {
  108. message.info('Clicked on Yes.')
  109. },
  110. },
  111. }
  112. </script>
  113. <style scoped>
  114. #components-a-popconfirm-demo-placement .ant-btn {
  115. width: 70px;
  116. text-align: center;
  117. padding: 0;
  118. margin-right: 8px;
  119. margin-bottom: 8px;
  120. }
  121. </style>

Popconfirm气泡确认框 - 图4

条件触发

可以判断是否需要弹出。

<template>
  <div>
    <a-popconfirm
      title="Are you sure delete this task?"
      :visible="visible"
      @visibleChange="handleVisibleChange"
      @confirm="confirm"
      @cancel="cancel"
      okText="Yes"
      cancelText="No"
    >
      <a href="#">Delete a task</a>
    </a-popconfirm>
    <br />
    <br />
    Whether directly execute:<a-checkbox defaultChecked @change="changeCondition" />
  </div>
</template>
<script>
import { message } from 'ant-design-vue'

export default {
  data () {
    return {
      visible: false,
      condition: true,
    }
  },
  methods: {
    changeCondition (e) {
      this.condition = e.target.checked
    },
    confirm () {
      this.visible = false
      message.success('Next step.')
    },
    cancel () {
      this.visible = false
      message.error('Click on cancel.')
    },
    handleVisibleChange (visible) {
      if (!visible) {
        this.visible = false
        return
      }
      // Determining condition before show the popconfirm.
      console.log(this.condition)
      if (this.condition) {
        this.confirm() // next step
      } else {
        this.visible = true
      }
    },
  },
}
</script>

Popconfirm气泡确认框 - 图5

自定义 Icon 图标

使用 icon 自定义提示 icon

<template>
  <a-popconfirm title="Are you sure?">
    <a-icon slot="icon" type="question-circle-o" style="color: red" />
    <a href="#">Delete</a>
  </a-popconfirm>
</template>

API

参数说明类型默认值
cancelText取消按钮文字string|slot取消
okText确认按钮文字string|slot确定
okType确认按钮类型stringprimary
title确认框的描述string|slot
icon自定义弹出气泡 Icon 图标ReactNode<Icon type="exclamation-circle" />

事件

事件名称说明回调参数
cancel点击取消的回调function(e)
confirm点击确认的回调function(e)
visibleChange显示隐藏的回调function(visible)

更多属性请参考 Tooltip

注意

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