ant-design/site/component/Demo/index.jsx

94 lines
2.6 KiB
React
Raw Normal View History

2016-02-29 14:08:40 +08:00
import React from 'react';
2016-03-07 17:33:38 +08:00
import { Link } from 'react-router';
import classNames from 'classnames';
import Animate from 'rc-animate';
2016-02-29 14:08:40 +08:00
import * as utils from '../utils';
export default class Demo extends React.Component {
2016-03-01 14:53:32 +08:00
constructor(props) {
super(props);
this.state = {
codeExpand: false,
2016-03-01 14:53:32 +08:00
};
}
componentWillReceiveProps(nextProps) {
if (nextProps.expand === undefined) return;
2016-03-01 14:53:32 +08:00
this.setState({
codeExpand: nextProps.expand,
2016-03-01 14:53:32 +08:00
});
}
handleCodeExapnd() {
this.setState({ codeExpand: !this.state.codeExpand });
}
2016-02-29 14:08:40 +08:00
render() {
2016-03-24 17:34:29 +08:00
const { id, className, meta, intro, preview, style, src,
highlightedCode, highlightedStyle, pathname } = this.props;
const codeExpand = this.state.codeExpand;
const codeBoxClass = classNames({
'code-box': true,
[className]: className,
expand: codeExpand,
});
2016-03-07 17:33:38 +08:00
const introChildren = intro.map(utils.objectToComponent.bind(null, pathname));
2016-03-07 15:20:18 +08:00
2016-02-29 14:08:40 +08:00
return (
<section className={codeBoxClass} id={id}>
2016-02-29 14:08:40 +08:00
<section className="code-box-demo">
2016-03-08 15:13:59 +08:00
{
meta.iframe === 'true' ?
<iframe src={src} /> :
preview
}
2016-03-01 17:42:23 +08:00
{
!!style ?
<style dangerouslySetInnerHTML={{ __html: style }} /> :
null
}
2016-02-29 14:08:40 +08:00
</section>
<section className="code-box-meta markdown">
<div className="code-box-title">
2016-03-08 15:13:59 +08:00
<Link to={{ pathname, query: { scrollTo: id } }}>
{ meta.chinese || meta.english }
</Link>
2016-02-29 14:08:40 +08:00
</div>
{ introChildren }
<span className="collapse anticon anticon-circle-o-right"
onClick={this.handleCodeExapnd.bind(this)}
unselectable="none" />
2016-02-29 14:08:40 +08:00
</section>
<Animate
transitionEnter transitionLeave>
{
codeExpand ?
<section key="code">
<div className="highlight">
<pre>
<code className="javascript" dangerouslySetInnerHTML={{
__html: highlightedCode,
}} />
</pre>
</div>
{
style ?
<div key="style" className="highlight">
<pre>
<code className="css" dangerouslySetInnerHTML={{
__html: highlightedStyle,
}} />
</pre>
</div> :
null
}
</section> : <div key="nothing" />
}
</Animate>
2016-02-29 14:08:40 +08:00
</section>
);
}
}