-
Notifications
You must be signed in to change notification settings - Fork 392
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(file-explorer): adds property not to show hidden files (#488)
* fix 482: hidden-files-in-explorer * fix: SandpackFileExplorer show hidden files * refactor file-explorer and add tests * rename prop * adds property not to show hidden files * adds property not to show hidden files Co-authored-by: jerrywu <[email protected]> Co-authored-by: Danilo Woznica <[email protected]>
- Loading branch information
1 parent
6025c40
commit 1048fe9
Showing
8 changed files
with
294 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,55 @@ | ||
import type { SandpackBundlerFiles } from "@codesandbox/sandpack-client"; | ||
import * as React from "react"; | ||
|
||
import type { SandpackOptions } from "../.."; | ||
|
||
import { File } from "./File"; | ||
import { ModuleList } from "./ModuleList"; | ||
|
||
export interface Props { | ||
import type { SandpackFileExplorerProp } from "."; | ||
|
||
export interface Props extends SandpackFileExplorerProp { | ||
prefixedPath: string; | ||
files: SandpackBundlerFiles; | ||
selectFile: (path: string) => void; | ||
activeFile: string; | ||
activeFile: NonNullable<SandpackOptions["activeFile"]>; | ||
depth: number; | ||
visibleFiles: NonNullable<SandpackOptions["visibleFiles"]>; | ||
} | ||
|
||
interface State { | ||
open: boolean; | ||
} | ||
|
||
export class Directory extends React.Component<Props, State> { | ||
state = { | ||
open: true, | ||
}; | ||
|
||
toggleOpen = (): void => { | ||
this.setState((state) => ({ open: !state.open })); | ||
}; | ||
|
||
render(): React.ReactElement { | ||
const { prefixedPath, files, selectFile, activeFile, depth } = this.props; | ||
|
||
return ( | ||
<div key={prefixedPath}> | ||
<File | ||
depth={depth} | ||
isDirOpen={this.state.open} | ||
onClick={this.toggleOpen} | ||
path={prefixedPath + "/"} | ||
export const Directory: React.FC<Props> = ({ | ||
prefixedPath, | ||
files, | ||
selectFile, | ||
activeFile, | ||
depth, | ||
autoHiddenFiles, | ||
visibleFiles, | ||
}) => { | ||
const [open, setOpen] = React.useState(true); | ||
|
||
const toggle = (): void => setOpen((prev) => !prev); | ||
|
||
return ( | ||
<div key={prefixedPath}> | ||
<File | ||
depth={depth} | ||
isDirOpen={open} | ||
onClick={toggle} | ||
path={prefixedPath + "/"} | ||
/> | ||
|
||
{open && ( | ||
<ModuleList | ||
activeFile={activeFile} | ||
autoHiddenFiles={autoHiddenFiles} | ||
depth={depth + 1} | ||
files={files} | ||
prefixedPath={prefixedPath} | ||
selectFile={selectFile} | ||
visibleFiles={visibleFiles} | ||
/> | ||
|
||
{this.state.open && ( | ||
<ModuleList | ||
activeFile={activeFile} | ||
depth={depth + 1} | ||
files={files} | ||
prefixedPath={prefixedPath} | ||
selectFile={selectFile} | ||
/> | ||
)} | ||
</div> | ||
); | ||
} | ||
} | ||
)} | ||
</div> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 51 additions & 52 deletions
103
sandpack-react/src/components/FileExplorer/ModuleList.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,64 +1,63 @@ | ||
import type { SandpackBundlerFiles } from "@codesandbox/sandpack-client"; | ||
import * as React from "react"; | ||
|
||
import type { SandpackOptions } from "../../types"; | ||
|
||
import { Directory } from "./Directory"; | ||
import { File } from "./File"; | ||
import { fromPropsToModules } from "./utils"; | ||
|
||
import type { SandpackFileExplorerProp } from "."; | ||
|
||
export interface Props { | ||
export interface ModuleListProps extends SandpackFileExplorerProp { | ||
prefixedPath: string; | ||
files: SandpackBundlerFiles; | ||
selectFile: (path: string) => void; | ||
activeFile: string; | ||
activeFile: NonNullable<SandpackOptions["activeFile"]>; | ||
depth?: number; | ||
visibleFiles: NonNullable<SandpackOptions["visibleFiles"]>; | ||
} | ||
|
||
export class ModuleList extends React.PureComponent<Props> { | ||
render(): JSX.Element { | ||
const { | ||
depth = 0, | ||
activeFile, | ||
selectFile, | ||
prefixedPath, | ||
files, | ||
} = this.props; | ||
|
||
const fileListWithoutPrefix = Object.keys(files) | ||
.filter((file) => file.startsWith(prefixedPath)) | ||
.map((file) => file.substring(prefixedPath.length)); | ||
|
||
const directoriesToShow = new Set( | ||
fileListWithoutPrefix | ||
.filter((file) => file.includes("/")) | ||
.map((file) => `${prefixedPath}${file.split("/")[0]}/`) | ||
); | ||
|
||
const filesToShow = fileListWithoutPrefix | ||
.filter((file) => !file.includes("/")) | ||
.map((file) => ({ path: `${prefixedPath}${file}` })); | ||
|
||
return ( | ||
<div> | ||
{Array.from(directoriesToShow).map((dir) => ( | ||
<Directory | ||
key={dir} | ||
activeFile={activeFile} | ||
depth={depth} | ||
files={files} | ||
prefixedPath={dir} | ||
selectFile={selectFile} | ||
/> | ||
))} | ||
|
||
{filesToShow.map((file) => ( | ||
<File | ||
key={file.path} | ||
active={activeFile === file.path} | ||
depth={depth} | ||
path={file.path} | ||
selectFile={this.props.selectFile} | ||
/> | ||
))} | ||
</div> | ||
); | ||
} | ||
} | ||
export const ModuleList: React.FC<ModuleListProps> = ({ | ||
depth = 0, | ||
activeFile, | ||
selectFile, | ||
prefixedPath, | ||
files, | ||
autoHiddenFiles, | ||
visibleFiles, | ||
}) => { | ||
const { directories, modules } = fromPropsToModules({ | ||
visibleFiles, | ||
autoHiddenFiles, | ||
prefixedPath, | ||
files, | ||
}); | ||
|
||
return ( | ||
<div> | ||
{directories.map((dir) => ( | ||
<Directory | ||
key={dir} | ||
activeFile={activeFile} | ||
autoHiddenFiles={autoHiddenFiles} | ||
depth={depth} | ||
files={files} | ||
prefixedPath={dir} | ||
selectFile={selectFile} | ||
visibleFiles={visibleFiles} | ||
/> | ||
))} | ||
|
||
{modules.map((file) => ( | ||
<File | ||
key={file} | ||
active={activeFile === file} | ||
depth={depth} | ||
path={file} | ||
selectFile={selectFile} | ||
/> | ||
))} | ||
</div> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.