Skip to content

Commit 2f02bb4

Browse files
committed
feat(feedback): add feedback hooks: useModal useToast useLoading useActionSheet
1 parent 3b0f442 commit 2f02bb4

File tree

25 files changed

+644
-0
lines changed

25 files changed

+644
-0
lines changed

packages/app/src/app.config.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ export default {
2121
'pages/useSelectorQuery/getNode/index',
2222
'pages/useSelectorQuery/getContext/index',
2323
'pages/useSelectorQuery/getScrollOffset/index',
24+
// feedback
25+
'pages/useToast/index',
26+
'pages/useModal/index',
27+
'pages/useLoading/index',
28+
'pages/useActionSheet/index',
2429
// wechat
2530
'pages/useAPICheck/index',
2631
'pages/useUpdateManager/index',
472 Bytes
Binary file not shown.
320 Bytes
Binary file not shown.
316 Bytes
Binary file not shown.

packages/app/src/assets/style/icon.less

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,7 @@
2828
.iconfont-network:before {
2929
content: '\e64c';
3030
}
31+
32+
.iconfont-feedback:before {
33+
content: '\e7b5';
34+
}

packages/app/src/components/DocPage/index.less

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,7 @@
1313
left: auto;
1414
right: auto;
1515
}
16+
17+
.gap {
18+
margin: 10px 0;
19+
}

packages/app/src/constant/index.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ export const List: APIList = [
1414
content: '包含事件、调试等',
1515
icon: 'basic',
1616
},
17+
{
18+
id: 'Feedback',
19+
title: '操作反馈Hooks',
20+
content: '包含Toast, Modal等',
21+
icon: 'feedback',
22+
},
1723
{
1824
id: 'Environment',
1925
title: '环境Hooks',
@@ -44,6 +50,7 @@ export enum APIChildrenName {
4450
environment = 'environment',
4551
wechat = 'wechat',
4652
network = 'network',
53+
feedback = 'feedback',
4754
}
4855

4956
export const ChildrenList: { [_: string]: APIChildrenItem[] } = {
@@ -85,6 +92,24 @@ export const ChildrenList: { [_: string]: APIChildrenItem[] } = {
8592
name: 'usePromise 异步',
8693
},
8794
],
95+
[APIChildrenName.feedback]: [
96+
{
97+
id: 'useToast',
98+
name: 'useToast 消息提示框',
99+
},
100+
{
101+
id: 'useModal',
102+
name: 'useModal 模态对话框',
103+
},
104+
{
105+
id: 'useLoading',
106+
name: 'useLoading 加载提示框',
107+
},
108+
{
109+
id: 'useActionSheet',
110+
name: 'useActionSheet 操作菜单',
111+
},
112+
],
88113
[APIChildrenName.environment]: [
89114
{
90115
id: 'useEnv',
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export default {
2+
navigationBarTitleText: 'useActionSheet',
3+
};
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import React, { useCallback } from 'react';
2+
import { AtButton } from 'taro-ui';
3+
import DocPage from '@components/DocPage';
4+
5+
import { useActionSheet, useToast } from 'taro-hooks';
6+
import { showActionSheet } from '@tarojs/taro';
7+
8+
export default () => {
9+
const [showToast] = useToast({ mask: true });
10+
11+
const onActionItemTap = useCallback(
12+
(tapIndex, tapItem) => {
13+
showToast({
14+
title: tapIndex + ': ' + tapItem,
15+
});
16+
},
17+
[showToast],
18+
);
19+
20+
const [show] = useActionSheet({
21+
itemList: ['A', 'B', 'C'],
22+
onActionItemTap,
23+
});
24+
25+
const showCustomActionSheet = useCallback(() => {
26+
show({
27+
itemColor: '#C5D9E8',
28+
itemList: ['taro', 'hooks', 'taro-hooks'],
29+
}).then((res: showActionSheet.SuccessCallbackResult) => {
30+
showToast({
31+
title: String(res.tapIndex),
32+
});
33+
});
34+
}, [show, showToast]);
35+
36+
return (
37+
<>
38+
<DocPage title="useActionSheet 操作菜单" panelTitle="useActionSheet">
39+
<AtButton onClick={show}>展示带初始配置ActionSheet</AtButton>
40+
<AtButton className="gap" onClick={showCustomActionSheet}>
41+
展示新配置ActionSheet
42+
</AtButton>
43+
</DocPage>
44+
</>
45+
);
46+
};
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export default {
2+
navigationBarTitleText: 'useLoading',
3+
};
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import React, { useCallback } from 'react';
2+
import { AtButton } from 'taro-ui';
3+
import DocPage from '@components/DocPage';
4+
5+
import { useLoading } from 'taro-hooks';
6+
7+
export default () => {
8+
const [show, hide] = useLoading({
9+
mask: false,
10+
title: 'initial title',
11+
});
12+
13+
const showCustomLoading = useCallback(() => {
14+
show({
15+
title: '点击隐藏按钮进行隐藏',
16+
});
17+
}, [show]);
18+
19+
return (
20+
<>
21+
<DocPage title="useLoading 加载提示框" panelTitle="useLoading">
22+
<AtButton onClick={show}>展示带初始配置Loading</AtButton>
23+
<AtButton className="gap" onClick={showCustomLoading}>
24+
展示新配置Loading
25+
</AtButton>
26+
<AtButton onClick={hide}>隐藏Loading</AtButton>
27+
</DocPage>
28+
</>
29+
);
30+
};
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export default {
2+
navigationBarTitleText: 'useModal',
3+
};
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import React, { useCallback } from 'react';
2+
import { AtButton } from 'taro-ui';
3+
import DocPage from '@components/DocPage';
4+
5+
import { useModal } from 'taro-hooks';
6+
7+
export default () => {
8+
const [show] = useModal({
9+
title: 'initial title',
10+
content: 'initial content',
11+
});
12+
13+
const showCustomModal = useCallback(() => {
14+
show({
15+
title: 'taro hooks',
16+
content: 'taro hooks 还不戳!',
17+
showCancel: false,
18+
confirmText: '确实!',
19+
confirmColor: '#4569d4',
20+
});
21+
}, [show]);
22+
23+
return (
24+
<>
25+
<DocPage title="useModal 模态对话框" panelTitle="useModal">
26+
<AtButton onClick={show}>展示带初始配置Modal</AtButton>
27+
<AtButton className="gap" onClick={showCustomModal}>
28+
展示新配置Modal
29+
</AtButton>
30+
</DocPage>
31+
</>
32+
);
33+
};

packages/app/src/pages/useRouter/index.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ export default () => {
2626
跳转TabBar
2727
</AtButton>
2828
<AtButton
29+
className="gap"
2930
disabled={env === ENV_TYPE.WEB}
3031
onClick={() => relaunch('/pages/useRequest/index')}
3132
>
@@ -38,6 +39,7 @@ export default () => {
3839
重定向页面
3940
</AtButton>
4041
<AtButton
42+
className="gap"
4143
disabled={env === ENV_TYPE.WEB}
4244
onClick={() =>
4345
navigateTo('/pages/useLaunchOptions/index', { from: 'useRoute' })
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export default {
2+
navigationBarTitleText: 'useToast',
3+
};
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import React, { useCallback } from 'react';
2+
import { AtButton } from 'taro-ui';
3+
import DocPage from '@components/DocPage';
4+
5+
import { useToast } from 'taro-hooks';
6+
import image from '../../assets/icon/about-select.png';
7+
8+
export default () => {
9+
const [show, hide] = useToast({
10+
mask: true,
11+
duration: 1000,
12+
title: 'initial title',
13+
icon: 'success',
14+
});
15+
16+
const showCustomToast = useCallback(() => {
17+
show({
18+
title: '点击隐藏按钮进行隐藏',
19+
image,
20+
duration: 3000000,
21+
mask: false,
22+
});
23+
}, [show]);
24+
25+
return (
26+
<>
27+
<DocPage title="useToast 消息提示框" panelTitle="useToast">
28+
<AtButton onClick={show}>展示带初始配置Toast</AtButton>
29+
<AtButton className="gap" onClick={showCustomToast}>
30+
展示新配置Toast
31+
</AtButton>
32+
<AtButton onClick={hide}>隐藏新配置Toast</AtButton>
33+
</DocPage>
34+
</>
35+
);
36+
};

packages/hooks/src/index.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ import useEvent from './useEvent';
1313
import useVisible from './useVisible';
1414
import useSelectorQuery from './useSelectorQuery';
1515

16+
// feedback
17+
import useToast from './useToast';
18+
import useModal from './useModal';
19+
import useLoading from './useLoading';
20+
import useActionSheet from './useActionSheet';
21+
1622
// wechat
1723
import useAPICheck from './useAPICheck';
1824
import useUpdateManager from './useUpdateManager';
@@ -39,4 +45,8 @@ export {
3945
useNetworkType,
4046
useOnline,
4147
useRequest,
48+
useToast,
49+
useModal,
50+
useLoading,
51+
useActionSheet,
4252
};
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
---
2+
title: useActionSheet
3+
nav:
4+
title: Hooks
5+
path: /hooks
6+
order: 2
7+
group:
8+
title: 操作反馈
9+
path: /feedback
10+
---
11+
12+
# useActionSheet
13+
14+
显示操作菜单
15+
16+
## 何时使用
17+
18+
当需要使用操作菜单
19+
20+
## API
21+
22+
```jsx | pure
23+
const [show] = useActionSheet(initialOption);
24+
```
25+
26+
## 参数说明
27+
28+
| 参数 | 说明 | 类型 | 默认值 |
29+
| ------------- | ---------------------------------------- | -------------------- | ------ |
30+
| initialOption | 初始操作菜单(若指定后面可与新的配置合并) | `General.IAnyObject` | - |
31+
32+
## 返回值说明
33+
34+
| 返回值 | 说明 | 类型 |
35+
| ------ | ------------ | ------------------------------------------------ |
36+
| show | 显示操作菜单 | `(options?: General.IAnyObject) => Promise<any>` |
37+
38+
## 代码演示
39+
40+
<code src="@pages/useActionSheet" />
41+
42+
## Hook 支持度
43+
44+
| 微信小程序 | H5 | ReactNative |
45+
| :--------: | :-: | :---------: |
46+
| ✔️ | ✔️ | ✔️ |
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import { showActionSheet, General } from '@tarojs/taro';
2+
import { useCallback, useEffect, useRef, useState } from 'react';
3+
4+
export interface ActionSheetOption {
5+
itemList: string[];
6+
itemColor?: string;
7+
onActionItemTap?: onActionItemTap;
8+
}
9+
10+
export type ShowActionSheet = (
11+
option?: ActionSheetOption,
12+
) => Promise<General.CallbackResult>;
13+
14+
export type onActionItemTap = (
15+
tapIndex: number,
16+
tapItem: string | undefined,
17+
) => any;
18+
19+
function useActionSheet(option?: ActionSheetOption): [ShowActionSheet] {
20+
const initialOption = useRef<ActionSheetOption>();
21+
22+
useEffect(() => {
23+
initialOption.current = option;
24+
}, [option]);
25+
26+
const showActionSheetAsync = useCallback<ShowActionSheet>(
27+
(option?: ActionSheetOption) => {
28+
return new Promise((resolve, reject) => {
29+
try {
30+
if (!option && !initialOption.current) {
31+
console.warn('please provide a option');
32+
return reject(new Error('please provide a option'));
33+
} else {
34+
const { onActionItemTap, ...options } = Object.assign(
35+
{},
36+
initialOption.current || {},
37+
option || {},
38+
) as ActionSheetOption;
39+
showActionSheet({
40+
...options,
41+
success: (res) => {
42+
if (onActionItemTap) {
43+
const tapIndex = res.tapIndex;
44+
onActionItemTap(
45+
tapIndex,
46+
options.itemList.find((v, i) => i === tapIndex),
47+
);
48+
}
49+
resolve(res);
50+
},
51+
fail: reject,
52+
}).catch(reject);
53+
}
54+
} catch (e) {
55+
reject(e);
56+
}
57+
});
58+
},
59+
[initialOption],
60+
);
61+
62+
return [showActionSheetAsync];
63+
}
64+
65+
export default useActionSheet;

0 commit comments

Comments
 (0)