Skip to content

Commit

Permalink
chore: refactor, separate active and expanded state for menu items
Browse files Browse the repository at this point in the history
  • Loading branch information
RomanHotsiy committed Jul 31, 2018
1 parent 5ebab5f commit a4f79bf
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 25 deletions.
23 changes: 11 additions & 12 deletions src/components/SideMenu/MenuItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,15 @@ export class MenuItem extends React.Component<MenuItemProps> {
};

componentDidMount() {
const item = this.props.item;
if (item.type !== 'group' && item.active) {
this.scrollIntoView();
}
this.scrollIntoViewIfActive();
}

componentDidUpdate() {
if (this.props.item.active) {
this.scrollIntoView();
}
this.scrollIntoViewIfActive();
}

scrollIntoView() {
if (this.ref) {
scrollIntoViewIfActive() {
if (this.props.item.active && this.ref) {
this.ref.scrollIntoViewIfNeeded();
}
}
Expand All @@ -56,22 +51,26 @@ export class MenuItem extends React.Component<MenuItemProps> {
{item.type === 'operation' ? (
<OperationMenuItemContent {...this.props} item={item as OperationModel} />
) : (
<MenuItemLabel depth={item.depth} active={item.active} type={item.type}>
<MenuItemLabel depth={item.depth} active={item.active || item.expanded} type={item.type}>
<MenuItemTitle title={item.name}>
{item.name}
{this.props.children}
</MenuItemTitle>
{(item.depth > 0 &&
item.items.length > 0 && (
<ShelfIcon float={'right'} direction={item.active ? 'down' : 'right'} />
<ShelfIcon float={'right'} direction={item.expanded ? 'down' : 'right'} />
)) ||
null}
</MenuItemLabel>
)}
{!withoutChildren &&
item.items &&
item.items.length > 0 && (
<MenuItems active={item.active} items={item.items} onActivate={this.props.onActivate} />
<MenuItems
expanded={item.expanded}
items={item.items}
onActivate={this.props.onActivate}
/>
)}
</MenuItemLi>
);
Expand Down
6 changes: 3 additions & 3 deletions src/components/SideMenu/MenuItems.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { MenuItemUl } from './styled.elements';

export interface MenuItemsProps {
items: IMenuItem[];
active?: boolean;
expanded?: boolean;
onActivate?: (item: IMenuItem) => void;
style?: React.CSSProperties;
root?: boolean;
Expand All @@ -20,12 +20,12 @@ export interface MenuItemsProps {
export class MenuItems extends React.Component<MenuItemsProps> {
render() {
const { items, root, className } = this.props;
const active = this.props.active == null ? true : this.props.active;
const expanded = this.props.expanded == null ? true : this.props.expanded;
return (
<MenuItemUl
className={className}
style={this.props.style}
active={active}
expanded={expanded}
{...(root ? { role: 'navigation' } : {})}
>
{items.map((item, idx) => (
Expand Down
4 changes: 2 additions & 2 deletions src/components/SideMenu/styled.elements.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,15 @@ function menuItemActiveBg(depth): string {
}
}

export const MenuItemUl = withProps<{ active: boolean }>(styled.ul)`
export const MenuItemUl = withProps<{ expanded: boolean }>(styled.ul)`
margin: 0;
padding: 0;
& & {
font-size: 0.929em;
}
${props => (props.active ? '' : 'display: none;')};
${props => (props.expanded ? '' : 'display: none;')};
`;

export const MenuItemLi = withProps<{ depth: number }>(styled.li)`
Expand Down
16 changes: 11 additions & 5 deletions src/services/MenuStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,17 @@ export interface IMenuItem {
description?: string;
depth: number;
active: boolean;
expanded: boolean;
items: IMenuItem[];
parent?: IMenuItem;
deprecated?: boolean;
type: MenuItemType;

deactivate(): void;
activate(): void;

collapse(): void;
expand(): void;
}

export const SECTION_ATTR = 'data-section-id';
Expand Down Expand Up @@ -198,19 +202,21 @@ export class MenuStore {
HistoryService.update(item.id, rewriteHistory);
}

while (item !== undefined) {
item.activate();
item = item.parent;
}
item.activate();
item.expand();
}

/**
* makes item and all the parents not active
* @param item item to deactivate
*/
deactivate(item: IMenuItem | undefined) {
if (item === undefined) {
return;
}
item.deactivate();
while (item !== undefined) {
item.deactivate();
item.collapse();
item = item.parent;
}
}
Expand Down
20 changes: 17 additions & 3 deletions src/services/models/Group.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export class GroupModel implements IMenuItem {
externalDocs?: OpenAPIExternalDocumentation;

@observable active: boolean = false;
@observable expanded: boolean = false;

depth: number;
//#endregion
Expand All @@ -41,7 +42,7 @@ export class GroupModel implements IMenuItem {

// groups are active (expanded) by default
if (this.type === 'group') {
this.active = true;
this.expanded = true;
}
}

Expand All @@ -51,11 +52,24 @@ export class GroupModel implements IMenuItem {
}

@action
deactivate() {
// disallow deactivating groups
expand() {
if (this.parent) {
this.parent.expand();
}
this.expanded = true;
}

@action
collapse() {
// disallow collapsing groups
if (this.type === 'group') {
return;
}
this.expanded = false;
}

@action
deactivate() {
this.active = false;
}
}
11 changes: 11 additions & 0 deletions src/services/models/Operation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export class OperationModel implements IMenuItem {

@observable ready?: boolean = true;
@observable active: boolean = false;
@observable expanded: boolean = false;
//#endregion

pointer: string;
Expand Down Expand Up @@ -105,6 +106,16 @@ export class OperationModel implements IMenuItem {
this.active = false;
}

expand() {
if (this.parent) {
this.parent.expand();
}
}

collapse() {
/* do nothing */
}

@memoize
get requestBody() {
return (
Expand Down

0 comments on commit a4f79bf

Please sign in to comment.