mirror of
https://github.com/kingomarnajjar/flex-template-web.git
synced 2026-07-31 02:26:50 +10:00
Add group navigation
This commit is contained in:
parent
7d6950b127
commit
8951b93140
2 changed files with 57 additions and 1 deletions
|
|
@ -76,3 +76,7 @@
|
||||||
border-width: 1px;
|
border-width: 1px;
|
||||||
border-top-color: transparent;
|
border-top-color: transparent;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.selectedGroup {
|
||||||
|
font-weight: bold;
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
import React, { PropTypes } from 'react';
|
import React, { PropTypes } from 'react';
|
||||||
|
import { isEmpty } from 'lodash';
|
||||||
import { NamedLink } from '../../components';
|
import { NamedLink } from '../../components';
|
||||||
import * as allExamples from '../../examples';
|
import * as allExamples from '../../examples';
|
||||||
|
|
||||||
|
|
@ -51,7 +52,7 @@ const Example = props => {
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
const { bool, func, node, object, oneOfType, shape, string } = PropTypes;
|
const { bool, func, node, object, oneOfType, shape, string, arrayOf } = PropTypes;
|
||||||
|
|
||||||
Example.defaultProps = {
|
Example.defaultProps = {
|
||||||
description: null,
|
description: null,
|
||||||
|
|
@ -70,6 +71,43 @@ Example.propTypes = {
|
||||||
useDefaultWrapperStyles: bool,
|
useDefaultWrapperStyles: bool,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const Nav = props => {
|
||||||
|
const { groups, selectedGroup } = props;
|
||||||
|
const filteredGroups = groups.filter(g => g !== ALL && g !== DEFAULT_GROUP);
|
||||||
|
const toGroupLink = group => {
|
||||||
|
const linkProps = {
|
||||||
|
name: group === ALL ? 'Styleguide' : 'StyleguideGroup',
|
||||||
|
params: group === ALL ? null : { group },
|
||||||
|
};
|
||||||
|
const linkContent = group === ALL ? 'all' : group;
|
||||||
|
const isSelected = selectedGroup && group === selectedGroup;
|
||||||
|
return (
|
||||||
|
<li key={group}>
|
||||||
|
<NamedLink {...linkProps} className={isSelected ? css.selectedGroup : null}>
|
||||||
|
{linkContent}
|
||||||
|
</NamedLink>
|
||||||
|
</li>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
return (
|
||||||
|
<nav>
|
||||||
|
<h2>Filter by category:</h2>
|
||||||
|
<ul>
|
||||||
|
{toGroupLink(ALL)}
|
||||||
|
{filteredGroups.map(toGroupLink)}
|
||||||
|
{toGroupLink(DEFAULT_GROUP)}
|
||||||
|
</ul>
|
||||||
|
</nav>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
Nav.defaultProps = { selectedGroup: null };
|
||||||
|
|
||||||
|
Nav.propTypes = {
|
||||||
|
groups: arrayOf(string).isRequired,
|
||||||
|
selectedGroup: string,
|
||||||
|
};
|
||||||
|
|
||||||
const flatExamples = examples => {
|
const flatExamples = examples => {
|
||||||
return Object.keys(examples).reduce(
|
return Object.keys(examples).reduce(
|
||||||
(flattened, componentName) => {
|
(flattened, componentName) => {
|
||||||
|
|
@ -80,6 +118,7 @@ const flatExamples = examples => {
|
||||||
{
|
{
|
||||||
componentName,
|
componentName,
|
||||||
exampleName,
|
exampleName,
|
||||||
|
group: DEFAULT_GROUP,
|
||||||
...ex,
|
...ex,
|
||||||
},
|
},
|
||||||
]);
|
]);
|
||||||
|
|
@ -107,6 +146,17 @@ const StyleguidePage = props => {
|
||||||
const exampleName = params.example || ALL;
|
const exampleName = params.example || ALL;
|
||||||
|
|
||||||
const flattened = flatExamples(allExamples);
|
const flattened = flatExamples(allExamples);
|
||||||
|
const groups = flattened.reduce(
|
||||||
|
(result, ex) => {
|
||||||
|
if (ex.group && !result.includes(ex.group)) {
|
||||||
|
return result.concat(ex.group);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
},
|
||||||
|
[]
|
||||||
|
);
|
||||||
|
groups.sort();
|
||||||
|
const selectedGroup = isEmpty(params) ? ALL : params.group;
|
||||||
const examples = examplesFor(flattened, group, componentName, exampleName);
|
const examples = examplesFor(flattened, group, componentName, exampleName);
|
||||||
|
|
||||||
// Raw examples are rendered without any wrapper
|
// Raw examples are rendered without any wrapper
|
||||||
|
|
@ -128,6 +178,8 @@ const StyleguidePage = props => {
|
||||||
<h1 className={css.withPadding}>
|
<h1 className={css.withPadding}>
|
||||||
<NamedLink name="Styleguide">Styleguide</NamedLink>
|
<NamedLink name="Styleguide">Styleguide</NamedLink>
|
||||||
</h1>
|
</h1>
|
||||||
|
<Nav groups={groups} selectedGroup={selectedGroup} />
|
||||||
|
<h2>Component examples:</h2>
|
||||||
{html}
|
{html}
|
||||||
</section>
|
</section>
|
||||||
);
|
);
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue