@holper/react-native-holper-storybook 0.6.37 → 0.6.38
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/components/Button/index.js +35 -12
- package/lib/hooks/index.js +1 -0
- package/lib/hooks/useDebounce.js +22 -0
- package/package.json +1 -1
|
@@ -1,12 +1,25 @@
|
|
|
1
1
|
import React from 'react';
|
|
2
2
|
import PropTypes from 'prop-types';
|
|
3
|
-
import {TouchableOpacity, ActivityIndicator} from 'react-native';
|
|
4
|
-
import {ViewPropTypes} from 'deprecated-react-native-prop-types';
|
|
3
|
+
import { TouchableOpacity, ActivityIndicator } from 'react-native';
|
|
4
|
+
import { ViewPropTypes } from 'deprecated-react-native-prop-types';
|
|
5
5
|
import withPreventDoubleClick from './../PreventDoubleClick';
|
|
6
|
-
import {Colors} from '../../configs/constants';
|
|
6
|
+
import { Colors } from '../../configs/constants';
|
|
7
|
+
import { useDebounce } from '../../hooks';
|
|
7
8
|
import style from './style';
|
|
8
9
|
|
|
9
|
-
const Button = ({
|
|
10
|
+
const Button = ({
|
|
11
|
+
onPress,
|
|
12
|
+
disabled,
|
|
13
|
+
isLoading,
|
|
14
|
+
bordered,
|
|
15
|
+
variant,
|
|
16
|
+
size,
|
|
17
|
+
noShadow,
|
|
18
|
+
style: customStyle,
|
|
19
|
+
debounceDelay,
|
|
20
|
+
children,
|
|
21
|
+
}) => {
|
|
22
|
+
const handleClick = useDebounce(onPress, debounceDelay);
|
|
10
23
|
const getSpinnerColor = () => {
|
|
11
24
|
switch (variant) {
|
|
12
25
|
case 'primary':
|
|
@@ -27,15 +40,16 @@ const Button = ({onPress, disabled, isLoading, bordered, variant, size, noShadow
|
|
|
27
40
|
bordered ? style.bordered : {},
|
|
28
41
|
disabled ? style.disabled : {},
|
|
29
42
|
noShadow ? style.noShadow : {},
|
|
30
|
-
customStyle
|
|
43
|
+
customStyle,
|
|
31
44
|
]}
|
|
32
45
|
disabled={isLoading || disabled}
|
|
33
|
-
onPress={
|
|
46
|
+
onPress={handleClick}
|
|
34
47
|
>
|
|
35
|
-
{isLoading ?
|
|
36
|
-
<ActivityIndicator color={getSpinnerColor()} size={28}/>
|
|
48
|
+
{isLoading ? (
|
|
49
|
+
<ActivityIndicator color={getSpinnerColor()} size={28} />
|
|
50
|
+
) : (
|
|
37
51
|
children
|
|
38
|
-
}
|
|
52
|
+
)}
|
|
39
53
|
</TouchableOpacity>
|
|
40
54
|
);
|
|
41
55
|
};
|
|
@@ -49,8 +63,8 @@ Button.defaultProps = {
|
|
|
49
63
|
variant: 'primary',
|
|
50
64
|
size: 'medium',
|
|
51
65
|
style: {},
|
|
52
|
-
onPress: () => {
|
|
53
|
-
|
|
66
|
+
onPress: () => {},
|
|
67
|
+
debounceDelay: 0,
|
|
54
68
|
};
|
|
55
69
|
|
|
56
70
|
Button.propTypes = {
|
|
@@ -59,10 +73,19 @@ Button.propTypes = {
|
|
|
59
73
|
isLoading: PropTypes.bool,
|
|
60
74
|
bordered: PropTypes.bool,
|
|
61
75
|
noShadow: PropTypes.bool,
|
|
62
|
-
variant: PropTypes.oneOf([
|
|
76
|
+
variant: PropTypes.oneOf([
|
|
77
|
+
'primary',
|
|
78
|
+
'secondary',
|
|
79
|
+
'dim',
|
|
80
|
+
'light',
|
|
81
|
+
'inverted',
|
|
82
|
+
'error',
|
|
83
|
+
'highlight',
|
|
84
|
+
]),
|
|
63
85
|
size: PropTypes.oneOf(['tiny', 'small', 'medium', 'fit', 'icon']),
|
|
64
86
|
style: ViewPropTypes.style,
|
|
65
87
|
onPress: PropTypes.func,
|
|
88
|
+
debounceDelay: PropTypes.number,
|
|
66
89
|
};
|
|
67
90
|
|
|
68
91
|
export default withPreventDoubleClick(Button);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { default as useDebounce } from './useDebounce';
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { useEffect, useRef } from 'react';
|
|
2
|
+
|
|
3
|
+
const useDebounce = (callback, delay) => {
|
|
4
|
+
const latestCallback = useRef();
|
|
5
|
+
const latestTimeout = useRef();
|
|
6
|
+
|
|
7
|
+
useEffect(() => {
|
|
8
|
+
latestCallback.current = callback;
|
|
9
|
+
}, [callback]);
|
|
10
|
+
|
|
11
|
+
return () => {
|
|
12
|
+
if (latestTimeout.current) {
|
|
13
|
+
clearTimeout(latestTimeout.current);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
latestTimeout.current = setTimeout(() => {
|
|
17
|
+
latestCallback.current();
|
|
18
|
+
}, delay);
|
|
19
|
+
};
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
export default useDebounce;
|