Skip to content

Commit

Permalink
feat: add toast
Browse files Browse the repository at this point in the history
  • Loading branch information
novlan1 committed Mar 1, 2024
1 parent e5b4960 commit 823e9cd
Show file tree
Hide file tree
Showing 3 changed files with 212 additions and 15 deletions.
148 changes: 145 additions & 3 deletions docs/zh/toast.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,115 @@
## 引入

```ts
import { showLoading-web } from 't-comm';
import {
showToast,
showSuccess,
showFail,
clearToast,
showLoading,
dismissLoading,
Toast
} from 't-comm';

// or

import { showLoading-web} from 't-comm/lib/toast/index';
import {
showToast,
showSuccess,
showFail,
clearToast,
showLoading,
dismissLoading,
Toast
} from 't-comm/lib/toast/index';
```


## `showLoading-web(options)`
## `showToast`


**描述**:<p>显示普通Toast</p>

**参数**


| 参数名 | 描述 |
| --- | --- |
| text | <p>文案</p> |
| duration | <p>显示时间,默认2秒</p> |



**示例**

```ts
Toast.show('文本', 3000);
```
<a name="showSuccess"></a>

## `showSuccess`


**描述**:<p>显示成功样式Toast(toast带√样式)</p>

**参数**


| 参数名 | 描述 |
| --- | --- |
| text | <p>文案</p> |
| duration | <p>显示时间,默认2秒</p> |



**示例**

```ts
Toast.showSuccess('文本', 3000);
```
<a name="showFail"></a>

## `showFail`


**描述**:<p>显示失败样式 Toast(toast带!样式)</p>

**参数**


| 参数名 | 描述 |
| --- | --- |
| text | <p>文案</p> |
| duration | <p>显示时间,默认2秒</p> |



**示例**

```ts
Toast.showFail('文本', 3000);
```
<a name="clearToast"></a>

## `clearToast`


**描述**:<p>清除(隐藏)上一个toast</p>

**参数**



**示例**

```ts
Toast.clear();

clearToast();
```
<a name="showLoading"></a>

## `showLoading`


**描述**:<p>显示loading Toast</p>
Expand All @@ -29,3 +129,45 @@ import { showLoading-web} from 't-comm/lib/toast/index';



**示例**

```ts
Toast.showLoading('文本');

Toast.showLoading({
message: '文本',
zIndex: 1000,
});
```
<a name="dismissLoading"></a>

## `dismissLoading`


**描述**:<p>隐藏loading toast</p>

**参数**



**示例**

```ts
Toast.dismissLoading();
```
<a name="Toast"></a>

## `Toast`


**描述**:<p>Toast 对象</p>

**参数**



**示例**

```ts
Toast.show('文本')
```
10 changes: 9 additions & 1 deletion src/toast/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,9 @@
export { Toast } from './toast';
export {
Toast,
showToast,
showSuccess,
showFail,
clearToast,
showLoading,
dismissLoading,
} from './toast';
69 changes: 58 additions & 11 deletions src/toast/toast.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@

let loadingToastId: any = {};
let toastId: any = {};
const mToast: {[key: string]: Function} = {};

let toastComp: any;

Expand All @@ -25,8 +24,12 @@ function getToast() {
* 显示普通Toast
* @param text 文案
* @param duration 显示时间,默认2秒
* @example
* ```ts
* Toast.show('文本', 3000);
* ```
*/
mToast.show = function (text = '', duration = 2000) {
export const showToast = function (text = '', duration = 2000) {
getToast().then(() => {
if (loadingToastId?.clear) {
setTimeout(() => {
Expand All @@ -42,8 +45,12 @@ mToast.show = function (text = '', duration = 2000) {
* 显示成功样式Toast(toast带√样式)
* @param text 文案
* @param duration 显示时间,默认2秒
* @example
* ```ts
* Toast.showSuccess('文本', 3000);
* ```
*/
mToast.showSuccess = function (text = '', duration = 2000) {
export const showSuccess = function (text = '', duration = 2000) {
getToast().then(() => {
if (loadingToastId?.clear) {
setTimeout(() => {
Expand All @@ -56,11 +63,15 @@ mToast.showSuccess = function (text = '', duration = 2000) {
};

/**
* 显示失败样式Taost(toast带!样式)
* 显示失败样式 Toast(toast带!样式)
* @param text 文案
* @param duration 显示时间,默认2秒
* @example
* ```ts
* Toast.showFail('文本', 3000);
* ```
*/
mToast.showFail = function (text = '', duration = 2000) {
export const showFail = function (text = '', duration = 2000) {
getToast().then(() => {
if (loadingToastId?.clear) {
setTimeout(() => {
Expand All @@ -74,22 +85,35 @@ mToast.showFail = function (text = '', duration = 2000) {

/**
* 清除(隐藏)上一个toast
* @example
* ```ts
* Toast.clear();
*
* clearToast();
* ```
*/
mToast.clear = function () {
export const clearToast = function () {
if (toastId.clear) toastId.clear();
};

/**
* 显示loading Toast
* @function
* @name showLoading-web
* @param {string | object} options 配置,传递字符串时候为message
* @param {string} options.message 内容
* @param {number} options.duration 展示时长(ms),值为 0 时,toast 不会消失
* @param {boolean} options.forbidClick 是否禁止背景点击
* @param {string} options.selector 自定义选择器
* @example
* ```ts
* Toast.showLoading('文本');
*
* Toast.showLoading({
* message: '文本',
* zIndex: 1000,
* });
* ```
*/
mToast.showLoading = function (options = {}) {
export const showLoading = function (options = {}) {
getToast().then(() => {
if (loadingToastId?.clear) {
loadingToastId.clear();
Expand All @@ -105,13 +129,36 @@ mToast.showLoading = function (options = {}) {

/**
* 隐藏loading toast
* @example
* ```ts
* Toast.dismissLoading();
* ```
*/
mToast.dismissLoading = function () {
export const dismissLoading = function () {
if (loadingToastId?.clear) {
loadingToastId.clear();
loadingToastId = {};
}
};

/**
* Toast 对象
* @example
* ```ts
* Toast.show('文本')
* ```
*/
export const Toast = {
show: showToast,
showToast,

showSuccess,
showFail,

clear: clearToast,
clearToast,

showLoading,
dismissLoading,
};

export { mToast as Toast };

0 comments on commit 823e9cd

Please sign in to comment.