BlueWallet/components/Button.tsx

95 lines
2.4 KiB
TypeScript
Raw Normal View History

2024-03-27 05:13:21 +01:00
import React, { forwardRef } from 'react';
2024-09-27 01:21:47 +02:00
import { ActivityIndicator, StyleProp, StyleSheet, Text, TouchableOpacity, TouchableOpacityProps, View, ViewStyle } from 'react-native';
2024-06-12 18:46:44 +02:00
import { Icon } from '@rneui/themed';
2024-05-20 11:54:13 +02:00
2023-11-15 09:40:22 +01:00
import { useTheme } from './themes';
2024-08-31 00:43:50 +02:00
interface ButtonProps extends TouchableOpacityProps {
2023-11-15 09:40:22 +01:00
backgroundColor?: string;
buttonTextColor?: string;
disabled?: boolean;
testID?: string;
icon?: {
name: string;
type: string;
color: string;
};
title?: string;
style?: StyleProp<ViewStyle>;
onPress?: () => void;
2024-09-27 01:21:47 +02:00
showActivityIndicator?: boolean;
2023-11-15 09:40:22 +01:00
}
2024-08-31 00:43:50 +02:00
export const Button = forwardRef<React.ElementRef<typeof TouchableOpacity>, ButtonProps>((props, ref) => {
2023-11-15 09:40:22 +01:00
const { colors } = useTheme();
let backgroundColor = props.backgroundColor ?? colors.mainColor;
let fontColor = props.buttonTextColor ?? colors.buttonTextColor;
if (props.disabled) {
backgroundColor = colors.buttonDisabledBackgroundColor;
fontColor = colors.buttonDisabledTextColor;
}
const buttonStyle = {
...styles.button,
backgroundColor,
borderColor: props.disabled ? colors.buttonDisabledBackgroundColor : 'transparent',
};
const textStyle = {
...styles.text,
color: fontColor,
};
const buttonView = props.showActivityIndicator ? (
2024-09-27 01:21:47 +02:00
<ActivityIndicator size="small" color={textStyle.color} />
) : (
<>
2024-03-22 01:54:40 +01:00
{props.icon && <Icon name={props.icon.name} type={props.icon.type} color={props.icon.color} />}
{props.title && <Text style={textStyle}>{props.title}</Text>}
</>
2024-03-22 01:54:40 +01:00
);
return props.onPress ? (
2023-11-15 09:40:22 +01:00
<TouchableOpacity
2024-03-27 05:13:21 +01:00
ref={ref}
2023-11-15 09:40:22 +01:00
testID={props.testID}
style={[buttonStyle, props.style, styles.content]}
2023-11-15 09:40:22 +01:00
accessibilityRole="button"
onPress={props.onPress}
disabled={props.disabled}
2024-08-31 00:43:50 +02:00
{...props}
2023-11-15 09:40:22 +01:00
>
2024-03-30 02:22:05 +01:00
{buttonView}
2023-11-15 09:40:22 +01:00
</TouchableOpacity>
2024-03-22 01:54:40 +01:00
) : (
<View style={[buttonStyle, props.style, styles.content]}>{buttonView}</View>
2023-11-15 09:40:22 +01:00
);
2024-03-27 05:15:28 +01:00
});
2023-11-15 09:40:22 +01:00
const styles = StyleSheet.create({
button: {
borderWidth: 0.7,
minHeight: 45,
height: 48,
maxHeight: 48,
2023-11-15 09:40:22 +01:00
borderRadius: 25,
justifyContent: 'center',
alignItems: 'center',
paddingHorizontal: 16,
flexGrow: 1,
},
content: {
flexDirection: 'row',
justifyContent: 'center',
alignItems: 'center',
},
text: {
marginHorizontal: 8,
fontSize: 16,
fontWeight: '600',
2023-11-15 09:40:22 +01:00
},
});
export default Button;