boxons/components/shared/Card.tsx

81 lines
1.9 KiB
TypeScript
Raw Permalink Normal View History

2024-10-13 11:29:36 +00:00
import styled from '@emotion/native';
import React, { useCallback } from 'react';
import { TouchableOpacity } from 'react-native';
2025-04-08 21:29:14 +00:00
export interface CardProps {
2024-10-13 11:29:36 +00:00
children: React.ReactNode;
backgroundColor: CardBackgroundColor;
2025-04-10 08:01:27 +00:00
borderColor: CardBorderColor;
color: CardColor;
2024-10-13 11:29:36 +00:00
testID?: string;
onPress?: () => void;
}
2025-04-10 08:01:27 +00:00
type CardBackgroundColor = 'red' | 'green' | 'grey' | 'black' | 'white' | 'darkGrey';
type CardBorderColor = 'pink' | 'blue' | 'grey' | 'black' | 'white' | 'darkGrey';
type CardColor = 'black' | 'white';
2024-10-13 11:29:36 +00:00
const Card: React.FC<CardProps> = ({
children,
backgroundColor = 'red',
2025-04-10 08:01:27 +00:00
color = 'black',
borderColor = 'pink',
2024-10-13 11:29:36 +00:00
testID = undefined,
onPress
}) => {
const safeOnPress = useCallback(() => onPress?.(), [onPress]);
const content = (
2025-04-10 08:01:27 +00:00
<Container
color={color}
bgColor={backgroundColor}
testID={testID}
borderColor={borderColor}
>
2024-10-13 11:29:36 +00:00
{children}
</Container>
);
const touchableContent = (
2025-04-10 08:01:27 +00:00
<CustomTouchableOpacity
onPress={safeOnPress}
bgColor={backgroundColor}
color={color}
borderColor={borderColor}
>
2024-10-13 11:29:36 +00:00
{content}
2025-04-10 08:01:27 +00:00
</CustomTouchableOpacity>
2024-10-13 11:29:36 +00:00
)
return (
onPress ? touchableContent : content
);
};
2025-04-10 08:01:27 +00:00
const CustomTouchableOpacity = styled(TouchableOpacity)<{
color: CardColor;
borderColor: CardBorderColor;
bgColor: CardBackgroundColor;
}>(({ theme, color, borderColor, bgColor }) => ({
color: theme.colors.fixed[color],
borderColor: theme.colors.fixed[borderColor],
backgroundColor: theme.colors.fixed[bgColor]
}));
2024-10-13 11:29:36 +00:00
const Container = styled.View<{
bgColor: CardBackgroundColor;
2025-04-10 08:01:27 +00:00
borderColor: CardBorderColor;
color: CardColor;
}>(({ theme, bgColor, borderColor, color }) => ({
padding: 25,
2024-10-13 11:29:36 +00:00
width: '100%',
2025-04-10 08:01:27 +00:00
borderRadius: 0,
2024-10-13 11:29:36 +00:00
overflow: 'hidden',
2025-04-10 08:01:27 +00:00
backgroundColor: theme.colors.fixed[bgColor],
borderWidth: 4,
borderColor: theme.colors.fixed[borderColor],
color: theme.colors.fixed[color]
2024-10-13 11:29:36 +00:00
}));
export default Card;