Skip to content

Commit

Permalink
Refactor TruncatedText & introduce TruncatedMultiLinesText components
Browse files Browse the repository at this point in the history
  • Loading branch information
ThibautBremand committed Aug 13, 2023
1 parent 6611661 commit 2721b72
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { ComponentProps } from 'react';

import Typography from '@mui/material/Typography';

type TruncatedMultiLinesTextProps = ComponentProps<typeof Typography> & {
text?: string;
maxLines?: string;
};

export const TruncatedMultiLinesText = ({
text = '',
maxLines = '4',
sx,
variant = 'body1'
}: TruncatedMultiLinesTextProps) => {
return (
<Typography
sx={{
overflow: 'hidden',
display: '-webkit-box',
width: '100%',
'-webkit-box-orient': 'vertical',
'-webkit-line-clamp': maxLines /* start showing ellipsis when X line is reached */,
'white-space': 'pre-wrap' /* let the text wrap preserving spaces */,
...sx
}}
variant={variant}
>
{text}
</Typography>
);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './TruncatedMultiLinesText';
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
import { ComponentProps, FC } from 'react';
import { ComponentProps } from 'react';

import { Typography } from '@mui/material';

type TruncatedTextProps = ComponentProps<typeof Typography> & {
text?: string;
maxLength?: number;
};

export const TruncatedText: FC<TruncatedTextProps> = ({ text, maxLength, sx, ...rest }) => {
if (!text) return null;

const displayedText =
maxLength && text.length > maxLength ? `${text.substring(0, maxLength - 3)}...` : text;

export const TruncatedText = ({
text = '',
sx,
variant = 'body1',
...rest
}: TruncatedTextProps) => {
return (
<Typography
sx={{
Expand All @@ -23,9 +22,9 @@ export const TruncatedText: FC<TruncatedTextProps> = ({ text, maxLength, sx, ...
...sx
}}
{...rest}
variant="body1"
variant={variant}
>
{displayedText}
{text}
</Typography>
);
};
1 change: 1 addition & 0 deletions packages/extension/src/components/atoms/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ export * from './PrivateRoute';
export * from './TileLoader';
export * from './TokenLoader';
export * from './Tokens';
export * from './TruncatedMultiLinesText';
export * from './TruncatedText';
export * from './WalletIcon';
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const mockNFT = {
Issuer: 'rDGh681kc6V1GKkQB378XhiM1tzkYrnFwQ',
NFTokenID: '000B0000867AD7165A812436FBFA175555413C26162BCF380000099A00000000',
NFTokenTaxon: 1,
URI: '697066733A2F2F6261667962656965356A626736336A6164676979736337796B6B6A64737170777732746A6D786D7935377077716F6A697666356562366B6D6D79612F312E6A736F6E',
URI: '697066733A2F2F6261667962656965356A626736336A6164676979736337796B6B6A64737170777732746A6D786D7935377077716F6A697666356562366B6D6D79612F312E6A736F6E', // Hex: ipfs://bafybeie5jbg63jadgiysc7ykkjdsqpww2tjmxmy57pwqojivf5eb6kmmya/1.json
nft_serial: 0
};

Expand Down Expand Up @@ -54,7 +54,7 @@ const renderNFTCard = (props: NFTCardProps) => {

describe('NFTCard', () => {
test('renders NFTCard component correctly', async () => {
mockGetNFTData.mockReturnValue({
mockGetNFTData.mockReturnValueOnce({
...mockNFTData
});

Expand All @@ -74,7 +74,9 @@ describe('NFTCard', () => {

await waitFor(() => expect(mockGetNFTData).toHaveBeenCalled());

// Check that the NFT data is not displayed when an error occurs
expect(screen.queryByTestId('nft_name')).not.toBeInTheDocument();
// Check that the NFT data description is set to convertHexToString(URI) when an error occurs
expect(
screen.getByText('ipfs://bafybeie5jbg63jadgiysc7ykkjdsqpww2tjmxmy57pwqojivf5eb6kmmya/1.json')
).toBeInTheDocument();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ import { convertHexToString } from 'xrpl';
import { AccountNFToken, NFTData } from '@gemwallet/constants';

import { useLedger } from '../../../contexts';
import { NFTImage } from '../../atoms';
import { TruncatedText } from '../../atoms';
import { NFTImage, TruncatedText, TruncatedMultiLinesText } from '../../atoms';
import { NFTDetails } from '../../organisms';

export interface NFTCardProps {
Expand Down Expand Up @@ -108,10 +107,9 @@ export const NFTCard: FC<NFTCardProps> = ({ NFT }) => {
sx={{ fontSize: '16px', color: 'white', marginTop: '10px' }}
data-testid="nft_name"
/>
<TruncatedText
<TruncatedMultiLinesText
text={NFTData.description}
sx={{ fontSize: '14px', color: 'grey', marginTop: '10px' }}
maxLength={200}
/>
<Button
variant="outlined"
Expand Down

0 comments on commit 2721b72

Please sign in to comment.