forked from mlaursen/react-md
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExampleCard.jsx
69 lines (61 loc) · 1.75 KB
/
ExampleCard.jsx
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
import React from 'react';
import PropTypes from 'prop-types';
import { Card, CardText, SVGIcon, bem } from 'react-md';
import { toCaterpillarCase } from 'utils/strings';
import codeIcon from 'icons/code.svg';
import Markdown from 'components/Markdown';
import Title from './Title';
const ExampleCard = ({ title, description: propDescription, code, children: propChildren, tableCard, ...props }) => {
let markdown = '';
if (code !== null) {
markdown = `
\`\`\`jsx
${code}
\`\`\`
`;
}
const description = (
<Markdown
key="description"
component="div"
markdown={propDescription}
className={bem('examples-page', 'card', 'description', {}, 'md-text-container')}
/>
);
let children = propChildren;
if (tableCard && description) {
children = [
<CardText key="description">{description}</CardText>,
React.cloneElement(children, { key: 'table-card-example' }),
];
} else if (!tableCard) {
children = <CardText key="example-card-text">{description}{children}</CardText>;
}
const id = toCaterpillarCase(title.replace(/["'()'".:]/g, ''));
return (
<Card
id={id}
{...props}
tabIndex={-1}
tableCard={tableCard}
expanderIcon={<SVGIcon use={codeIcon.url} />}
expanderTooltipLabel="View the source for this example."
expanderTooltipDelay={300}
>
<Title id={id} title={title} expander={!!code} />
<CardText expandable>
<Markdown markdown={markdown} />
</CardText>
{children}
</Card>
);
};
ExampleCard.propTypes = {
title: PropTypes.string.isRequired,
description: PropTypes.string,
code: PropTypes.string,
children: PropTypes.node,
tableCard: PropTypes.bool,
className: PropTypes.string,
};
export default ExampleCard;