Skip to content

Commit e7b7cb6

Browse files
authored
Merge pull request #17 from Himenon/feat/add-full-path-params
feat: add full path params
2 parents 547f07f + 74f9058 commit e7b7cb6

File tree

5 files changed

+49
-10
lines changed

5 files changed

+49
-10
lines changed

packages/react/public/main.css

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,24 @@
2020
display: inline;
2121
}
2222

23+
.directory-item[data-open=false]::before {
24+
content: "+";
25+
padding-left: 4px;
26+
margin-right: 3px;
27+
display: inline-block;
28+
width: 1em;
29+
}
30+
31+
.directory-item[data-open=true]::before {
32+
content: "-";
33+
padding-left: 4px;
34+
margin-right: 3px;
35+
display: inline-block;
36+
width: 1em;
37+
}
38+
2339
.directory-item-name {
24-
display: block;
40+
display: inline-block;
2541
padding-bottom: 1px;
2642
}
2743

@@ -49,3 +65,14 @@
4965
background-color: #EEEEEE;
5066
cursor: pointer;
5167
}
68+
69+
.file-item::before {
70+
content: "";
71+
display: inline-block;
72+
padding-left: 4px;
73+
margin-right: 3px;
74+
width: 1em;
75+
height: 1em;
76+
background-image: url('data:image/svg+xml;charset=utf8,%3Csvg%20version%3D%221.1%22%20id%3D%22_x32_%22%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20x%3D%220px%22%20y%3D%220px%22%20viewBox%3D%220%200%20512%20512%22%20style%3D%22width%3A%20256px%3B%20height%3A%20256px%3B%20opacity%3A%201%3B%22%20xml%3Aspace%3D%22preserve%22%3E%3Cstyle%20type%3D%22text%2Fcss%22%3E%20.st0%7Bfill%3A%234B4B4B%3B%7D%3C%2Fstyle%3E%3Cg%3E%20%3Cpath%20class%3D%22st0%22%20d%3D%22M198.765%2C0L53.398%2C145.383V512h405.204V0H198.765z%20M196.634%2C49.667v93.576h-93.577L196.634%2C49.667z%20M424.995%2C478.393H87.005V172.897h139.29V33.614h198.7V478.393z%22%20style%3D%22fill%3A%20rgb(75%2C%2075%2C%2075)%3B%22%3E%3C%2Fpath%3E%3C%2Fg%3E%3C%2Fsvg%3E');
77+
background-repeat: no-repeat;
78+
}

packages/react/src/Directory.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as React from "react";
22

33
export interface Props {
4+
name: string;
45
path: string;
56
level: number;
67
}
@@ -10,10 +11,10 @@ export type ComponentType = React.ComponentType<Props>;
1011
export const Component: ComponentType = (props) => {
1112
const [isActive, toggle] = React.useState(false);
1213
return (
13-
<ul className="tree-item directory" key={props.path}>
14-
<li className="directory-item" key={props.path}>
14+
<ul className="tree-item directory" key={props.name}>
15+
<li className="directory-item" key={props.name} data-open={isActive}>
1516
<span className="directory-item-name" aria-pressed={isActive} onClick={() => toggle(!isActive)}>
16-
{props.path}
17+
{props.name}
1718
</span>
1819
{isActive && props.children}
1920
</li>

packages/react/src/DirectoryTree.tsx

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,11 @@ const createTreeComponent = (
2121
} else {
2222
visited.push(edge);
2323
}
24-
const [type, name] = edge.split(":");
24+
const [type, fullPath] = edge.split(":");
2525
if (type === "file") {
2626
const props: File.Props = {
27-
path: basename(name),
27+
path: fullPath,
28+
name: basename(fullPath),
2829
level,
2930
};
3031
return <FileComponent key={edge} {...props} />;
@@ -33,10 +34,11 @@ const createTreeComponent = (
3334
return createTreeComponent(childEdge, treeData, visited, { FileComponent, DirectoryComponent }, level + 1);
3435
});
3536
const props: Directory.Props = {
36-
path: basename(name),
37+
path: fullPath,
38+
name: basename(fullPath),
3739
level,
3840
};
39-
if (props.path === ".") {
41+
if (props.name === ".") {
4042
return <React.Fragment key={`level-${level}`}>{children}</React.Fragment>;
4143
}
4244
return <DirectoryComponent key={edge} {...props} children={children} />;

packages/react/src/File.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@ import * as React from "react";
22

33
export interface Props {
44
path: string;
5+
name: string;
56
level: number;
67
}
78

89
export type ComponentType = React.ComponentType<Props>;
910

1011
export const Component: ComponentType = (props) => {
11-
return <p className="tree-item file-item">{props.path}</p>;
12+
return <p className="tree-item file-item">{props.name}</p>;
1213
};
1314

1415
Component.displayName = "File";

packages/react/src/Tree.tsx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export interface Props {
1313
DirectoryComponent?: Directory.ComponentType;
1414
}
1515

16-
export const Component: React.FC<Props> = (props) => {
16+
const useTree = (props: Props) => {
1717
const [{ pathItems: inputPathItems, FileComponent = File.Component, DirectoryComponent = Directory.Component }, updateProps] = React.useState(
1818
props,
1919
);
@@ -34,6 +34,14 @@ export const Component: React.FC<Props> = (props) => {
3434
return item;
3535
});
3636
const treeData = collect(pathItems);
37+
return {
38+
componentSet,
39+
treeData,
40+
};
41+
};
42+
43+
export const Component: React.FC<Props> = (props) => {
44+
const { treeData, componentSet } = useTree(props);
3745
return <DirectoryTree.Component treeData={treeData} componentSet={componentSet} />;
3846
};
3947

0 commit comments

Comments
 (0)