Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(filetab): not show files that don't exist #283

Merged
merged 1 commit into from
Dec 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions sandpack-react/src/presets/Playground.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Sandpack } from "./Sandpack";

export default {
title: "Playground",
component: Sandpack,
};

const code = `export default function App() {
return '12312312';
};
`;

export const Main = (): JSX.Element => {
return (
<Sandpack
customSetup={{ entry: "/index.tsx", main: "/App.tsx" }}
files={{ "/App.tsx": code }}
options={{ showTabs: true, openPaths: ["/index.tsx"] }}
template="react-ts"
/>
);
};
13 changes: 11 additions & 2 deletions sandpack-react/src/utils/sandpackUtils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
createSetupFromUserInput,
} from "./sandpackUtils";

describe("getSandpackStateFromProps", () => {
describe(getSandpackStateFromProps, () => {
test("always return an activeFile", () => {
const template = getSandpackStateFromProps({ template: "react" });
expect(template.activePath).toBe("/App.js");
Expand Down Expand Up @@ -110,9 +110,18 @@ describe("getSandpackStateFromProps", () => {

expect(output.activePath).toEqual("/App.js");
});

test("should not show invalid files into `openPaths`", () => {
const output = getSandpackStateFromProps({
template: "react",
openPaths: ["/App.js", "not-exist.js"],
});

expect(output.openPaths).toEqual(["/App.js"]);
});
});

describe("createSetupFromUserInput", () => {
describe(createSetupFromUserInput, () => {
test("convert `files` to a key/value format", () => {
const output = createSetupFromUserInput({ files: { "App.js": "" } });
expect(output).toStrictEqual({ files: { "App.js": { code: "" } } });
Expand Down
3 changes: 2 additions & 1 deletion sandpack-react/src/utils/sandpackUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,9 @@ export const getSandpackStateFromProps = (
);

const environment = projectSetup.environment;
const existOpenPath = openPaths.filter((file) => files[file]);

return { openPaths, activePath, files, environment };
return { openPaths: existOpenPath, activePath, files, environment };
};

// The template is predefined (eg: react, vue, vanilla)
Expand Down