feat: usage

This commit is contained in:
jialin
2026-04-22 13:16:48 +08:00
committed by jialin
parent b057d52499
commit 7d75714662
37 changed files with 2199 additions and 288 deletions
+18 -4
View File
@@ -12,8 +12,13 @@ import { useEffect, useRef, useState } from 'react';
* @param option.fetchList: (params, extra) => Promise<{ items: ListItem[] }>
* @returns loading, dataList, fetchData, cancelRequest
*/
export function useQueryDataList<ListItem, Params = any>(option: {
export function useQueryDataList<
ListItem,
Params = any,
Response = Array<ListItem>
>(option: {
key: string;
responseType?: 'array' | 'object';
fetchList: (
params: Params,
options?: any
@@ -21,13 +26,21 @@ export function useQueryDataList<ListItem, Params = any>(option: {
getLabel?: (item: ListItem) => string;
getValue?: (item: ListItem) => any;
errorMsg?: string;
debounceWait?: number;
}): {
loading: boolean;
dataList: Array<ListItem & { label: string; value: any }>;
cancelRequest: () => void;
fetchData: (params: Params, extra?: any) => Promise<ListItem[]>;
fetchData: (params: Params, extra?: any) => Promise<Response>;
} {
const { key, fetchList, getLabel, getValue, errorMsg } = option;
const {
key,
fetchList,
getLabel,
getValue,
responseType = 'array',
errorMsg
} = option;
const axiosTokenRef = useRef<CancelTokenSource | null>(null);
const [dataList, setDataList] = useState<
Array<ListItem & { label: string; value: any }>
@@ -54,10 +67,11 @@ export function useQueryDataList<ListItem, Params = any>(option: {
})) || []
);
return res.items || [];
return responseType === 'array' ? res.items || [] : res;
},
{
manual: true,
debounceWait: option.debounceWait || 300,
onSuccess: () => {},
onError: (error) => {
message.error(