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

112 lines
3.2 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';
2016-03-01 14:53:32 +08:00
import classNames from 'classnames';
2016-03-21 16:49:59 +08:00
import { Row, Col, Icon, Affix } from '../../../';
2016-02-29 14:08:40 +08:00
import Demo from '../Demo';
import * as utils from '../utils';
2016-03-22 11:32:49 +08:00
import demosList from '../../../_data/demos-list';
2016-02-29 14:08:40 +08:00
export default class ComponentDoc extends React.Component {
2016-03-01 14:53:32 +08:00
constructor(props) {
super(props);
this.state = {
expandAll: false,
};
}
2016-03-23 14:15:00 +08:00
componentDidMount() {
this.componentDidUpdate();
}
componentDidUpdate() {
const { chinese, english } = this.props.doc.meta;
utils.setTitle(`${chinese} ${english} - Ant Design`);
}
2016-03-01 14:53:32 +08:00
handleExpandToggle() {
this.setState({
expandAll: !this.state.expandAll,
});
}
2016-02-29 14:08:40 +08:00
render() {
2016-03-07 17:33:38 +08:00
const { doc, location } = this.props;
2016-03-24 17:34:29 +08:00
const scrollTo = location.query.scrollTo;
2016-03-07 15:20:18 +08:00
const { description, meta } = doc;
const demos = demosList[meta.fileName] || [];
2016-03-01 14:53:32 +08:00
const expand = this.state.expandAll;
2016-02-29 14:08:40 +08:00
2016-03-07 15:20:18 +08:00
const isSingleCol = meta.cols === '1';
2016-02-29 14:08:40 +08:00
const leftChildren = [];
const rightChildren = [];
2016-03-07 14:31:47 +08:00
demos.sort((a, b) => {
2016-03-08 15:13:59 +08:00
return parseInt(a.meta.order, 10) - parseInt(b.meta.order, 10);
2016-03-07 14:31:47 +08:00
}).forEach((demoData, index) => {
2016-02-29 15:56:15 +08:00
if (index % 2 === 0 || isSingleCol) {
2016-03-07 17:33:38 +08:00
leftChildren.push(
2016-03-24 17:34:29 +08:00
<Demo {...demoData} className={scrollTo === demoData.id ? 'code-box-target' : ''}
key={index}
expand={expand} pathname={location.pathname} />
2016-03-07 17:33:38 +08:00
);
2016-02-29 14:08:40 +08:00
} else {
2016-03-07 17:33:38 +08:00
rightChildren.push(
2016-03-24 17:34:29 +08:00
<Demo {...demoData} className={scrollTo === demoData.id ? 'code-box-target' : ''}
key={index}
expand={expand} pathname={location.pathname} />
2016-03-07 17:33:38 +08:00
);
2016-02-29 14:08:40 +08:00
}
});
2016-03-01 14:53:32 +08:00
const expandTriggerClass = classNames({
'code-box-expand-trigger': true,
'code-box-expand-trigger-active': expand,
});
2016-02-29 14:08:40 +08:00
2016-03-07 15:20:18 +08:00
const jumper = demos.map((demo) => {
return (
<li key={demo.id}>
2016-03-24 17:34:29 +08:00
<Link className={ demo.id === scrollTo ? 'current' : ''}
to={{ pathname: location.pathname, query: { scrollTo: `${demo.id}` } }}>
2016-03-08 15:13:59 +08:00
{ demo.meta.chinese || demo.meta.english }
2016-03-07 17:33:38 +08:00
</Link>
2016-03-07 15:20:18 +08:00
</li>
);
});
2016-02-29 14:08:40 +08:00
return (
2016-03-01 11:46:18 +08:00
<article>
2016-03-09 15:09:10 +08:00
<Affix className="toc-affix">
<ul className="toc demos-anchor">
{ jumper }
</ul>
</Affix>
2016-02-29 15:56:15 +08:00
<section className="markdown">
2016-03-07 15:20:18 +08:00
<h1>{meta.chinese || meta.english}</h1>
2016-03-07 17:33:38 +08:00
{ description.map(utils.objectToComponent.bind(null, location.pathname)) }
2016-03-01 14:53:32 +08:00
<h2>
代码演示
<Icon type="appstore" className={expandTriggerClass}
title="展开全部代码" onClick={this.handleExpandToggle.bind(this)} />
</h2>
2016-02-29 15:56:15 +08:00
</section>
2016-02-29 14:08:40 +08:00
<Row>
2016-03-15 17:40:31 +08:00
<Col span={ isSingleCol ? '24' : '12' }
className={ isSingleCol ?
'code-boxes-col-1-1' :
'code-boxes-col-2-1'
}
>
2016-03-07 14:31:47 +08:00
{ leftChildren }
</Col>
2016-03-15 17:40:31 +08:00
{
isSingleCol ? null :
<Col className="code-boxes-col-2-1" span="12">{ rightChildren }</Col>
}
2016-02-29 14:08:40 +08:00
</Row>
2016-02-29 15:56:15 +08:00
<section className="markdown">
2016-03-07 17:33:38 +08:00
{ (doc.api || []).map(utils.objectToComponent.bind(null, location.pathname)) }
2016-02-29 15:56:15 +08:00
</section>
2016-03-01 11:46:18 +08:00
</article>
2016-02-29 14:08:40 +08:00
);
}
}