From b5ecc922a39cbd0877e3e2ae77783a72e2bedbc0 Mon Sep 17 00:00:00 2001 From: neverland Date: Sat, 7 Dec 2019 16:33:27 +0800 Subject: [PATCH] feat(Notify): add clear method (#2501) --- packages/notify/README.md | 11 +++++++++-- packages/notify/notify.ts | 16 ++++++++++++++-- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/packages/notify/README.md b/packages/notify/README.md index 3f3218de..9892cb40 100644 --- a/packages/notify/README.md +++ b/packages/notify/README.md @@ -76,18 +76,25 @@ Notify({ ## API +### 方法 + +| 方法名 | 说明 | 参数 | 返回值 | +|------|------|------|------| +| Notify | 展示提示 | `options | message` | notify 实例 | +| Notify.clear | 关闭提示 | `options` | `void` | + ### Options | 参数 | 说明 | 类型 | 默认值 | 版本 | |-----------|-----------|-----------|-------------|-------------| -| type | 类型,可选值为 `primary` `info` `warning` | *string* | `danger` | 1.0.0 | +| type | 类型,可选值为 `primary` `success` `warning` | *string* | `danger` | 1.0.0 | | message | 展示文案,支持通过`\n`换行 | *string* | 1.0.0 | - | | duration | 展示时长(ms),值为 0 时,notify 不会消失 | *number* | `3000` | - | | selector | 自定义节点选择器 | *string* | `van-notify` | - | | color | 字体颜色 | *string* | `#fff` | - | | background | 背景颜色 | *string* | - | - | | context | 选择器的选择范围,可以传入自定义组件的 this 作为上下文 | *object* | 当前页面 | - | -| safe-area-inset-top | 是否留出顶部安全距离(状态栏高度) | *boolean* | `false` | - | | onClick | 点击时的回调函数 | *Function* | - | - | | onOpened | 完全展示后的回调函数 | *Function* | - | - | | onClose | 关闭时的回调函数 | *Function* | - | - | +| safeAreaInsetTop | 是否留出顶部安全距离(状态栏高度) | *boolean* | `false` | - | diff --git a/packages/notify/notify.ts b/packages/notify/notify.ts index 071fa2f8..37636c7d 100644 --- a/packages/notify/notify.ts +++ b/packages/notify/notify.ts @@ -49,7 +49,19 @@ export default function Notify(options: NotifyOptions | string) { if (notify) { notify.set(options); notify.show(); - } else { - console.warn('未找到 van-notify 节点,请确认 selector 及 context 是否正确'); + return notify; } + + console.warn('未找到 van-notify 节点,请确认 selector 及 context 是否正确'); } + +Notify.clear = function(options?: NotifyOptions) { + options = Object.assign({}, defaultOptions, parseOptions(options)); + + const context = options.context || getContext(); + const notify = context.selectComponent(options.selector); + + if (notify) { + notify.hide(); + } +};