-
Notifications
You must be signed in to change notification settings - Fork 0
/
LoadingButton.tsx
82 lines (80 loc) · 2.11 KB
/
LoadingButton.tsx
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import React from 'react';
import {
ImageStyle,
StyleProp,
TextStyle,
View,
ViewStyle,
} from 'react-native';
import { useThemeContext } from '../contexts';
import { getScaleFactor } from '../styles/createScaleFactor';
import type { ButtonStateColor } from '../types';
import Button from './Button';
import Loading from './Loading';
type LoadingButtonProps = {
iconStyle?: StyleProp<ImageStyle>;
disabled?: boolean | undefined;
style?: StyleProp<ViewStyle> | undefined;
color?: Partial<ButtonStateColor> | undefined;
font?: StyleProp<TextStyle> | undefined;
content?: string | undefined;
state: 'loading' | 'stop';
onChangeState?: (currentState: 'loading' | 'stop') => void;
};
export default function LoadingButton({
iconStyle,
disabled,
style,
color,
font,
content,
state,
onChangeState,
}: LoadingButtonProps): JSX.Element {
const { colors } = useThemeContext();
const sf = getScaleFactor();
const [height, setHeight] = React.useState(0);
const [width, setWidth] = React.useState(0);
let iconSize = sf(28);
if (iconStyle && typeof (iconStyle as ImageStyle).height === 'number') {
iconSize = (iconStyle as ImageStyle).height as number;
}
return (
<View>
<Button
onLayout={(event) => {
setHeight(sf(event.nativeEvent.layout.height));
setWidth(sf(event.nativeEvent.layout.width));
}}
style={style}
disabled={state === 'loading' ? true : disabled}
onPress={() => {
onChangeState?.(state);
}}
color={{
...color,
disabled:
state === 'loading'
? colors.button.enabled
: colors.button.disabled,
}}
font={font}
children={state === 'loading' ? '' : content}
/>
{state === 'loading' ? (
<Loading
style={[
{
position: 'absolute',
left: width / 2 - iconSize / 2,
top: height / 2 - iconSize / 2,
},
iconStyle,
]}
color="white"
size={iconSize}
/>
) : null}
</View>
);
}