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