format.ts
1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import { useCallback, useEffect, useRef } from 'react';
interface IArea {
value: string;
label: string;
children?: IArea[];
}
// 省市区信息平铺
export const getAreaData = (data: IArea[]) => {
let areaData: any = [];
const eachArea = (items: IArea[]) => {
items.map((item, i) => {
areaData.push({
value: item.value,
label: item.label,
});
if (item.children) eachArea(item.children);
});
};
eachArea(data);
return areaData;
};
// 根据code数组和地区信息返回数组地区名称
export const getAreaNamesByCodes = (codes: string[], areaData: IArea[]) => {
let names: any = [];
const eachName = (items: IArea[]) => {
items.map((item, i) => {
if (codes.includes(item.value)) {
names.push(item.label);
}
});
};
eachName(areaData);
return names;
};
export function useDebounce(fn: any, delay: number) {
const { current } = useRef({ fn, timer: null as unknown as NodeJS.Timeout });
useEffect(
function () {
current.fn = fn;
},
[current.fn, fn],
);
return useCallback(
function (...args: any[]) {
if (current.timer) {
clearTimeout(current.timer as unknown as number);
}
current.timer = setTimeout(() => {
current.fn.call(current.fn, ...args);
}, delay);
},
[current.fn, current.timer, delay],
);
}