-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExample.js
More file actions
46 lines (38 loc) · 1.28 KB
/
Copy pathExample.js
File metadata and controls
46 lines (38 loc) · 1.28 KB
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
import React from 'react';
import PropTypes from 'prop-types';
import CodeExample from './CodeExample';
class Example extends React.Component {
constructor(props) {
super(props);
this.state = { showCode: false };
}
toggleCode = event => {
event.preventDefault();
this.setState(prevState => {
return {showCode: !prevState.showCode};
});
}
render() {
const {showCode} = this.state;
const {code, description, name} = this.props.example;
// Must use COmmonJS require to dynamically require because ES modules must be statically analyzable.
const ExampleComponent = require(`./examples/${this.props.componentName}/${name}`).default;
return (
<div className="example">
{description && <h4>{description}</h4> }
<ExampleComponent />
<p>
<a href="" onClick={this.toggleCode}>
{showCode ? "Hide" : "Show"} Code
</a>
</p>
{showCode && <CodeExample>{code}</CodeExample>}
</div>
)
}
}
Example.propTypes = {
example: PropTypes.object.isRequired,
componentName: PropTypes.string.isRequired
}
export default Example;