Skip to content

Commit

Permalink
feat(NavigationTree): add additionalNodeElements to NavigationTree (#55)
Browse files Browse the repository at this point in the history
  • Loading branch information
krosy1337 authored Aug 8, 2023
1 parent 4d06d91 commit d462aaf
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/components/NavigationTree/NavigationTree.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export function NavigationTree({
rootState: partialRootState,
fetchPath,
getActions,
renderAdditionalNodeElements,
activePath,
onActivePathUpdate,
cache = true,
Expand All @@ -55,6 +56,7 @@ export function NavigationTree({
dispatch={dispatch}
onActivate={onActivePathUpdate}
getActions={getActions}
renderAdditionalNodeElements={renderAdditionalNodeElements}
cache={cache}
level={node.level}
/>
Expand Down
7 changes: 7 additions & 0 deletions src/components/NavigationTree/NavigationTreeNode.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export interface NavigationTreeNodeProps {
children?: React.ReactNode;
onActivate?: (path: string) => void;
getActions?: NavigationTreeProps['getActions'];
renderAdditionalNodeElements?: NavigationTreeProps['renderAdditionalNodeElements'];
cache?: boolean;
}

Expand Down Expand Up @@ -62,6 +63,7 @@ export function NavigationTreeNode({
children,
onActivate,
getActions,
renderAdditionalNodeElements,
cache,
}: NavigationTreeNodeProps) {
const nodeState = state[path];
Expand Down Expand Up @@ -112,6 +114,10 @@ export function NavigationTreeNode({
dispatch({type: NavigationTreeActionType.ToggleCollapsed, payload: {path}});
}, []);

const additionalNodeElements = React.useMemo(() => {
return renderAdditionalNodeElements?.(nodeState.path, nodeState.type);
}, [renderAdditionalNodeElements, nodeState]);

const actions = React.useMemo(() => {
return getActions?.(nodeState.path, nodeState.type);
}, [getActions, nodeState]);
Expand All @@ -123,6 +129,7 @@ export function NavigationTreeNode({
collapsed={nodeState.collapsed}
active={nodeState.path === activePath}
actions={actions}
additionalNodeElements={additionalNodeElements}
hasArrow={nodeState.expandable}
onClick={handleClick}
onArrowClick={handleArrowClick}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React from 'react';
import {Meta, Story} from '@storybook/react';
import {Button} from '@gravity-ui/uikit';
import {NavigationTreeDataItem, NavigationTreeNodeType} from '../types';
import {NavigationTree, NavigationTreeProps} from '../NavigationTree';

Expand All @@ -21,6 +22,7 @@ export const Default: Story<NavigationTreeProps> = () => {
}}
fetchPath={fetchPath}
getActions={getActions}
renderAdditionalNodeElements={renderAdditionalNodeElements}
activePath={activePath}
onActivePathUpdate={setActivePath}
/>
Expand Down Expand Up @@ -217,3 +219,33 @@ function getActions(path: string, type: NavigationTreeNodeType) {

return [];
}

function renderAdditionalNodeElements(path: string, type: NavigationTreeNodeType) {
if (type === 'directory') {
return (
<Button
onClick={() => {
alert(`Directory path is "${path}"`);
}}
size="s"
>
Show Directory
</Button>
);
}

if (type === 'table') {
return (
<Button
onClick={() => {
alert(`Table path is "${path}"`);
}}
size="s"
>
Show Table
</Button>
);
}

return undefined;
}
4 changes: 4 additions & 0 deletions src/components/NavigationTree/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ export interface NavigationTreeProps<D = any> {
rootState: NavigationTreeNodePartialState;
fetchPath: (path: string) => Promise<NavigationTreeDataItem[]>;
getActions?: (path: string, type: NavigationTreeNodeType) => DropdownMenuItemMixed<D>[];
renderAdditionalNodeElements?: (
path: string,
type: NavigationTreeNodeType,
) => JSX.Element | undefined;
activePath?: string;
onActivePathUpdate?: (activePath: string) => void;
cache?: boolean;
Expand Down
6 changes: 5 additions & 1 deletion src/components/TreeView/TreeView.scss
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ $step-offset: 24px;
background-color: var(--yc-color-base-simple-hover);

#{$root}__actions {
display: block;
display: flex;
}
}

Expand Down Expand Up @@ -75,6 +75,10 @@ $step-offset: 24px;
&__actions {
display: none;
margin-left: 6px;

align-items: center;

overflow: hidden;
}

.tree-view_arrow {
Expand Down
3 changes: 3 additions & 0 deletions src/components/TreeView/TreeView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export interface TreeViewProps {
onArrowClick?: () => void;
hasArrow?: boolean;
actions?: DropdownMenuItemMixed<any>[];
additionalNodeElements?: JSX.Element;
level?: number;
}

Expand All @@ -34,6 +35,7 @@ export function TreeView({
onArrowClick,
hasArrow = false,
actions,
additionalNodeElements,
level,
}: TreeViewProps) {
const rootRef = React.useRef<HTMLDivElement>(null);
Expand All @@ -46,6 +48,7 @@ export function TreeView({
</div>
{actions && actions.length > 0 && (
<div className={b('actions')}>
{additionalNodeElements}
<DropdownMenu
defaultSwitcherProps={{
view: 'flat-secondary',
Expand Down

0 comments on commit d462aaf

Please sign in to comment.