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

WAITP-1211: Entity Dropdown Menu #4672

Merged
merged 15 commits into from
Jun 25, 2023
32 changes: 20 additions & 12 deletions packages/tupaia-web/src/features/EntitySearch/EntityMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,18 @@ const MenuLink = styled(Button).attrs({
`;

type EntityWithChildren = Entity & { children?: Entity[] };

interface EntityMenuProps {
projectCode: string;
children: EntityWithChildren[];
isLoading: boolean;
}

/*
* ExpandedList is a recursive component that renders a list of entities and their children to
* display an expandable entity menu.
*/
export const EntityMenu = ({
projectCode,
children,
isLoading,
}: {
projectCode: string;
children: EntityWithChildren[];
isLoading: boolean;
}) => {
export const EntityMenu = ({ projectCode, children, isLoading }: EntityMenuProps) => {
const entityList = children.sort((a, b) => a.name.localeCompare(b.name));
return (
<List>
Expand Down Expand Up @@ -82,7 +81,7 @@ const EntityMenuItem = ({
const [isExpanded, setIsExpanded] = useState(false);
const { data, isLoading } = useEntities(projectCode!, entity.code!, { enabled: isExpanded });

const onExpand = () => {
const toggleExpanded = () => {
setIsExpanded(!isExpanded);
};

Expand All @@ -100,12 +99,21 @@ const EntityMenuItem = ({
<MenuLink to={link}>
{entity.name} {entity.type === 'facility' && <HospitalIcon />}
</MenuLink>
<IconButton onClick={onExpand} disabled={parentIsLoading || !nextChildren}>
<IconButton
onClick={toggleExpanded}
disabled={parentIsLoading || !nextChildren}
aria-controls="entity-expand-button"
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice, thank you! Can we just add an aria-label of something like toggle menu for .... ?

>
<ExpandIcon />
</IconButton>
</FlexRow>
{isExpanded && (
<EntityMenu children={nextChildren} projectCode={projectCode} isLoading={isLoading} />
<EntityMenu
children={nextChildren}
projectCode={projectCode}
isLoading={isLoading}
aria-expanded={isExpanded}
Copy link
Contributor

Choose a reason for hiding this comment

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

Just checking - does this get carried down to the menu element?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ah very good point. I have moved it to the List element and it's always true there since the menu list only shows when expanded.

/>
)}
</div>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
import React, { useState } from 'react';
import styled from 'styled-components';
import { ClickAwayListener } from '@material-ui/core';
import { useParams } from 'react-router-dom';
import { SearchBar } from './SearchBar';
import { EntityMenu } from './EntityMenu';
import { useParams } from 'react-router-dom';
import { useEntities, useProject } from '../../api/queries';

const Wrapper = styled.div`
Expand Down