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

124 lines
3.5 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-04-11 10:12:48 +08:00
import { Row, Col, Icon, Affix } from 'antd';
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-30 16:16:18 +08:00
handleExpandToggle = () => {
2016-03-01 14:53:32 +08:00
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;
2016-04-05 14:19:58 +08:00
const demos = (demosList[meta.fileName] || [])
.filter((demoData) => !demoData.meta.hidden);
2016-03-01 14:53:32 +08:00
const expand = this.state.expandAll;
2016-02-29 14:08:40 +08:00
2016-03-31 15:22:38 +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}>
<Link className={demo.id === scrollTo ? 'current' : ''}
2016-03-24 17:34:29 +08:00
to={{ pathname: location.pathname, query: { scrollTo: `${demo.id}` } }}>
{demo.meta.title}
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-04-18 13:43:33 +08:00
<Affix className="toc-affix" offsetTop={16}>
2016-03-09 15:09:10 +08:00
<ul className="toc demos-anchor">
{jumper}
2016-03-09 15:09:10 +08:00
</ul>
</Affix>
2016-02-29 15:56:15 +08:00
<section className="markdown">
2016-04-18 16:59:04 +08:00
<h1>{meta.english} {meta.chinese}</h1>
2016-04-01 16:52:47 +08:00
{
utils.jsonmlToComponent(
location.pathname,
['section', { className: 'markdown' }]
.concat(description)
)
}
2016-03-01 14:53:32 +08:00
<h2>
代码演示
<Icon type="appstore" className={expandTriggerClass}
2016-03-30 16:16:18 +08:00
title="展开全部代码" onClick={this.handleExpandToggle} />
2016-03-01 14:53:32 +08:00
</h2>
2016-02-29 15:56:15 +08:00
</section>
2016-04-18 13:43:33 +08:00
<Row gutter={16}>
<Col span={isSingleCol ? '24' : '12'}
className={isSingleCol ?
2016-03-15 17:40:31 +08:00
'code-boxes-col-1-1' :
'code-boxes-col-2-1'
}
>
{leftChildren}
2016-03-07 14:31:47 +08:00
</Col>
2016-03-15 17:40:31 +08:00
{
isSingleCol ? null :
<Col className="code-boxes-col-2-1" span="12">{rightChildren}</Col>
2016-03-15 17:40:31 +08:00
}
2016-02-29 14:08:40 +08:00
</Row>
2016-04-01 16:52:47 +08:00
{
utils.jsonmlToComponent(
location.pathname,
['section', {
className: 'markdown api-container',
}].concat(doc.api || [])
)
}
2016-03-01 11:46:18 +08:00
</article>
2016-02-29 14:08:40 +08:00
);
}
}