forked from coldemo/gallery.code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGentleSpin.tsx
More file actions
38 lines (30 loc) · 858 Bytes
/
GentleSpin.tsx
File metadata and controls
38 lines (30 loc) · 858 Bytes
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
import { Spin } from 'antd';
import { SpinProps } from 'antd/lib/spin';
import _ from 'lodash';
import React, { useEffect, useMemo, useState } from 'react';
type UseGentle = <T = any>(v: T) => T;
let useGentle: UseGentle = _value => {
let [value, setValue] = useState(_value);
let debouncedSetValue = useMemo(() => {
return _.debounce(setValue, 1000, {
leading: true,
trailing: true,
});
}, []);
useEffect(() => {
if (_value) {
setValue(_value);
} else {
debouncedSetValue(_value);
}
return () => {
debouncedSetValue.cancel();
};
}, [_value, debouncedSetValue]);
return value;
};
export let GentleSpin: React.FC<SpinProps> = props => {
let { spinning: _spinning, ...rest } = props;
let spinning = useGentle(_spinning); // todo
return <Spin {...{ ...rest, spinning }} />;
};