Skip to content

Commit 1791d59

Browse files
committed
feat(NavigationTree): explicitly specify expandable nodes
1 parent babb05b commit 1791d59

File tree

5 files changed

+32
-20
lines changed

5 files changed

+32
-20
lines changed

src/components/NavigationTree/NavigationTree.tsx

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React from 'react';
22

33
import {NavigationTreeProps} from './types';
4-
import {reducer, getDefaultNodeState} from './state';
4+
import {reducer, getNodeState} from './state';
55
import {NavigationTreeNode} from './NavigationTreeNode';
66
import {NavigationTreeDirectory} from './NavigationTreeDirectory';
77

@@ -16,10 +16,7 @@ export function NavigationTree({
1616
cache = true,
1717
}: NavigationTreeProps) {
1818
const [state, dispatch] = React.useReducer(reducer, {
19-
[partialRootState.path]: {
20-
...getDefaultNodeState(),
21-
...partialRootState,
22-
},
19+
[partialRootState.path]: getNodeState(partialRootState),
2320
});
2421
const rootState = state[partialRootState.path];
2522

src/components/NavigationTree/NavigationTreeDirectory.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ export function NavigationTreeDirectory({
7070
const childPath = `${path}/${childName}`;
7171
let children;
7272

73-
if (state[childPath].type === 'directory') {
73+
if (state[childPath].expandable) {
7474
children = (
7575
<NavigationTreeDirectory
7676
state={state}

src/components/NavigationTree/NavigationTreeNode.tsx

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,6 @@ function renderIcon(type: NavigationTreeNodeType | string, collapsed: boolean) {
3232
}
3333
}
3434

35-
function isArrowVisible(type: NavigationTreeNodeType | string) {
36-
return type === 'database' || type === 'directory';
37-
}
38-
3935
export function NavigationTreeNode({
4036
path,
4137
state,
@@ -68,7 +64,7 @@ export function NavigationTreeNode({
6864
collapsed={nodeState.collapsed}
6965
active={active}
7066
actions={actions}
71-
hasArrow={isArrowVisible(nodeState.type)}
67+
hasArrow={nodeState.expandable}
7268
onClick={handleClick}
7369
onArrowClick={handleArrowClick}
7470
>

src/components/NavigationTree/state.ts

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
import {NavigationTreeState} from './types';
1+
import {
2+
NavigationTreeNodeState,
3+
NavigationTreeNodePartialState,
4+
NavigationTreeState,
5+
} from './types';
26

37
export enum NavigationTreeActionType {
48
ToggleCollapsed = 'toggle-collapsed',
@@ -21,6 +25,16 @@ export function getDefaultNodeState() {
2125
};
2226
}
2327

28+
export function getNodeState(
29+
partialState: NavigationTreeNodePartialState,
30+
): NavigationTreeNodeState {
31+
return {
32+
...getDefaultNodeState(),
33+
expandable: partialState.type === 'database' || partialState.type === 'directory',
34+
...partialState,
35+
};
36+
}
37+
2438
export function reducer(state: NavigationTreeState = {}, action: NavigationTreeAction) {
2539
switch (action.type) {
2640
case NavigationTreeActionType.ToggleCollapsed:
@@ -60,21 +74,17 @@ export function reducer(state: NavigationTreeState = {}, action: NavigationTreeA
6074

6175
for (const item of action.payload.data) {
6276
const path = `${action.payload.path}/${item.name}`;
63-
const name = item.name;
64-
const type = item.type;
6577

6678
// expand the tree according to the active path
6779
// prioritize the existing state to expand the tree only on first load
6880
const {activePath = ''} = action.payload;
6981
const collapsed = state[path]?.collapsed ?? !activePath.startsWith(`${path}/`);
7082

71-
newState[path] = {
72-
...getDefaultNodeState(),
83+
newState[path] = getNodeState({
84+
...item,
7385
collapsed,
7486
path,
75-
name,
76-
type,
77-
};
87+
});
7888
}
7989
}
8090

src/components/NavigationTree/types.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ export type NavigationTreeNodeType = 'database' | 'directory' | 'table';
55
export interface NavigationTreeDataItem {
66
name: string;
77
type: NavigationTreeNodeType;
8+
/** determined by type by default */
9+
expandable?: boolean;
810
}
911

1012
export interface NavigationTreeState {
@@ -15,15 +17,22 @@ export interface NavigationTreeNodeState {
1517
path: string;
1618
name: string;
1719
type: NavigationTreeNodeType;
20+
/** determined by type by default */
21+
expandable?: boolean;
1822
collapsed: boolean;
1923
loading: boolean;
2024
loaded: boolean;
2125
error: boolean;
2226
children: string[];
2327
}
2428

29+
export type NavigationTreeNodePartialState = Omit<
30+
NavigationTreeNodeState,
31+
'loading' | 'loaded' | 'error' | 'children'
32+
>;
33+
2534
export interface NavigationTreeProps<D = any> {
26-
rootState: Omit<NavigationTreeNodeState, 'loading' | 'loaded' | 'error' | 'children'>;
35+
rootState: NavigationTreeNodePartialState;
2736
fetchPath: (path: string) => Promise<NavigationTreeDataItem[]>;
2837
getActions?: (path: string, type: NavigationTreeNodeType) => DropdownMenuItemMixed<D>[];
2938
activePath?: string;

0 commit comments

Comments
 (0)