Skip to content

Commit

Permalink
Harry mongo nav tweaks (#301)
Browse files Browse the repository at this point in the history
* Unify OnElementClick and NavItem enums to one enum

* Revise exports from index.ts and also export NavItem type

* Rename NavItem to NavElement

* When clicking a project menu item close the menu

* move all useState calls in Menu into one location

* Add remaining activeNav values

* add changeset
  • Loading branch information
hswolff authored Mar 6, 2020
1 parent 0f7d8a3 commit 4e1e9dc
Show file tree
Hide file tree
Showing 12 changed files with 90 additions and 69 deletions.
5 changes: 5 additions & 0 deletions .changeset/old-tables-flash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@leafygreen-ui/mongo-nav': minor
---

Add remaining activeNav values. Unify OnElementClick into NavElement.
16 changes: 9 additions & 7 deletions packages/menu/src/Menu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,21 @@ function Menu({

const hasSetInitialFocus = useRef(false);
const hasSetInitialOpen = useRef(false);
const [focused, setFocused] = useState<HTMLElement>(refs[0] || null);

const [, setClosed] = useState(false);
const [currentSubMenu, setCurrentSubMenu] = useState<React.ReactElement<
HTMLLIElement
> | null>(null);
const [uncontrolledOpen, uncontrolledSetOpen] = useState(false);

const { setUsingKeyboard } = useUsingKeyboardContext();

const setOpen =
(typeof controlledOpen === 'boolean' && controlledSetOpen) ||
uncontrolledSetOpen;
const open = controlledOpen ?? uncontrolledOpen;

function updateChildren(children: React.ReactNode): React.ReactNode {
return React.Children.map(children, child => {
if (!React.isValidElement(child) || child.props?.disabled) {
Expand Down Expand Up @@ -196,13 +205,6 @@ function Menu({
});
}

const [focused, setFocused] = useState<HTMLElement>(refs[0] || null);
const [uncontrolledOpen, uncontrolledSetOpen] = useState(false);
const setOpen =
(typeof controlledOpen === 'boolean' && controlledSetOpen) ||
uncontrolledSetOpen;
const open = controlledOpen ?? uncontrolledOpen;

// When a SubMenu becomes open, it's set to currentSubMenu, and we focus on the first child.
useEffect(() => {
const focusedRefIndex = refs.indexOf(focused);
Expand Down
12 changes: 6 additions & 6 deletions packages/mongo-nav/src/MongoNav.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import '@testing-library/jest-dom/extend-expect';
import { nullableElement, Queries } from 'packages/lib/src/testHelpers';
import { dataFixtures } from './data';
import MongoNav from './MongoNav';
import { OnElementClick } from './types';
import { NavElement } from './types';

// types
interface ExpectedElements {
Expand Down Expand Up @@ -349,7 +349,7 @@ describe('packages/mongo-nav', () => {
fireEvent.click(atlas as HTMLElement);
expect(onElementClick).toHaveBeenCalled();
expect(onElementClick).toHaveBeenCalledWith(
OnElementClick.Cloud,
NavElement.Cloud,
expect.anything(),
);
});
Expand All @@ -359,7 +359,7 @@ describe('packages/mongo-nav', () => {
fireEvent.click(realm as HTMLElement);
expect(onElementClick).toHaveBeenCalled();
expect(onElementClick).toHaveBeenCalledWith(
OnElementClick.Realm,
NavElement.Realm,
expect.anything(),
);
});
Expand All @@ -369,7 +369,7 @@ describe('packages/mongo-nav', () => {
fireEvent.click(charts as HTMLElement);
expect(onElementClick).toHaveBeenCalled();
expect(onElementClick).toHaveBeenCalledWith(
OnElementClick.Charts,
NavElement.Charts,
expect.anything(),
);
});
Expand All @@ -381,7 +381,7 @@ describe('packages/mongo-nav', () => {
const logout = expectedElements.userMenuLogout;
fireEvent.click(logout as HTMLElement);
expect(onElementClick).toHaveBeenCalledWith(
OnElementClick.Logout,
NavElement.Logout,
expect.anything(),
);
});
Expand All @@ -405,7 +405,7 @@ describe('packages/mongo-nav', () => {
const logout = expectedElements.onPremLogout;
fireEvent.click(logout as HTMLElement);
expect(onElementClick).toHaveBeenCalledWith(
OnElementClick.Logout,
NavElement.Logout,
expect.anything(),
);
});
Expand Down
10 changes: 7 additions & 3 deletions packages/mongo-nav/src/MongoNav.story.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { select, boolean } from '@storybook/addon-knobs';
import { css } from '@leafygreen-ui/emotion';
import MongoNav from '.';
import LeafygreenProvider from '@leafygreen-ui/leafygreen-provider';
import { Product, NavItem } from './types';
import { Product, NavElement } from './types';

const setStorybookWidth = css`
width: 100%;
Expand All @@ -21,14 +21,18 @@ storiesOf('MongoNav', module).add('Default', () => (
Object.values(Product),
Product.Cloud,
)}
activeNav={select('activeNav', Object.values(NavItem), 'accessManager')}
activeNav={select(
'activeNav',
Object.values(NavElement),
'accessManager',
)}
onOrganizationChange={() => {}}
onProjectChange={() => {}}
admin={boolean('admin', true)}
onPrem={{
mfa: true,
version: '4.4.0',
enabled: boolean('enabled', false),
enabled: boolean('onPrem', false),
}}
loadData={boolean('loadData', true)}
/>
Expand Down
12 changes: 6 additions & 6 deletions packages/mongo-nav/src/MongoNav.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,11 @@ import {
Product,
URLSInterface,
HostsInterface,
NavItem,
NavElement,
Mode,
DataInterface,
ErrorCode,
OnPremInterface,
OnElementClick,
PostBodyInterface,
} from './types';
import { dataFixtures, hostDefaults } from './data';
Expand All @@ -40,7 +39,7 @@ interface MongoNavInterface {
/**
* Determines what nav item is currently active.
*/
activeNav?: NavItem;
activeNav?: NavElement;

/**
* Describes whether or not user is an `admin`.
Expand Down Expand Up @@ -124,7 +123,7 @@ interface MongoNavInterface {
* Click EventHandler that receives a `type` as its first argument and the associated `MouseEvent` as its second
* This prop provides a hook into product link and logout link clicks and allows consuming applications to handle routing internally
*/
onElementClick?: (type: OnElementClick, event: React.MouseEvent) => void;
onElementClick?: (type: NavElement, event: React.MouseEvent) => void;

/**
* Determines whether or not the component will fetch data from cloud
Expand Down Expand Up @@ -183,7 +182,7 @@ function MongoNav({
constructProjectURL: constructProjectURLProp,
onError = () => {},
onSuccess = () => {},
onElementClick = (_type: OnElementClick, _event: React.MouseEvent) => {}, // eslint-disable-line @typescript-eslint/no-unused-vars
onElementClick = (_type: NavElement, _event: React.MouseEvent) => {}, // eslint-disable-line @typescript-eslint/no-unused-vars
onPrem = { mfa: false, enabled: false, version: '' },
activeOrgId,
activeProjectId,
Expand Down Expand Up @@ -334,6 +333,7 @@ function MongoNav({
{showProjNav && !onPrem.enabled && (
<ProjectNav
activeProduct={activeProduct}
activeNav={activeNav}
current={data?.currentProject}
data={data?.projects}
constructProjectURL={constructProjectURL}
Expand All @@ -352,7 +352,7 @@ MongoNav.displayName = 'MongoNav';

MongoNav.propTypes = {
activeProduct: PropTypes.oneOf(Object.values(Product)),
activeNav: PropTypes.oneOf(Object.values(NavItem)),
activeNav: PropTypes.oneOf(Object.values(NavElement)),
hosts: PropTypes.objectOf(PropTypes.string),
onOrganizationChange: PropTypes.func,
onProjectChange: PropTypes.func,
Expand Down
7 changes: 2 additions & 5 deletions packages/mongo-nav/src/OnElementClickProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import React, { createContext, useContext } from 'react';
import { OnElementClick } from './types';
import { NavElement } from './types';

type OnElementClickType = (
type: OnElementClick,
event: React.MouseEvent,
) => void;
type OnElementClickType = (type: NavElement, event: React.MouseEvent) => void;

interface OnElementClickProviderProps {
children: React.ReactNode;
Expand Down
4 changes: 2 additions & 2 deletions packages/mongo-nav/src/helpers/OnPremUserMenu.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react';
import { Menu, MenuItem } from '@leafygreen-ui/menu';
import { css } from '@leafygreen-ui/emotion';
import { URLSInterface, OnElementClick } from '../types';
import { URLSInterface, NavElement } from '../types';
import { UserMenuTrigger } from '../user-menu/index';
import { useOnElementClick } from '../OnElementClickProvider';

Expand Down Expand Up @@ -81,7 +81,7 @@ export default function OnPremUserMenu({

<MenuItem
onClick={(event: React.MouseEvent) =>
onElementClick(OnElementClick.Logout, event)
onElementClick(NavElement.Logout, event)
}
data-testid="om-user-menuitem-sign-out"
>
Expand Down
12 changes: 6 additions & 6 deletions packages/mongo-nav/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import MongoNav from './MongoNav';
import { orgNavHeight } from './org-nav/index';
import { projectNavHeight } from './project-nav/index';
import UserMenu from './user-menu/index';
export { default } from './MongoNav';

export { UserMenu, orgNavHeight, projectNavHeight };
export default MongoNav;
export { default as UserMenu } from './user-menu/index';

export { NavElement } from './types';
export { orgNavHeight } from './org-nav/index';
export { projectNavHeight } from './project-nav/index';
18 changes: 9 additions & 9 deletions packages/mongo-nav/src/org-nav/OrgNav.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
OrganizationInterface,
Product,
URLSInterface,
NavItem,
NavElement,
CurrentOrganizationInterface,
HostsInterface,
OrgPaymentLabel,
Expand Down Expand Up @@ -124,7 +124,7 @@ interface OrgNav {
data?: Array<OrganizationInterface>;
constructOrganizationURL: (orgID: string) => string;
urls: Required<URLSInterface>;
activeNav?: NavItem;
activeNav?: NavElement;
onOrganizationChange: React.ChangeEventHandler;
admin: boolean;
hosts: Required<HostsInterface>;
Expand Down Expand Up @@ -156,7 +156,7 @@ export default function OrgNav({
const { width: viewportWidth } = useViewportSize();
const isTablet = viewportWidth < breakpoints.medium;
const isMobile = viewportWidth < breakpoints.small;
const disabled = activeNav === 'userSettings';
const disabled = activeNav === NavElement.UserSettings;

let paymentVariant: Colors | undefined;
let key: Colors;
Expand Down Expand Up @@ -243,7 +243,7 @@ export default function OrgNav({
constructOrganizationURL={constructOrganizationURL}
urls={urls}
onChange={onOrganizationChange}
isActive={activeNav === 'orgSettings'}
isActive={activeNav === NavElement.OrgSettings}
loading={!current}
disabled={disabled}
isOnPrem={onPremEnabled}
Expand All @@ -257,7 +257,7 @@ export default function OrgNav({
<>
<OrgNavLink
href={current && orgNav.accessManager}
isActive={activeNav === 'accessManager'}
isActive={activeNav === NavElement.AccessManager}
loading={!current}
data-testid="org-nav-access-manager"
>
Expand Down Expand Up @@ -293,7 +293,7 @@ export default function OrgNav({

<OrgNavLink
href={current && orgNav.support}
isActive={activeNav === 'support'}
isActive={activeNav === NavElement.Support}
loading={!current}
className={supportContainer}
data-testid="org-nav-support"
Expand All @@ -304,7 +304,7 @@ export default function OrgNav({
{!onPremEnabled && (
<OrgNavLink
href={current && orgNav.billing}
isActive={activeNav === 'billing'}
isActive={activeNav === NavElement.Billing}
loading={!current}
data-testid="org-nav-billing"
>
Expand All @@ -330,7 +330,7 @@ export default function OrgNav({
{!isMobile && (
<OrgNavLink
href={orgNav.allClusters}
isActive={activeNav === 'allClusters'}
isActive={activeNav === NavElement.AllClusters}
className={rightLinkMargin}
data-testid="org-nav-all-clusters-link"
>
Expand All @@ -341,7 +341,7 @@ export default function OrgNav({
{!isTablet && admin && !onPremEnabled && (
<OrgNavLink
href={orgNav.admin}
isActive={activeNav === 'admin'}
isActive={activeNav === NavElement.Admin}
className={rightLinkMargin}
data-testid="org-nav-admin-link"
>
Expand Down
Loading

0 comments on commit 4e1e9dc

Please sign in to comment.