Skip to content

Commit d4472bf

Browse files
authored
Converts Card to typescript (#1160)
1 parent 43848d8 commit d4472bf

File tree

3 files changed

+98
-111
lines changed

3 files changed

+98
-111
lines changed

src/Card/Card.jsx

Lines changed: 0 additions & 111 deletions
This file was deleted.

src/Card/Card.tsx

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
import React, {
2+
createElement,
3+
type DetailedHTMLProps,
4+
type HTMLAttributes,
5+
type ReactNode,
6+
} from 'react';
7+
import classNames from 'classnames';
8+
9+
import { LoadingSkeleton } from 'src/LoadingSkeleton';
10+
11+
import './Card.scss';
12+
13+
export const CardSizes = {
14+
EXTRA_SMALL: 'xs',
15+
SMALL: 'sm',
16+
MEDIUM: 'md',
17+
LARGE: 'lg',
18+
} as const;
19+
20+
type ElementProps = DetailedHTMLProps<HTMLAttributes<HTMLElement>, HTMLElement>;
21+
22+
type CardProps = {
23+
children?: ReactNode;
24+
className?: string;
25+
divided?: boolean;
26+
elementType?: string;
27+
helperText?: ReactNode;
28+
isLoading?: boolean;
29+
loadingSkeleton?: ReactNode;
30+
loadingSkeletonParagraphCount?: number;
31+
noPadding?: boolean;
32+
size: 'xs' | 'sm' | 'md' | 'lg';
33+
subTitle?: ReactNode;
34+
title?: ReactNode;
35+
} & ElementProps;
36+
37+
const Card = ({
38+
children,
39+
className,
40+
divided = false,
41+
elementType = 'section',
42+
helperText,
43+
isLoading = false,
44+
loadingSkeleton,
45+
loadingSkeletonParagraphCount = 1,
46+
noPadding = false,
47+
size,
48+
subTitle,
49+
title,
50+
...props
51+
}: CardProps) => {
52+
const defaultLoadingSkeleton = (
53+
<>
54+
<LoadingSkeleton height={24} width="33%" />
55+
<br />
56+
{Array(loadingSkeletonParagraphCount).fill(0).map((_, i) => (
57+
// eslint-disable-next-line react/no-array-index-key
58+
<div className="Card__loading-skeleton-paragraphs" key={`skeleton-paragraph-${i}`}>
59+
<LoadingSkeleton count={3} />
60+
</div>
61+
))}
62+
</>
63+
);
64+
65+
return createElement(
66+
elementType,
67+
{
68+
...props,
69+
className: classNames(
70+
'Card',
71+
{ [`Card--${size}`]: size },
72+
className,
73+
{
74+
'Card--divided': divided,
75+
'Card--no-padding': noPadding,
76+
},
77+
),
78+
},
79+
<>
80+
{isLoading ? (loadingSkeleton || defaultLoadingSkeleton) : (
81+
<>
82+
{title && (
83+
<div className="Card__header">
84+
<h2 className="Card__title">{title}</h2>
85+
{helperText && <span className="Card__helper-text">{helperText}</span>}
86+
</div>
87+
)}
88+
89+
{divided && <hr className="Card__divider" /> }
90+
{subTitle && <h3 className="Card__subtitle">{subTitle}</h3> }
91+
{children}
92+
</>
93+
)}
94+
</>,
95+
);
96+
};
97+
98+
export default Card;
File renamed without changes.

0 commit comments

Comments
 (0)