Popconfirm

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

何时使用

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

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

代码演示

Popconfirm 气泡确认框 - 图1

基本

最简单的用法。

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

<template>
  <div id="components-a-popconfirm-demo-placement">
    <div :style="{ marginLeft: `${buttonWidth}px`, whiteSpace: 'nowrap' }">
      <a-popconfirm placement="topLeft" okText="Yes" cancelText="No" @confirm="confirm">
        <template slot="title">
          <p>{{text}}</p>
          <p>{{text}}</p>
        </template>
        <a-button>TL</a-button>
      </a-popconfirm>
      <a-popconfirm placement="top" okText="Yes" cancelText="No" @confirm="confirm">
        <template slot="title">
          <p>{{text}}</p>
          <p>{{text}}</p>
        </template>
        <a-button>Top</a-button>
      </a-popconfirm>
      <a-popconfirm placement="topRight" okText="Yes" cancelText="No" @confirm="confirm">
        <template slot="title">
          <p>{{text}}</p>
          <p>{{text}}</p>
        </template>
        <a-button>TR</a-button>
      </a-popconfirm>
    </div>
    <div :style="{ width: `${buttonWidth}px`, float: 'left' }">
      <a-popconfirm placement="leftTop" okText="Yes" cancelText="No" @confirm="confirm">
        <template slot="title">
          <p>{{text}}</p>
          <p>{{text}}</p>
        </template>
        <a-button>LT</a-button>
      </a-popconfirm>
      <a-popconfirm placement="left" okText="Yes" cancelText="No" @confirm="confirm">
        <template slot="title">
          <p>{{text}}</p>
          <p>{{text}}</p>
        </template>
        <a-button>Left</a-button>
      </a-popconfirm>
      <a-popconfirm placement="leftBottom" okText="Yes" cancelText="No" @confirm="confirm">
        <template slot="title">
          <p>{{text}}</p>
          <p>{{text}}</p>
        </template>
        <a-button>LB</a-button>
      </a-popconfirm>
    </div>
    <div :style="{ width: `${buttonWidth}px`, marginLeft: `${buttonWidth * 4 + 24 }px`}">
      <a-popconfirm placement="rightTop" okText="Yes" cancelText="No" @confirm="confirm">
        <template slot="title">
          <p>{{text}}</p>
          <p>{{text}}</p>
        </template>
        <a-button>RT</a-button>
      </a-popconfirm>
      <a-popconfirm placement="right" okText="Yes" cancelText="No" @confirm="confirm">
        <template slot="title">
          <p>{{text}}</p>
          <p>{{text}}</p>
        </template>
        <a-button>Right</a-button>
      </a-popconfirm>
      <a-popconfirm placement="rightBottom" okText="Yes" cancelText="No" @confirm="confirm">
        <template slot="title">
          <p>{{text}}</p>
          <p>{{text}}</p>
        </template>
        <a-button>RB</a-button>
      </a-popconfirm>
    </div>
    <div :style="{ marginLeft: `${buttonWidth}px`, clear: 'both', whiteSpace: 'nowrap' }">
      <a-popconfirm placement="bottomLeft" okText="Yes" cancelText="No" @confirm="confirm">
        <template slot="title">
          <p>{{text}}</p>
          <p>{{text}}</p>
        </template>
        <a-button>BL</a-button>
      </a-popconfirm>
      <a-popconfirm placement="bottom" okText="Yes" cancelText="No" @confirm="confirm">
        <template slot="title">
          <p>{{text}}</p>
          <p>{{text}}</p>
        </template>
        <a-button>Bottom</a-button>
      </a-popconfirm>
      <a-popconfirm placement="bottomRight" okText="Yes" cancelText="No" @confirm="confirm">
        <template slot="title">
          <p>{{text}}</p>
          <p>{{text}}</p>
        </template>
        <a-button>BR</a-button>
      </a-popconfirm>
    </div>
  </div>
</template>
<script>
  import { message } from 'ant-design-vue';

  export default {
    data() {
      return {
        buttonWidth: 70,
        text: 'Are you sure to delete this task?',
      };
    },
    methods: {
      confirm() {
        message.info('Clicked on Yes.');
      },
    },
  };
</script>
<style scoped>
  #components-a-popconfirm-demo-placement .ant-btn {
    width: 70px;
    text-align: center;
    padding: 0;
    margin-right: 8px;
    margin-bottom: 8px;
  }
</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 图标vNode<Icon type="exclamation-circle" />

事件

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

更多属性请参考 Tooltip

注意

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