mirror of
https://github.com/ansible/awx.git
synced 2026-03-01 00:38:45 -03:30
Improve nav behavior
* Use more selective route matching when determining if a nav item is active * Don't automatically collapse nav groups when user navigates to a different group
This commit is contained in:
@@ -1,16 +1,18 @@
|
|||||||
import React, { Component } from 'react';
|
import React, { Component } from 'react';
|
||||||
import PropTypes from 'prop-types';
|
import PropTypes from 'prop-types';
|
||||||
import { withRouter, Link } from 'react-router-dom';
|
import { matchPath, Link, withRouter } from 'react-router-dom';
|
||||||
import { NavExpandable, NavItem } from '@patternfly/react-core';
|
import { NavExpandable, NavItem } from '@patternfly/react-core';
|
||||||
|
|
||||||
class NavExpandableGroup extends Component {
|
class NavExpandableGroup extends Component {
|
||||||
constructor(props) {
|
constructor(props) {
|
||||||
super(props);
|
super(props);
|
||||||
const { routes } = this.props;
|
const { routes } = this.props;
|
||||||
|
this.state = { isExpanded: false };
|
||||||
|
|
||||||
// Extract a list of paths from the route params and store them for later. This creates
|
// Extract a list of paths from the route params and store them for later. This creates
|
||||||
// an array of url paths associated with any NavItem component rendered by this component.
|
// an array of url paths associated with any NavItem component rendered by this component.
|
||||||
this.navItemPaths = routes.map(({ path }) => path);
|
this.navItemPaths = routes.map(({ path }) => path);
|
||||||
|
this.handleExpand = this.handleExpand.bind(this);
|
||||||
this.isActiveGroup = this.isActiveGroup.bind(this);
|
this.isActiveGroup = this.isActiveGroup.bind(this);
|
||||||
this.isActivePath = this.isActivePath.bind(this);
|
this.isActivePath = this.isActivePath.bind(this);
|
||||||
}
|
}
|
||||||
@@ -21,20 +23,24 @@ class NavExpandableGroup extends Component {
|
|||||||
|
|
||||||
isActivePath(path) {
|
isActivePath(path) {
|
||||||
const { history } = this.props;
|
const { history } = this.props;
|
||||||
|
return Boolean(matchPath(history.location.pathname, { path }));
|
||||||
|
}
|
||||||
|
|
||||||
return history.location.pathname.startsWith(path);
|
handleExpand(e, isExpanded) {
|
||||||
|
this.setState({ isExpanded });
|
||||||
}
|
}
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
const { groupId, groupTitle, routes } = this.props;
|
const { groupId, groupTitle, routes } = this.props;
|
||||||
const isActive = this.isActiveGroup();
|
const { isExpanded } = this.state;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<NavExpandable
|
<NavExpandable
|
||||||
isActive={isActive}
|
isActive={this.isActiveGroup()}
|
||||||
isExpanded={isActive}
|
isExpanded={isExpanded}
|
||||||
groupId={groupId}
|
groupId={groupId}
|
||||||
title={groupTitle}
|
title={groupTitle}
|
||||||
|
onExpand={this.handleExpand}
|
||||||
>
|
>
|
||||||
{routes.map(({ path, title }) => (
|
{routes.map(({ path, title }) => (
|
||||||
<NavItem
|
<NavItem
|
||||||
|
|||||||
Reference in New Issue
Block a user