Popconfirm
点击元素,弹出气泡式的确认框。
何时使用
目标元素的操作需要用户进一步的确认时,在目标元素附近弹出浮层提示,询问用户。
和 ‘confirm’ 弹出的全屏居中模态对话框相比,交互形式更轻量。
代码演示
基本
最简单的用法。
<template>
<a-popconfirm
title="Are you sure delete this task?"
ok-text="Yes"
cancel-text="No"
@confirm="confirm"
@cancel="cancel"
>
<a href="#">Delete</a>
</a-popconfirm>
</template>
<script>
export default {
methods: {
confirm(e) {
console.log(e);
this.$message.success('Click on Yes');
},
cancel(e) {
console.log(e);
this.$message.error('Click on No');
},
},
};
</script>
位置
位置有十二个方向。如需箭头指向目标元素中心,可以设置 arrowPointAtCenter
。
<template>
<div id="components-a-popconfirm-demo-placement">
<div :style="{ marginLeft: `${buttonWidth}px`, whiteSpace: 'nowrap' }">
<a-popconfirm placement="topLeft" ok-text="Yes" cancel-text="No" @confirm="confirm">
<template slot="title">
<p>{{ text }}</p>
<p>{{ text }}</p>
</template>
<a-button>TL</a-button>
</a-popconfirm>
<a-popconfirm placement="top" ok-text="Yes" cancel-text="No" @confirm="confirm">
<template slot="title">
<p>{{ text }}</p>
<p>{{ text }}</p>
</template>
<a-button>Top</a-button>
</a-popconfirm>
<a-popconfirm placement="topRight" ok-text="Yes" cancel-text="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" ok-text="Yes" cancel-text="No" @confirm="confirm">
<template slot="title">
<p>{{ text }}</p>
<p>{{ text }}</p>
</template>
<a-button>LT</a-button>
</a-popconfirm>
<a-popconfirm placement="left" ok-text="Yes" cancel-text="No" @confirm="confirm">
<template slot="title">
<p>{{ text }}</p>
<p>{{ text }}</p>
</template>
<a-button>Left</a-button>
</a-popconfirm>
<a-popconfirm placement="leftBottom" ok-text="Yes" cancel-text="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" ok-text="Yes" cancel-text="No" @confirm="confirm">
<template slot="title">
<p>{{ text }}</p>
<p>{{ text }}</p>
</template>
<a-button>RT</a-button>
</a-popconfirm>
<a-popconfirm placement="right" ok-text="Yes" cancel-text="No" @confirm="confirm">
<template slot="title">
<p>{{ text }}</p>
<p>{{ text }}</p>
</template>
<a-button>Right</a-button>
</a-popconfirm>
<a-popconfirm placement="rightBottom" ok-text="Yes" cancel-text="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" ok-text="Yes" cancel-text="No" @confirm="confirm">
<template slot="title">
<p>{{ text }}</p>
<p>{{ text }}</p>
</template>
<a-button>BL</a-button>
</a-popconfirm>
<a-popconfirm placement="bottom" ok-text="Yes" cancel-text="No" @confirm="confirm">
<template slot="title">
<p>{{ text }}</p>
<p>{{ text }}</p>
</template>
<a-button>Bottom</a-button>
</a-popconfirm>
<a-popconfirm placement="bottomRight" ok-text="Yes" cancel-text="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>
自定义 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>
国际化
使用 okText
和 cancelText
自定义按钮文字。
<template>
<a-popconfirm title="Are you sure?" ok-text="Yes" cancel-text="No">
<a href="#">Delete</a>
</a-popconfirm>
</template>
条件触发
可以判断是否需要弹出。
<template>
<div>
<a-popconfirm
title="Are you sure delete this task?"
:visible="visible"
ok-text="Yes"
cancel-text="No"
@visibleChange="handleVisibleChange"
@confirm="confirm"
@cancel="cancel"
>
<a href="#">Delete a task</a>
</a-popconfirm>
<br />
<br />
Whether directly execute:<a-checkbox default-checked @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>
API
参数 | 说明 | 类型 | 默认值 | 版本 |
---|---|---|---|---|
cancelText | 取消按钮文字 | string|slot | 取消 | |
okText | 确认按钮文字 | string|slot | 确定 | |
okType | 确认按钮类型 | string | primary | |
title | 确认框的描述 | string|slot | 无 | |
icon | 自定义弹出气泡 Icon 图标 | vNode | <Icon type=”exclamation-circle” /> | |
disabled | 点击 Popconfirm 子元素是否弹出气泡确认框 | boolean | false | 1.5.0 |
事件
事件名称 | 说明 | 回调参数 |
---|---|---|
cancel | 点击取消的回调 | function(e) |
confirm | 点击确认的回调 | function(e) |
visibleChange | 显示隐藏的回调 | function(visible) |
更多属性请参考 Tooltip。
注意
请确保 Popconfirm
的子元素能接受 mouseenter
、mouseleave
、focus
、click
事件。