Skip to content

Commit

Permalink
feat: add new texwithbadge result component
Browse files Browse the repository at this point in the history
  • Loading branch information
ffeliperocha committed Jun 10, 2024
1 parent b27faea commit 487b2f0
Showing 1 changed file with 74 additions and 0 deletions.
74 changes: 74 additions & 0 deletions packages/yoga/src/Result/native/Result/TextWithBadge.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import React, { useCallback, useState } from 'react';
import { useWindowDimensions } from 'react-native';
import { string, number, node } from 'prop-types';

import Text from '../../../Text';
import Box from '../../../Box';
import Badge from '../Badge';

const SCREEN_PADDINGS = 20;
const CONTENT_MARGINS = 20;
const AVATAR_CONTENT_MARGINS = 16;
const BADGE_LIMIT = 20;

const TextWithBadge = ({ avatarWidth, badgeIcon, title }) => {
const [textSize, setTextSize] = useState(0);
const { width: windowWidth } = useWindowDimensions();

const textMaxSize =
windowWidth -
(SCREEN_PADDINGS + CONTENT_MARGINS + AVATAR_CONTENT_MARGINS + avatarWidth);
const shouldTruncate = textSize >= textMaxSize - BADGE_LIMIT;
const containerWidth = shouldTruncate ? null : textSize;
const textWidth = shouldTruncate ? '100%' : null;

const onTextLayout = useCallback(event => {
const { width } = event.nativeEvent.layout;

setTextSize(width);
}, []);

return (
<Box
flexDirection="row"
alignItems="center"
justifyContent="flex-end"
position="relative"
width={containerWidth}
>
<Text.Body1
onLayout={onTextLayout}
bold
position="absolute"
left={0}
numberOfLines={1}
paddingRight="medium"
flex={1}
width={textWidth}
>
{title}
</Text.Body1>
<Badge
icon={badgeIcon}
fill="text.primary"
ml="xxxsmall"
bg="neon"
justifyContent="center"
alignItems="center"
borderRadius="circle"
w="small"
h="small"
/>
</Box>
);
};

TextWithBadge.propTypes = {
avatarWidth: number.isRequired,
badgeIcon: node.isRequired,
title: string.isRequired,
};

TextWithBadge.defaultProps = {};

export default TextWithBadge;

0 comments on commit 487b2f0

Please sign in to comment.