doc.silverstripe.org/src/components/ChildrenOf.tsx

90 lines
3.2 KiB
TypeScript
Raw Normal View History

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">
<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[] => {
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>
);
});
};
const ChildrenOf: StatelessComponent<ChildrenOfProps> = ({ folderName, exclude, only, currentNode, asList, includeFolders, reverse }) => {
2019-11-08 03:40:20 +01:00
if (!currentNode) {
return null;
}
var nodes: SilverstripeDocument[] = [];
if (!folderName && !exclude && !only) {
2019-12-19 02:33:08 +01:00
nodes = currentNode.isIndex ? getChildren(currentNode, includeFolders) : 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) {
nodes = getChildren(targetFolder, false);
2019-11-08 03:40:20 +01:00
}
} else if (exclude) {
const exclusions = exclude.split(',').map(e => e.toLowerCase());
nodes = getChildren(currentNode, includeFolders).filter(
2019-11-08 03:40:20 +01:00
child => !exclusions.includes(child.fileTitle.toLowerCase())
);
} else if (only) {
const inclusions = only.split(',').map(e => e.toLowerCase());
nodes = getChildren(currentNode, includeFolders).filter(
child => inclusions.includes(child.fileTitle.toLowerCase())
);
}
2020-02-28 03:47:25 +01:00
const clonedNodes = [...nodes];
reverse && clonedNodes.reverse();
let children: ReactElement[] = asList ? createList(clonedNodes) : createCards(clonedNodes);
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;