Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(toast): toast 支持手动关闭 #514

Merged
merged 3 commits into from
Jun 8, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/toast/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,24 @@ Toast({
duration: 3000,
icon: 'star',
});
```
### 手动关闭轻提示
```js
import { showToast, hideToast } from 'tdesign-miniprogram/toast/index';
```

```js
showToast({
context: this,
selector: '#t-toast',
message: '成功文案',
});

hideToast({
context: this,
selector: '#t-toast',
});

```

## API
Expand Down
23 changes: 22 additions & 1 deletion src/toast/_example/toast.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Toast from 'tdesign-miniprogram/toast/index';
import Toast, { hideToast } from 'tdesign-miniprogram/toast/index';

Page({
data: {
Expand Down Expand Up @@ -81,6 +81,17 @@ Page({
],
},
],
operList4: [
{
title: '手动关闭轻提示',
btns: [
{
type: 'hideToast',
text: '关闭提示',
},
],
},
],
},
handleToast(option) {
Toast({
Expand All @@ -89,6 +100,12 @@ Page({
...option,
});
},
hideToast() {
hideToast({
context: this,
selector: '#t-toast',
});
},
clickHandle(e) {
switch (e.detail) {
case 'pureText': {
Expand Down Expand Up @@ -196,6 +213,10 @@ Page({
});
break;
}
case 'hideToast': {
this.hideToast();
break;
}
default: {
this.handleToast({
message: '未知点击事件',
Expand Down
1 change: 1 addition & 0 deletions src/toast/_example/toast.wxml
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
<t-demo title="01 基本用法" desc="" operList="{{operList1}}" bindclickoper="clickHandle" />
<t-demo title="02 展示位置和展示时间" desc="" operList="{{operList2}}" bindclickoper="clickHandle" />
<t-demo title="03 显示遮罩" desc="" operList="{{operList3}}" bindclickoper="clickHandle" />
<t-demo title="04 手动关闭" desc="" operList="{{operList4}}" bindclickoper="clickHandle" />
</view>
<t-toast id="t-toast" />
16 changes: 15 additions & 1 deletion src/toast/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const getInstance = (context?: Context, selector = '#t-toast') => {
return instance;
};

export default function (options: ToastOptionsType) {
function Toast(options: ToastOptionsType) {
const { context, selector, ...Options } = options;
const instance = getInstance(context, selector);
if (instance) {
Expand All @@ -40,3 +40,17 @@ export default function (options: ToastOptionsType) {
});
}
}

function showToast(options: ToastOptionsType) {
Toast(options);
}

function hideToast(options: ToastOptionsType) {
const { context, selector } = options;
const instance = getInstance(context, selector);
if (instance) {
instance.hide();
}
}

export { Toast as default, showToast, hideToast };
4 changes: 3 additions & 1 deletion src/toast/toast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,9 @@ export default class Toast extends SuperComponent {
this.clear();
}, duration as any);
},

hide() {
this.clear();
},
clear() {
this.setData({ show: false });
this.removeTimer = setTimeout(() => {
Expand Down