|
@@ -1,8 +1,14 @@
|
|
-import {getAllRoleList, getAllStorage, getAllUser, getDictionaryOptions} from '@apis';
|
|
|
|
|
|
+import {
|
|
|
|
+ getAllRoleList,
|
|
|
|
+ getAllStorage,
|
|
|
|
+ getAllUser,
|
|
|
|
+ getDictionaryInfo,
|
|
|
|
+ getDictionaryOptions,
|
|
|
|
+} from '@apis';
|
|
import {BaseResult, DictionaryData, DictionaryParamsType, StorageListData} from '@models';
|
|
import {BaseResult, DictionaryData, DictionaryParamsType, StorageListData} from '@models';
|
|
import {useQuery} from '@tanstack/react-query';
|
|
import {useQuery} from '@tanstack/react-query';
|
|
import {useLatest} from 'ahooks';
|
|
import {useLatest} from 'ahooks';
|
|
-import {useMemo, useState} from 'react';
|
|
|
|
|
|
+import {useCallback, useMemo, useState} from 'react';
|
|
import {debounce} from 'lodash-es';
|
|
import {debounce} from 'lodash-es';
|
|
|
|
|
|
function useQueryOptions<T extends {id: number | string}>(options: {
|
|
function useQueryOptions<T extends {id: number | string}>(options: {
|
|
@@ -153,3 +159,31 @@ export function useDictionarySelect(
|
|
|
|
|
|
return [data, onSearch] as const;
|
|
return [data, onSearch] as const;
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+export function useDictionaryBlur(type: DictionaryParamsType, code: () => string) {
|
|
|
|
+ const [value, setValue] = useState('');
|
|
|
|
+ const getCode = useLatest(code);
|
|
|
|
+
|
|
|
|
+ const onBlur = useCallback(
|
|
|
|
+ function () {
|
|
|
|
+ setValue(getCode.current?.());
|
|
|
|
+ },
|
|
|
|
+ [getCode],
|
|
|
|
+ );
|
|
|
|
+
|
|
|
|
+ const {data} = useQuery(
|
|
|
|
+ [getDictionaryInfo.name, value, 'useDictionaryBlur'],
|
|
|
|
+ async function () {
|
|
|
|
+ const data = await getDictionaryInfo(type, '', value);
|
|
|
|
+
|
|
|
|
+ if (data.msg === '200') {
|
|
|
|
+ return data.data.list.length ? data.data.list[0].name : '';
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return '';
|
|
|
|
+ },
|
|
|
|
+ {initialData: '', enabled: Boolean(value)},
|
|
|
|
+ );
|
|
|
|
+
|
|
|
|
+ return [data, onBlur] as const;
|
|
|
|
+}
|