Skip to content

Commit

Permalink
fix(type): fix type of feedback hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
innocces committed Sep 15, 2021
1 parent f45191b commit 4728379
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 43 deletions.
48 changes: 28 additions & 20 deletions packages/hooks/src/useActionSheet/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { showActionSheet, General } from '@tarojs/taro';
import { useCallback, useEffect, useRef, useState } from 'react';
import { useCallback, useEffect, useRef } from 'react';

export interface ActionSheetOption {
itemList: string[];
Expand All @@ -8,23 +8,25 @@ export interface ActionSheetOption {
}

export type ShowActionSheet = (
option?: ActionSheetOption,
option?: Partial<ActionSheetOption>,
) => Promise<General.CallbackResult>;

export type onActionItemTap = (
tapIndex: number,
tapItem: string | undefined,
) => any;

function useActionSheet(option?: ActionSheetOption): [ShowActionSheet] {
const initialOption = useRef<ActionSheetOption>();
function useActionSheet(
option?: Partial<ActionSheetOption>,
): [ShowActionSheet] {
const initialOption = useRef<Partial<ActionSheetOption>>();

useEffect(() => {
initialOption.current = option;
}, [option]);

const showActionSheetAsync = useCallback<ShowActionSheet>(
(option?: ActionSheetOption) => {
(option?: Partial<ActionSheetOption>) => {
return new Promise((resolve, reject) => {
try {
if (!option && !initialOption.current) {
Expand All @@ -35,21 +37,27 @@ function useActionSheet(option?: ActionSheetOption): [ShowActionSheet] {
{},
initialOption.current || {},
option || {},
) as ActionSheetOption;
showActionSheet({
...options,
success: (res) => {
if (onActionItemTap) {
const tapIndex = res.tapIndex;
onActionItemTap(
tapIndex,
options.itemList.find((v, i) => i === tapIndex),
);
}
resolve(res);
},
fail: reject,
}).catch(reject);
);
if (!options.itemList) {
reject({ errMsg: 'showActionSheet: fail' });
} else {
showActionSheet({
...(options as ActionSheetOption),
success: (res) => {
if (onActionItemTap) {
const tapIndex = res.tapIndex;
onActionItemTap(
tapIndex,
(options as ActionSheetOption).itemList.find(
(v, i) => i === tapIndex,
),
);
}
resolve(res);
},
fail: reject,
}).catch(reject);
}
}
} catch (e) {
reject(e);
Expand Down
26 changes: 16 additions & 10 deletions packages/hooks/src/useLoading/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,21 @@ export interface LoadingOption {
}

export type ShowLoading = (
option?: LoadingOption,
option?: Partial<LoadingOption>,
) => Promise<General.CallbackResult>;
export type HideLoading = () => Promise<General.CallbackResult>;

function useLoading(option?: LoadingOption): [ShowLoading, HideLoading] {
const initialOption = useRef<LoadingOption>();
function useLoading(
option?: Partial<LoadingOption>,
): [ShowLoading, HideLoading] {
const initialOption = useRef<Partial<LoadingOption>>();

useEffect(() => {
initialOption.current = option;
}, [option]);

const showLoadingAsync = useCallback<ShowLoading>(
(option?: LoadingOption) => {
(option?: Partial<LoadingOption>) => {
return new Promise((resolve, reject) => {
try {
if (!option && !initialOption.current) {
Expand All @@ -30,12 +32,16 @@ function useLoading(option?: LoadingOption): [ShowLoading, HideLoading] {
{},
initialOption.current || {},
option || {},
) as LoadingOption;
showLoading({
...options,
success: resolve,
fail: reject,
}).catch(reject);
);
if (!options.title) {
reject({ errMsg: 'showLoading: fail' });
} else {
showLoading({
...(options as LoadingOption),
success: resolve,
fail: reject,
}).catch(reject);
}
}
} catch (e) {
reject(e);
Expand Down
5 changes: 3 additions & 2 deletions packages/hooks/src/useModal/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import { useCallback, useEffect, useRef } from 'react';

export interface ModalOption {
title?: string;
content: string;
content?: string;
mask?: boolean;
cancelColor?: string;
cancelText?: string;
confirmColor?: string;
Expand Down Expand Up @@ -34,7 +35,7 @@ function useModal(option?: ModalOption): [ShowModal] {
{},
initialOption.current || {},
option || {},
) as ModalOption;
);
showModal({
...options,
success: resolve,
Expand Down
26 changes: 15 additions & 11 deletions packages/hooks/src/useToast/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { showToast, hideToast, General } from '@tarojs/taro';
import { useCallback, useEffect, useRef, useState } from 'react';
import { useCallback, useEffect, useRef } from 'react';

export interface ToastOption {
title: string;
Expand All @@ -10,19 +10,19 @@ export interface ToastOption {
}

export type ShowToast = (
option?: ToastOption,
option?: Partial<ToastOption>,
) => Promise<General.CallbackResult>;
export type HideToast = () => Promise<General.CallbackResult>;

function useToast(option?: ToastOption): [ShowToast, HideToast] {
const initialOption = useRef<ToastOption>();
function useToast(option?: Partial<ToastOption>): [ShowToast, HideToast] {
const initialOption = useRef<Partial<ToastOption>>();

useEffect(() => {
initialOption.current = option;
}, [option]);

const showToastAsync = useCallback<ShowToast>(
(option?: ToastOption) => {
(option?: Partial<ToastOption>) => {
return new Promise((resolve, reject) => {
try {
if (!option && !initialOption.current) {
Expand All @@ -33,12 +33,16 @@ function useToast(option?: ToastOption): [ShowToast, HideToast] {
{},
initialOption.current || {},
option || {},
) as ToastOption;
showToast({
...options,
success: resolve,
fail: reject,
}).catch(reject);
);
if (!options.title) {
reject({ errMsg: 'showToast: fail' });
} else {
showToast({
...(options as ToastOption),
success: resolve,
fail: reject,
}).catch(reject);
}
}
} catch (e) {
reject(e);
Expand Down

2 comments on commit 4728379

@vercel
Copy link

@vercel vercel bot commented on 4728379 Sep 15, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

taro-hooks-h5 – ./

taro-hooks-h5-git-main-innocces.vercel.app
taro-hooks-h5-green.vercel.app
taro-hooks-h5-innocces.vercel.app

@vercel
Copy link

@vercel vercel bot commented on 4728379 Sep 15, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

taro-hooks – ./

taro-hooks-git-main-innocces.vercel.app
taro-hooks-innocces.vercel.app
taro-hooks-theta.vercel.app

Please # to comment.