2019-11-08 03:40:20 +01:00
|
|
|
import React, { StatelessComponent, ReactElement } from 'react';
|
|
|
|
import { SilverstripeDocument, ChildrenOfProps } from '../types';
|
|
|
|
import { Link } from 'gatsby';
|
|
|
|
import { getChildren, getSiblings } from '../utils/nodes';
|
2019-11-12 04:09:29 +01:00
|
|
|
|
2019-11-08 03:40:20 +01:00
|
|
|
const createCards = (children: SilverstripeDocument[]): ReactElement[] => {
|
2019-11-12 05:05:31 +01:00
|
|
|
return children.map(({ summary, slug, title, icon, iconBrand }) => {
|
|
|
|
const iconClass = iconBrand ? `fab fa-${iconBrand}` : `fas fa-${icon || 'file-alt'}`
|
2019-11-08 03:40:20 +01:00
|
|
|
return (
|
|
|
|
<div className="col-12 col-lg-6 py-3" key={slug}>
|
|
|
|
<div className="card shadow-sm">
|
|
|
|
<div className="card-body">
|
2019-11-19 22:08:33 +01:00
|
|
|
<h5 className="card-title">
|
2019-11-20 22:26:11 +01:00
|
|
|
<span className="theme-icon-holder card-icon-holder mr-2">
|
2019-11-12 05:05:31 +01:00
|
|
|
<i className={iconClass}></i>
|
2019-11-08 03:40:20 +01:00
|
|
|
</span>
|
|
|
|
<span className="card-title-text">{title}</span>
|
|
|
|
</h5>
|
|
|
|
<div className="card-text">
|
|
|
|
{summary || ''}
|
|
|
|
</div>
|
2019-11-19 23:46:39 +01:00
|
|
|
<Link className="card-link-mask" to={slug} aria-label={title}></Link>
|
2019-11-08 03:40:20 +01:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
})
|
|
|
|
};
|
|
|
|
|
2019-11-12 04:09:29 +01:00
|
|
|
const createList = (children: SilverstripeDocument[]): ReactElement[] => {
|
2019-11-20 22:26:11 +01:00
|
|
|
return children.map(({ summary, slug, title }) => {
|
2019-11-08 03:40:20 +01:00
|
|
|
return (
|
|
|
|
<React.Fragment key={slug}>
|
|
|
|
<dt><Link to={slug}>{title}</Link></dt>
|
|
|
|
<dd>{summary || ''}</dd>
|
|
|
|
</React.Fragment>
|
|
|
|
);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2019-11-20 22:26:11 +01:00
|
|
|
const ChildrenOf: StatelessComponent<ChildrenOfProps> = ({ folderName, exclude, only, currentNode, asList, includeFolders, reverse }) => {
|
2019-11-08 03:40:20 +01:00
|
|
|
if (!currentNode) {
|
|
|
|
return null;
|
|
|
|
}
|
2019-11-20 22:26:11 +01:00
|
|
|
|
|
|
|
var nodes: SilverstripeDocument[] = [];
|
|
|
|
|
|
|
|
if (!folderName && !exclude && !only) {
|
|
|
|
nodes = currentNode.isIndex ? getChildren(currentNode, false) : getSiblings(currentNode)
|
|
|
|
|
2019-11-08 03:40:20 +01:00
|
|
|
} else if (folderName) {
|
|
|
|
const targetFolder = getChildren(currentNode, true).find(
|
|
|
|
child => child.isIndex && child.fileTitle.toLowerCase() === folderName.toLowerCase()
|
|
|
|
);
|
|
|
|
if (targetFolder) {
|
2019-11-20 22:26:11 +01:00
|
|
|
nodes = getChildren(targetFolder, false);
|
2019-11-08 03:40:20 +01:00
|
|
|
}
|
|
|
|
} else if (exclude) {
|
|
|
|
const exclusions = exclude.split(',').map(e => e.toLowerCase());
|
2019-11-20 22:26:11 +01:00
|
|
|
nodes = getChildren(currentNode, includeFolders).filter(
|
2019-11-08 03:40:20 +01:00
|
|
|
child => !exclusions.includes(child.fileTitle.toLowerCase())
|
|
|
|
);
|
2019-11-20 22:26:11 +01:00
|
|
|
} else if (only) {
|
|
|
|
const inclusions = only.split(',').map(e => e.toLowerCase());
|
|
|
|
nodes = getChildren(currentNode, includeFolders).filter(
|
|
|
|
child => inclusions.includes(child.fileTitle.toLowerCase())
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (reverse) {
|
|
|
|
nodes.reverse();
|
2019-11-08 03:40:20 +01:00
|
|
|
}
|
2019-11-20 22:26:11 +01:00
|
|
|
let children: ReactElement[] = asList ? createList(nodes) : createCards(nodes);
|
2019-11-08 03:40:20 +01:00
|
|
|
|
|
|
|
return (
|
|
|
|
<div className="docs-overview py-5">
|
|
|
|
{asList &&
|
|
|
|
<dl>{children}</dl>
|
|
|
|
}
|
|
|
|
{!asList &&
|
|
|
|
<div className="row">
|
|
|
|
{children}
|
|
|
|
</div>
|
|
|
|
}
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
export default ChildrenOf;
|