feat(Popup): add click-close-icon event (#7599)

This commit is contained in:
neverland 2020-11-21 18:36:23 +08:00 committed by GitHub
parent 5605b0aa57
commit b53254e33a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 38 additions and 17 deletions

View File

@ -130,10 +130,11 @@ export default {
### Events ### Events
| Event | Description | Arguments | | Event | Description | Arguments |
| ------------- | ------------------------------- | -------------- | | --- | --- | --- |
| click | Emitted when Popup is clicked | _event: Event_ | | click | Emitted when Popup is clicked | _event: Event_ |
| click-overlay | Emitted when overlay is clicked | - |
| click-close-icon `v2.11.0` | Emitted when close icon is clicked | _event: Event_ |
| open | Emitted when opening Popup | - | | open | Emitted when opening Popup | - |
| close | Emitted when closing Popup | - | | close | Emitted when closing Popup | - |
| opened | Emitted when Popup is opened | - | | opened | Emitted when Popup is opened | - |
| closed | Emitted when Popup is closed | - | | closed | Emitted when Popup is closed | - |
| click-overlay | Emitted when overlay is clicked | - |

View File

@ -141,10 +141,11 @@ export default {
### Events ### Events
| 事件名 | 说明 | 回调参数 | | 事件名 | 说明 | 回调参数 |
| ------------- | -------------------------- | -------------- | | -------------------------- | -------------------------- | -------------- |
| click | 点击弹出层时触发 | _event: Event_ | | click | 点击弹出层时触发 | _event: Event_ |
| click-overlay | 点击遮罩层时触发 | - |
| click-close-icon `v2.11.0` | 点击关闭图标时触发 | _event: Event_ |
| open | 打开弹出层时触发 | - | | open | 打开弹出层时触发 | - |
| close | 关闭弹出层时触发 | - | | close | 关闭弹出层时触发 | - |
| opened | 打开弹出层且动画结束后触发 | - | | opened | 打开弹出层且动画结束后触发 | - |
| closed | 关闭弹出层且动画结束后触发 | - | | closed | 关闭弹出层且动画结束后触发 | - |
| click-overlay | 点击遮罩层时触发 | - |

View File

@ -44,6 +44,13 @@ export default createComponent({
this.onClosed = createEmitter('closed'); this.onClosed = createEmitter('closed');
}, },
methods: {
onClickCloseIcon(event) {
this.$emit('click-close-icon', event);
this.close();
},
},
render() { render() {
if (!this.shouldRender) { if (!this.shouldRender) {
return; return;
@ -86,7 +93,7 @@ export default createComponent({
tabindex="0" tabindex="0"
name={this.closeIcon} name={this.closeIcon}
class={bem('close-icon', this.closeIconPosition)} class={bem('close-icon', this.closeIconPosition)}
onClick={this.close} onClick={this.onClickCloseIcon}
/> />
)} )}
</div> </div>

View File

@ -252,6 +252,18 @@ test('closeable prop', () => {
expect(wrapper.emitted('input')[0][0]).toEqual(false); expect(wrapper.emitted('input')[0][0]).toEqual(false);
}); });
test('should emit click-close-icon event when close icon is clicked', () => {
const wrapper = mount(Popup, {
propsData: {
value: true,
closeable: true,
},
});
wrapper.find('.van-popup__close-icon').trigger('click');
expect(wrapper.emitted('click-close-icon').length).toEqual(1);
});
test('close-icon prop', () => { test('close-icon prop', () => {
const wrapper = mount(Popup, { const wrapper = mount(Popup, {
propsData: { propsData: {