Skip to content

Commit

Permalink
feat: navigation tree item subtitle and default expanded state
Browse files Browse the repository at this point in the history
  • Loading branch information
gndz07 authored Mar 23, 2023
1 parent 9511631 commit 12d8294
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 6 deletions.
35 changes: 32 additions & 3 deletions components/NavigationTree/NavigationTree.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,13 @@ const Template: ComponentStory<typeof NavigationTreeContainer> = (args) => {
return (
<NavigationDrawer>
<NavigationTreeContainer {...args}>
<NavigationTreeItem {...navigationHandlerProps('one')} label="One">
<NavigationTreeItem {...navigationHandlerProps('one-one')} as="a" label="One.One" />
<NavigationTreeItem {...navigationHandlerProps('one')} label="One" subtitle="/one">
<NavigationTreeItem
{...navigationHandlerProps('one-one')}
as="a"
label="One.One"
subtitle="/one-one"
/>
<NavigationTreeItem {...navigationHandlerProps('one-two')} label="One.Two">
<NavigationTreeItem
{...navigationHandlerProps('one-two-one')}
Expand All @@ -55,7 +60,31 @@ const Template: ComponentStory<typeof NavigationTreeContainer> = (args) => {
/>
</NavigationTreeItem>
</NavigationTreeItem>
<NavigationTreeItem {...navigationHandlerProps('two')} label="Two" />
<NavigationTreeItem
{...navigationHandlerProps('two')}
label="Two"
subtitle="/two"
defaultExpanded
>
<NavigationTreeItem
{...navigationHandlerProps('two-one')}
as="a"
label="Two.One"
subtitle="/two-one"
/>
<NavigationTreeItem
{...navigationHandlerProps('two-two')}
label="Two.Two"
subtitle="/two-two"
>
<NavigationTreeItem
{...navigationHandlerProps('two-two-one')}
startAdornment={<ArchiveIcon />}
label="Two.Two.One"
/>
</NavigationTreeItem>
</NavigationTreeItem>
<NavigationTreeItem {...navigationHandlerProps('three')} label="Three" />
</NavigationTreeContainer>
</NavigationDrawer>
);
Expand Down
2 changes: 1 addition & 1 deletion components/NavigationTree/NavigationTreeItem.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ const Template: ComponentStory<typeof NavigationTreeItem> = (args) => {
defaultCollapseIcon={args.defaultCollapseIcon}
defaultExpandIcon={args.defaultExpandIcon}
>
<NavigationTreeItem {...navigationHandlerProps('one')} label="One">
<NavigationTreeItem {...navigationHandlerProps('one')} label="One" subtitle="/parent">
<NavigationTreeItem {...navigationHandlerProps('one-one')} {...args}>
<NavigationTreeItem
{...navigationHandlerProps('one-one-one')}
Expand Down
22 changes: 20 additions & 2 deletions components/NavigationTree/NavigationTreeItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@ import React, { useMemo, useState } from 'react';
import { Box } from '../Box';
import { NavigationItem, NavigationItemProps } from '../Navigation';
import { NavigationTreeContainer } from './NavigationTreeContainer';
import { Flex } from '../Flex';
import { Text } from '../Text';

export interface NavigationTreeItemProps {
label: string;
subtitle?: string;
children?: React.ReactNode;
defaultExpanded?: boolean;
onClick?: () => void;
defaultExpandIcon?: React.ReactNode;
defaultCollapseIcon?: React.ReactNode;
Expand All @@ -15,15 +19,17 @@ export interface NavigationTreeItemProps {

export const NavigationTreeItem = ({
label,
subtitle,
children,
onClick,
defaultExpanded = false,
defaultCollapseIcon,
defaultExpandIcon,
customCollapseIcon,
customExpandIcon,
...props
}: NavigationTreeItemProps & NavigationItemProps) => {
const [isExpanded, setIsExpanded] = useState(false);
const [isExpanded, setIsExpanded] = useState(defaultExpanded);
const isExpandable = useMemo(() => React.Children.count(children) > 0, [children]);
const hasStartAdornment = useMemo(() => !!props.startAdornment, [props.startAdornment]);
const usedStartAdornment = useMemo(
Expand Down Expand Up @@ -55,7 +61,19 @@ export const NavigationTreeItem = ({
startAdornment={usedStartAdornment}
onClick={isExpandable ? () => setIsExpanded(!isExpanded) : onClick}
>
<Box css={{ ml: isExpandable || hasStartAdornment ? 0 : '$4' }}>{label}</Box>
<Flex
direction="column"
align="start"
gap={1}
css={{ ml: isExpandable || hasStartAdornment ? 0 : '$4' }}
>
<Text>{label}</Text>
{subtitle && (
<Text variant="subtle" css={{ fontSize: '$3', opacity: 0.8 }}>
{subtitle}
</Text>
)}
</Flex>
</NavigationItem>
{isExpanded && (
<NavigationTreeContainer
Expand Down

0 comments on commit 12d8294

Please sign in to comment.