Skip to content
This repository has been archived by the owner on Mar 1, 2024. It is now read-only.

Commit

Permalink
Merge pull request #383 from tabler/navlink-state-fix
Browse files Browse the repository at this point in the history
fix(NavLink): Fixed navlink active state.
  • Loading branch information
AaronCoplan authored Dec 5, 2018
2 parents 628e1f3 + b7f263e commit 157c33c
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 3 deletions.
9 changes: 8 additions & 1 deletion example/src/SiteWrapper.react.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,17 @@ type navItem = {|
+active?: boolean,
+LinkComponent?: React.ElementType,
+subItems?: Array<subNavItem>,
+useExact?: boolean,
|};

const navBarItems: Array<navItem> = [
{ value: "Home", to: "/", icon: "home", LinkComponent: withRouter(NavLink) },
{
value: "Home",
to: "/",
icon: "home",
LinkComponent: withRouter(NavLink),
useExact: true,
},
{
value: "Interface",
icon: "box",
Expand Down
2 changes: 2 additions & 0 deletions src/components/Nav/Nav.react.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type navItem = {|
+active?: boolean,
+LinkComponent?: React.ElementType,
+subItems?: Array<subNavItem>,
+useExact?: boolean,
|};

type Props = {|
Expand Down Expand Up @@ -123,6 +124,7 @@ class Nav extends React.Component<Props, State> {
LinkComponent={a.LinkComponent}
subItemsObjects={a.subItems}
active={this.computeActive(a.active, a.to, a.subItems)}
useExact={a.useExact}
/>
))) ||
children}
Expand Down
7 changes: 7 additions & 0 deletions src/components/Nav/NavItem.react.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ type Props = {|
* Position of the subnav Dropdown
*/
+position?: Placement,
/**
* Whether or not to pass "exact" property to underlying NavLink component
*/
+useExact?: boolean,
|};

type State = {
Expand Down Expand Up @@ -68,6 +72,7 @@ class NavItem extends React.Component<Props, State> {
active,
subItems,
subItemsObjects,
useExact,
position = "bottom-start",
}: Props = this.props;

Expand All @@ -85,6 +90,7 @@ class NavItem extends React.Component<Props, State> {
hasSubNav={hasSubNav}
active={active}
rootRef={ref}
useExact={useExact}
>
{!hasSubNav && typeof children === "string" ? children : value}
</Nav.Link>
Expand All @@ -98,6 +104,7 @@ class NavItem extends React.Component<Props, State> {
RootComponent={LinkComponent}
hasSubNav={hasSubNav}
active={active}
useExact={useExact}
>
{!hasSubNav && typeof children === "string" ? children : value}
</Nav.Link>
Expand Down
5 changes: 3 additions & 2 deletions src/components/Nav/NavLink.react.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type Props = {|
+to?: string,
+hasSubNav?: boolean,
+rootRef?: (?HTMLElement) => void,
+useExact?: boolean,
|};

function NavLink({
Expand All @@ -23,6 +24,7 @@ function NavLink({
to,
hasSubNav,
rootRef,
useExact,
}: Props): React.Node {
const classes = cn({ "nav-link": true, active: active }, className);

Expand All @@ -36,9 +38,8 @@ function NavLink({
{children}
</React.Fragment>
);

return RootComponent ? (
<RootComponent className={classes} to={to}>
<RootComponent exact={useExact || false} className={classes} to={to}>
{childrenForAll}
</RootComponent>
) : (
Expand Down
1 change: 1 addition & 0 deletions src/components/Site/SiteNav.react.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type navItem = {|
+active?: boolean,
+LinkComponent?: React.ElementType,
+subItems?: Array<subNavItem>,
+useExact?: boolean,
|};

type navItems = Array<navItem>;
Expand Down

0 comments on commit 157c33c

Please sign in to comment.