Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Organize doc menu into groups #110

Merged
merged 1 commit into from
Nov 19, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 42 additions & 7 deletions docs/app/Components/Sidebar/Sidebar.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,63 @@
import _ from 'lodash';
import React, {Component} from 'react';
import stardust, {Menu, MenuItem, Input} from 'stardust';
import META from 'src/utils/Meta';

export default class Sidebar extends Component {
state = {query: ''};

handleSearchChange = e => this.setState({query: e.target.value});

getComponentsByQuery() {
return _.filter(stardust, component => {
const name = component._meta.name;
const isParent = META.isParent(component);
const isQueryMatch = new RegExp(this.state.query, 'i').test(name);
return isParent && isQueryMatch;
});
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pulled menu item filtering into separate methods.


getComponentsByType = type => {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method builds a submenu for each section, like:

Elements

  • Button

Only parent elements are included in the menu. For instance, Modal, but not its child components ModalHeader, ModalContent, etc.

const items = _(this.getComponentsByQuery())
.filter(component => META.isType(component, type))
.sortBy((component, name) => name)
.map(component => {
const name = component._meta.name;
return <MenuItem key={name} name={name} href={`#${name}`} />;
})
.value();

const subMenu = (
<div className='item'>
<div className='header'>{_.capitalize(type)}s</div>
<div className='menu'>{items}</div>
</div>
);

return items && subMenu;
};

render() {
const menuItems = Object.keys(stardust)
.sort()
.filter(name => new RegExp(this.state.query, 'i').test(name))
.map(name => <MenuItem key={name} name={name} href={`#${name}`} />);
const elements = this.getComponentsByType(META.type.element);
const collections = this.getComponentsByType(META.type.collection);
const views = this.getComponentsByType(META.type.view);
const modules = this.getComponentsByType(META.type.module);

return (
<Menu className='small inverted secondary vertical pointing fluid'>
<Menu className='inverted secondary vertical fluid' style={{margin: 0}}>
<MenuItem>
<Input
className='small transparent inverted'
className='transparent inverted icon'
icon='search'
placeholder='Search'
iconClass='search link icon'
onChange={this.handleSearchChange}
/>
</MenuItem>
{menuItems}
{elements}
{collections}
{views}
{modules}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Order all the submenus.

</Menu>
);
}
Expand Down
2 changes: 1 addition & 1 deletion src/collections/Menu/Menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export default class Menu extends Component {
const activeItemName = !hasActiveItem && i === 0
? child.props.name
: this.state.activeItem || this.props.activeItem;
return React.cloneElement(child, {
return child && React.cloneElement(child, {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The menu component cannot clone the child if it doesn't exist. When filtering the doc site menu by search we we're getting errors. Now, we skip cloning and updating menu children if the child is falsy.

activeItem: activeItemName,
callbackParent: this.handleClickItem,
});
Expand Down
1 change: 0 additions & 1 deletion src/collections/Menu/MenuItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ export default class MenuItem extends Component {
const classes = classNames(
'sd-menu-item',
this.props.className,
'blue',
'item',
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed default color class on menu item, this should be up to the consumer.

{active: isActive}
);
Expand Down
1 change: 1 addition & 0 deletions src/elements/Button/Buttons.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export default class Buttons extends Component {
library: META.library.semanticUI,
name: 'Buttons',
type: META.type.element,
parent: 'Button'
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Buttons is not a top level component, but a child to the Button component. This prevents it from showing in the menu.

};

render() {
Expand Down
9 changes: 8 additions & 1 deletion src/utils/Meta.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import _ from 'lodash';

/**
* Component meta information. Used to declaratively classify and identify components.
* @type {{}}
Expand Down Expand Up @@ -28,9 +30,14 @@ const META = {
isElement: ({_meta}) => _meta.type === META.type.element,
isView: ({_meta}) => _meta.type === META.type.view,
isModule: ({_meta}) => _meta.type === META.type.module,
isType: ({_meta}, type) => _meta.type === type,

// parent
isChild: ({_meta}) => !!_meta.parent,
isParent: (component) => (
// has no 'parent' or 'parent' is self
!_.has(component, '_meta.parent') || _.get(component, '_meta.parent') === component._meta.name
),
isChild: component => !META.isParent(component),
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extended META to include helper method for identifying a parent component and a component with a type string.

};

export default META;