Skip to content

Commit 7782bdc

Browse files
authored
feat: add files via file tree (stackblitz#314)
1 parent 38df1f3 commit 7782bdc

File tree

30 files changed

+1192
-68
lines changed

30 files changed

+1192
-68
lines changed
Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,32 @@
1-
import { useState } from 'react';
1+
import { useState, type ComponentProps } from 'react';
22
import FileTree from '@tutorialkit/react/core/FileTree';
33

44
export default function ExampleFileTree() {
5-
const [selectedFile, setSelectedFile] = useState(FILES[0]);
5+
const [files, setFiles] = useState(INITIAL_FILES);
6+
const [selectedFile, setSelectedFile] = useState(INITIAL_FILES[0].path);
67

78
return (
89
<FileTree
9-
files={FILES}
10+
files={files}
1011
hideRoot
1112
className="my-file-tree"
1213
hiddenFiles={['package-lock.json']}
1314
selectedFile={selectedFile}
1415
onFileSelect={setSelectedFile}
16+
onFileChange={(event) => {
17+
if (event.method === 'add') {
18+
setFiles([...files, { path: event.value, type: event.type }]);
19+
}
20+
}}
1521
/>
1622
);
1723
}
1824

19-
const FILES = [
20-
'/src/index.js',
21-
'/src/index.html',
22-
'/src/assets/logo.svg',
23-
'/package-lock.json',
24-
'/package.json',
25-
'/vite.config.js',
25+
const INITIAL_FILES: ComponentProps<typeof FileTree>['files'] = [
26+
{ path: '/package-lock.json', type: 'file' },
27+
{ path: '/package.json', type: 'file' },
28+
{ path: '/src/assets/logo.svg', type: 'file' },
29+
{ path: '/src/index.html', type: 'file' },
30+
{ path: '/src/index.js', type: 'file' },
31+
{ path: '/vite.config.js', type: 'file' },
2632
];

docs/tutorialkit.dev/src/components/react-examples/ExampleSimpleEditor.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ const FILES: Record<string, EditorDocument> = {
227227
},
228228
};
229229

230-
const FILE_PATHS = Object.keys(FILES);
230+
const FILE_PATHS = Object.keys(FILES).map((path) => ({ path, type: 'file' }) as const);
231231

232232
function stripIndent(string: string) {
233233
const indent = minIndent(string.slice(1));

docs/tutorialkit.dev/src/content/docs/reference/configuration.mdx

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,34 @@ Defines which file should be opened in the [code editor](/guides/ui/#code-editor
134134
<PropertyTable inherited type="string" />
135135

136136
##### `editor`
137-
Configure whether or not the editor should be rendered. If an object is provided with `fileTree: false`, only the file tree is hidden.
138-
<PropertyTable inherited type="boolean | { fileTree: false }" />
137+
Configures options for the editor and its file tree. Editor can be hidden by providing `false`.
138+
Optionally you can hide just file tree by providing `fileTree: false`.
139+
140+
File tree can be set to allow file editing from right clicks by setting `fileTree.allowEdits: true`.
141+
142+
<PropertyTable inherited type={'Editor'} />
143+
144+
The `Editor` type has the following shape:
145+
146+
```ts
147+
type Editor =
148+
| false
149+
| { editor: { allowEdits: boolean } }
150+
151+
```
152+
153+
Example values:
154+
155+
```yaml
156+
editor: false # Editor is hidden
157+
158+
editor: # Editor is visible
159+
fileTree: false # File tree is hidden
160+
161+
editor: # Editor is visible
162+
fileTree: # File tree is visible
163+
allowEdits: true # User can add new files and folders from the file tree
164+
```
139165
140166
##### `previews`
141167
Configure which ports should be used for the previews allowing you to align the behavior with your demo application's dev server setup. If not specified, the lowest port will be used.

docs/tutorialkit.dev/src/content/docs/reference/react-components.mdx

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,12 +107,23 @@ A component to list files in a tree view.
107107

108108
* `onFileSelect: (file: string) => void` - A callback that will be called when a file is clicked. The path of the file that was clicked will be passed as an argument.
109109

110+
* `onFileChange: (event: FileChangeEvent) => void` - An optional callback that will be called when a new file or folder is created from the file tree's context menu. When callback is not passed, file tree does not allow adding new files.
111+
```ts
112+
interface FileChangeEvent {
113+
type: 'file' | 'folder';
114+
method: 'add' | 'remove' | 'rename';
115+
value: string;
116+
}
117+
```
118+
110119
* `hideRoot: boolean` - Whether or not to hide the root directory in the tree. Defaults to `false`.
111120

112121
* `hiddenFiles: (string | RegExp)[]` - A list of file paths that should be hidden from the tree.
113122

114123
* `scope?: string` - Every file path that does not start with this scope will be hidden.
115124

125+
* `i18n?: object` - Texts for file tree's components.
126+
116127
* `className?: string` - A class name to apply to the root element of the component.
117128

118129

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default 'File in first level';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default 'File in second level';
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
type: lesson
3+
title: Allow Edits Disabled
4+
previews: false
5+
terminal:
6+
panels: terminal
7+
---
8+
9+
# File Tree test - Allow Edits Disabled
10+
11+
Option `editor.fileTree.allowEdits` has default `false` value.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default 'File in first level';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default 'File in second level';
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
type: lesson
3+
title: Allow Edits Enabled
4+
previews: false
5+
editor:
6+
fileTree:
7+
allowEdits: true
8+
terminal:
9+
panels: terminal
10+
---
11+
12+
# File Tree test - Allow Edits Enabled

0 commit comments

Comments
 (0)