2016-05-27 11:48:08 +08:00
|
|
|
import React from 'react';
|
2017-04-12 04:49:08 +08:00
|
|
|
import PropTypes from 'prop-types';
|
2016-05-27 11:48:08 +08:00
|
|
|
import DocumentTitle from 'react-document-title';
|
2016-09-08 16:53:50 +08:00
|
|
|
import { FormattedMessage } from 'react-intl';
|
2016-05-27 11:48:08 +08:00
|
|
|
import classNames from 'classnames';
|
2018-12-07 16:17:45 +08:00
|
|
|
import { Row, Col, Icon, Affix, Tooltip } from 'antd';
|
2016-05-27 11:48:08 +08:00
|
|
|
import { getChildren } from 'jsonml.js/lib/utils';
|
|
|
|
import Demo from './Demo';
|
2016-08-17 12:06:38 +08:00
|
|
|
import EditButton from './EditButton';
|
2016-05-27 11:48:08 +08:00
|
|
|
|
|
|
|
export default class ComponentDoc extends React.Component {
|
|
|
|
static contextTypes = {
|
2017-04-12 04:49:08 +08:00
|
|
|
intl: PropTypes.object,
|
2018-12-07 16:17:45 +08:00
|
|
|
};
|
2016-05-27 11:48:08 +08:00
|
|
|
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
|
|
|
|
this.state = {
|
|
|
|
expandAll: false,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
handleExpandToggle = () => {
|
2018-06-22 21:05:13 +08:00
|
|
|
const { expandAll } = this.state;
|
2016-05-27 11:48:08 +08:00
|
|
|
this.setState({
|
2018-06-22 21:05:13 +08:00
|
|
|
expandAll: !expandAll,
|
2016-05-27 11:48:08 +08:00
|
|
|
});
|
2018-12-07 16:17:45 +08:00
|
|
|
};
|
2016-05-27 11:48:08 +08:00
|
|
|
|
|
|
|
render() {
|
2017-10-09 13:23:20 +08:00
|
|
|
const { props } = this;
|
2016-05-27 11:48:08 +08:00
|
|
|
const { doc, location } = props;
|
|
|
|
const { content, meta } = doc;
|
2018-12-07 16:17:45 +08:00
|
|
|
const {
|
|
|
|
intl: { locale },
|
|
|
|
} = this.context;
|
2016-10-19 17:39:25 +08:00
|
|
|
const demos = Object.keys(props.demos).map(key => props.demos[key]);
|
2018-07-01 12:13:46 +08:00
|
|
|
const { expandAll } = this.state;
|
2016-05-27 11:48:08 +08:00
|
|
|
|
|
|
|
const isSingleCol = meta.cols === 1;
|
|
|
|
const leftChildren = [];
|
|
|
|
const rightChildren = [];
|
2018-06-22 21:05:13 +08:00
|
|
|
const showedDemo = demos.some(demo => demo.meta.only)
|
2018-12-07 16:17:45 +08:00
|
|
|
? demos.filter(demo => demo.meta.only)
|
|
|
|
: demos.filter(demo => demo.preview);
|
|
|
|
showedDemo
|
|
|
|
.sort((a, b) => a.meta.order - b.meta.order)
|
2016-05-27 11:48:08 +08:00
|
|
|
.forEach((demoData, index) => {
|
2017-01-13 14:59:14 +08:00
|
|
|
const demoElem = (
|
|
|
|
<Demo
|
|
|
|
{...demoData}
|
|
|
|
key={demoData.meta.filename}
|
|
|
|
utils={props.utils}
|
2018-07-01 12:13:46 +08:00
|
|
|
expand={expandAll}
|
2017-01-13 14:59:14 +08:00
|
|
|
location={location}
|
|
|
|
/>
|
|
|
|
);
|
2016-05-27 11:48:08 +08:00
|
|
|
if (index % 2 === 0 || isSingleCol) {
|
2017-01-13 14:59:14 +08:00
|
|
|
leftChildren.push(demoElem);
|
2016-05-27 11:48:08 +08:00
|
|
|
} else {
|
2017-01-13 14:59:14 +08:00
|
|
|
rightChildren.push(demoElem);
|
2016-05-27 11:48:08 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
const expandTriggerClass = classNames({
|
|
|
|
'code-box-expand-trigger': true,
|
2018-07-01 12:13:46 +08:00
|
|
|
'code-box-expand-trigger-active': expandAll,
|
2016-05-27 11:48:08 +08:00
|
|
|
});
|
|
|
|
|
2018-12-07 16:17:45 +08:00
|
|
|
const jumper = showedDemo.map(demo => {
|
2017-10-09 13:23:20 +08:00
|
|
|
const { title } = demo.meta;
|
2016-05-27 11:48:08 +08:00
|
|
|
const localizeTitle = title[locale] || title;
|
|
|
|
return (
|
2016-09-15 01:42:45 +08:00
|
|
|
<li key={demo.meta.id} title={localizeTitle}>
|
2018-12-07 16:17:45 +08:00
|
|
|
<a href={`#${demo.meta.id}`}>{localizeTitle}</a>
|
2016-05-27 11:48:08 +08:00
|
|
|
</li>
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2016-09-21 11:28:38 +08:00
|
|
|
const { title, subtitle, filename } = meta;
|
2016-05-27 11:48:08 +08:00
|
|
|
return (
|
2016-09-30 11:51:36 +08:00
|
|
|
<DocumentTitle title={`${subtitle || ''} ${title[locale] || title} - Ant Design`}>
|
2016-05-27 11:48:08 +08:00
|
|
|
<article>
|
|
|
|
<Affix className="toc-affix" offsetTop={16}>
|
2017-11-27 15:36:58 +08:00
|
|
|
<ul id="demo-toc" className="toc">
|
2016-05-27 11:48:08 +08:00
|
|
|
{jumper}
|
|
|
|
</ul>
|
|
|
|
</Affix>
|
|
|
|
<section className="markdown">
|
2016-07-26 19:45:55 +08:00
|
|
|
<h1>
|
2016-09-21 11:28:38 +08:00
|
|
|
{title[locale] || title}
|
2018-12-07 16:17:45 +08:00
|
|
|
{!subtitle ? null : <span className="subtitle">{subtitle}</span>}
|
|
|
|
<EditButton
|
|
|
|
title={<FormattedMessage id="app.content.edit-page" />}
|
|
|
|
filename={filename}
|
|
|
|
/>
|
2016-07-26 19:45:55 +08:00
|
|
|
</h1>
|
2018-12-07 16:17:45 +08:00
|
|
|
{props.utils.toReactComponent(
|
|
|
|
['section', { className: 'markdown' }].concat(getChildren(content)),
|
|
|
|
)}
|
2016-05-27 11:48:08 +08:00
|
|
|
<h2>
|
2016-09-08 16:53:50 +08:00
|
|
|
<FormattedMessage id="app.component.examples" />
|
2018-07-01 12:13:46 +08:00
|
|
|
<Tooltip
|
2018-12-07 16:17:45 +08:00
|
|
|
title={
|
|
|
|
<FormattedMessage
|
|
|
|
id={`app.component.examples.${expandAll ? 'collpse' : 'expand'}`}
|
|
|
|
/>
|
|
|
|
}
|
2018-07-01 12:13:46 +08:00
|
|
|
>
|
|
|
|
<Icon
|
|
|
|
type={`${expandAll ? 'appstore' : 'appstore-o'}`}
|
|
|
|
className={expandTriggerClass}
|
|
|
|
onClick={this.handleExpandToggle}
|
|
|
|
/>
|
|
|
|
</Tooltip>
|
2016-05-27 11:48:08 +08:00
|
|
|
</h2>
|
|
|
|
</section>
|
|
|
|
<Row gutter={16}>
|
2018-11-28 15:00:03 +08:00
|
|
|
<Col
|
|
|
|
span={isSingleCol ? '24' : '12'}
|
2018-12-07 16:17:45 +08:00
|
|
|
className={isSingleCol ? 'code-boxes-col-1-1' : 'code-boxes-col-2-1'}
|
2016-05-27 11:48:08 +08:00
|
|
|
>
|
|
|
|
{leftChildren}
|
|
|
|
</Col>
|
2018-12-07 16:17:45 +08:00
|
|
|
{isSingleCol ? null : (
|
|
|
|
<Col className="code-boxes-col-2-1" span={12}>
|
|
|
|
{rightChildren}
|
|
|
|
</Col>
|
|
|
|
)}
|
2016-05-27 11:48:08 +08:00
|
|
|
</Row>
|
2018-12-07 16:17:45 +08:00
|
|
|
{props.utils.toReactComponent(
|
|
|
|
[
|
|
|
|
'section',
|
|
|
|
{
|
2016-05-27 11:48:08 +08:00
|
|
|
className: 'markdown api-container',
|
2018-12-07 16:17:45 +08:00
|
|
|
},
|
|
|
|
].concat(getChildren(doc.api || ['placeholder'])),
|
|
|
|
)}
|
2016-05-27 11:48:08 +08:00
|
|
|
</article>
|
|
|
|
</DocumentTitle>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|