ant-design/site/theme/template/Content/ComponentDoc.jsx

183 lines
5.2 KiB
React
Raw Normal View History

import React from 'react';
2019-09-08 00:13:43 +08:00
import { Helmet } from 'react-helmet';
2019-08-08 12:35:48 +08:00
import { FormattedMessage, injectIntl } from 'react-intl';
import classNames from 'classnames';
2018-12-07 16:17:45 +08:00
import { Row, Col, Icon, Affix, Tooltip } from 'antd';
import { getChildren } from 'jsonml.js/lib/utils';
import Demo from './Demo';
import EditButton from './EditButton';
import { ping } from '../utils';
2019-08-08 12:35:48 +08:00
class ComponentDoc extends React.Component {
state = {
expandAll: false,
showRiddleButton: false,
};
componentDidMount() {
this.pingTimer = ping(status => {
if (status !== 'timeout' && status !== 'error') {
this.setState({
showRiddleButton: true,
});
}
});
}
2019-05-21 17:51:53 +08:00
shouldComponentUpdate(nextProps, nextState) {
const { location } = this.props;
const { location: nextLocation } = nextProps;
2019-05-21 17:51:53 +08:00
const { expandAll, showRiddleButton } = this.state;
const { expandAll: nextExpandAll, showRiddleButton: nextShowRiddleButton } = nextState;
2019-05-21 17:51:53 +08:00
if (
nextLocation.pathname === location.pathname &&
expandAll === nextExpandAll &&
showRiddleButton === nextShowRiddleButton
) {
return false;
}
return true;
}
componentWillUnmount() {
clearTimeout(this.pingTimer);
}
handleExpandToggle = () => {
const { expandAll } = this.state;
this.setState({
expandAll: !expandAll,
});
2018-12-07 16:17:45 +08:00
};
render() {
const {
doc,
location,
intl: { locale },
utils,
demos,
} = this.props;
const { content, meta } = doc;
2019-08-08 12:35:48 +08:00
const demoValues = Object.keys(demos).map(key => demos[key]);
const { expandAll, showRiddleButton } = this.state;
const isSingleCol = meta.cols === 1;
const leftChildren = [];
const rightChildren = [];
2019-08-08 12:35:48 +08:00
const showedDemo = demoValues.some(demo => demo.meta.only)
? demoValues.filter(demo => demo.meta.only)
: demoValues.filter(demo => demo.preview);
2018-12-07 16:17:45 +08:00
showedDemo
.sort((a, b) => a.meta.order - b.meta.order)
.forEach((demoData, index) => {
const demoElem = (
<Demo
{...demoData}
key={demoData.meta.filename}
2019-08-08 12:35:48 +08:00
utils={utils}
expand={expandAll}
location={location}
/>
);
if (index % 2 === 0 || isSingleCol) {
leftChildren.push(demoElem);
} else {
rightChildren.push(demoElem);
}
});
const expandTriggerClass = classNames({
'code-box-expand-trigger': true,
'code-box-expand-trigger-active': expandAll,
});
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;
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>
</li>
);
});
2016-09-21 11:28:38 +08:00
const { title, subtitle, filename } = meta;
const articleClassName = classNames({
'show-riddle-button': showRiddleButton,
});
2019-09-08 00:13:43 +08:00
const helmetTitle = `${subtitle || ''} ${title[locale] || title} - Ant Design`;
2019-09-08 11:39:51 +08:00
const contentChild = getChildren(content.content).find(jml => {
const [tag] = jml;
return tag === 'p';
});
return (
2019-09-08 00:13:43 +08:00
<article className={articleClassName}>
<Helmet>
2019-09-08 11:07:54 +08:00
{helmetTitle && <title>{helmetTitle}</title>}
{helmetTitle && <meta property="og:title" content={helmetTitle} />}
{contentChild && contentChild[1] && <meta name="description" content={contentChild[1]} />}
2019-09-08 00:13:43 +08:00
</Helmet>
<Affix className="toc-affix" offsetTop={16}>
<ul id="demo-toc" className="toc">
{jumper}
</ul>
</Affix>
<section className="markdown">
<h1>
{title[locale] || title}
{!subtitle ? null : <span className="subtitle">{subtitle}</span>}
<EditButton
title={<FormattedMessage id="app.content.edit-page" />}
filename={filename}
/>
</h1>
{utils.toReactComponent(
['section', { className: 'markdown' }].concat(getChildren(content)),
)}
<h2>
<FormattedMessage id="app.component.examples" />
<Tooltip
title={
<FormattedMessage
id={`app.component.examples.${expandAll ? 'collapse' : 'expand'}`}
/>
2019-09-08 00:13:43 +08:00
}
>
2019-09-08 00:13:43 +08:00
<Icon
type={`${expandAll ? 'appstore' : 'appstore-o'}`}
className={expandTriggerClass}
onClick={this.handleExpandToggle}
/>
</Tooltip>
</h2>
</section>
<Row gutter={16}>
<Col
span={isSingleCol ? 24 : 12}
className={isSingleCol ? 'code-boxes-col-1-1' : 'code-boxes-col-2-1'}
>
{leftChildren}
</Col>
{isSingleCol ? null : (
<Col className="code-boxes-col-2-1" span={12}>
{rightChildren}
</Col>
2018-12-07 16:17:45 +08:00
)}
2019-09-08 00:13:43 +08:00
</Row>
{utils.toReactComponent(
[
'section',
{
className: 'markdown api-container',
},
].concat(getChildren(doc.api || ['placeholder'])),
)}
</article>
);
}
}
2019-08-08 12:35:48 +08:00
export default injectIntl(ComponentDoc);