Skip to content

Commit d71901e

Browse files
authored
Merge pull request #33 from mikeytown19/issue/21
feat: can now pass empty value to snippet
2 parents 432a12f + c113305 commit d71901e

File tree

5 files changed

+80
-34
lines changed

5 files changed

+80
-34
lines changed

example/src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import Playground from "@agney/playground";
77
const App = () => {
88
const snippet = {
99
markup: `<div id=app />`,
10-
css: ``,
10+
css: `div { color: red }`,
1111
javascript: `import { h, Component, render } from 'preact';
1212
import htm from 'htm';
1313

playground/src/Editor/index.tsx

Lines changed: 33 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React, { FC, useMemo } from "react";
2-
import {styled} from "goober";
2+
import { styled } from "goober";
33
import { IEditorTabs, ISnippet } from "../types";
44
import EditorSetup from "./EditorSetup";
55
import { ITabConfig } from "../types";
@@ -12,7 +12,7 @@ import {
1212
} from "../TabStyles";
1313

1414
const TabContainer = styled(StyledTabs)`
15-
min-width: ${props => props.theme.container.minWidth};
15+
min-width: ${(props) => props.theme.container.minWidth};
1616
`;
1717

1818
interface IProps {
@@ -23,34 +23,44 @@ interface IProps {
2323
}
2424

2525
const Editor: FC<IProps> = ({ code, defaultTab, onChange, width }) => {
26-
const tabs: Readonly<ITabConfig<IEditorTabs>[]> = useMemo(
27-
() => [
28-
{ name: "HTML", value: "markup" },
29-
{ name: "CSS", value: "css" },
30-
{ name: "JS", value: "javascript" },
31-
],
32-
[]
33-
);
26+
const tabs: Readonly<ITabConfig<IEditorTabs>[]> = useMemo(() => {
27+
const tabsArr = [];
28+
if (code.markup) {
29+
tabsArr.push({ name: "HTML", value: "markup", code: code.markup });
30+
}
31+
if (code.css) {
32+
tabsArr.push({ name: "CSS", value: "css", code: code.css });
33+
}
34+
if (code.javascript) {
35+
tabsArr.push({ name: "JS", value: "javascript", code: code.javascript });
36+
}
37+
return tabsArr;
38+
}, []);
3439
return (
3540
<TabContainer
36-
defaultIndex={tabs.findIndex(tab => tab.value === defaultTab)}
41+
defaultIndex={tabs.findIndex(
42+
(tab) => tab.code && tab.value === defaultTab
43+
)}
3744
style={{ width: width }}
3845
>
3946
<StyledTabList>
40-
{tabs.map(tab => (
41-
<StyledTab key={tab.value}>{tab.name}</StyledTab>
42-
))}
47+
{tabs.map(
48+
(tab) => tab.code && <StyledTab key={tab.value}>{tab.name}</StyledTab>
49+
)}
4350
</StyledTabList>
4451
<StyledTabPanels>
45-
{tabs.map(tab => (
46-
<StyledTabPanel key={tab.value}>
47-
<EditorSetup
48-
code={code[tab.value]}
49-
onChange={onChange}
50-
language={tab.value}
51-
/>
52-
</StyledTabPanel>
53-
))}
52+
{tabs.map(
53+
(tab) =>
54+
tab.code && (
55+
<StyledTabPanel key={tab.value}>
56+
<EditorSetup
57+
code={tab.code}
58+
onChange={onChange}
59+
language={tab.value}
60+
/>
61+
</StyledTabPanel>
62+
)
63+
)}
5464
</StyledTabPanels>
5565
</TabContainer>
5666
);

playground/src/Result/index.tsx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,20 +31,20 @@ const Result: FC<IProps> = ({
3131
const [logs, setLogs] = useState<unknown[]>([]);
3232
const tabs: Readonly<ITabConfig<IResultTabs>[]> = useMemo(
3333
() => [
34-
{ name: "Result", value: "result" },
35-
{ name: "Console", value: "console" },
34+
{ name: "Result", value: "result" as IResultTabs },
35+
{ name: "Console", value: "console" as IResultTabs },
3636
],
3737
[]
3838
);
3939
useEffect(() => {
4040
function waitForMessage() {
4141
if (typeof window !== "undefined") {
42-
window.addEventListener("message", data => {
42+
window.addEventListener("message", (data) => {
4343
if (
4444
data.data.source === `frame-${id}` &&
4545
data.data.message.type === "log"
4646
) {
47-
setLogs(prevLogs => [...prevLogs, ...data.data.message.data]);
47+
setLogs((prevLogs) => [...prevLogs, ...data.data.message.data]);
4848
}
4949
});
5050
}
@@ -53,11 +53,11 @@ const Result: FC<IProps> = ({
5353
}, [id]);
5454
return (
5555
<StyledTabs
56-
defaultIndex={tabs.findIndex(tab => tab.value === defaultTab)}
56+
defaultIndex={tabs.findIndex((tab) => tab.value === defaultTab)}
5757
style={{ width: width }}
5858
>
5959
<StyledTabList>
60-
{tabs.map(tab => (
60+
{tabs.map((tab) => (
6161
<StyledTab key={tab.value}>{tab.name}</StyledTab>
6262
))}
6363
</StyledTabList>

playground/src/__tests__/Editor.test.tsx

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,24 @@ import { render } from "../../scripts/test-utils";
33

44
import Editor from "../Editor";
55

6-
const initialSnippet = {
7-
markup: ``,
6+
const emptySnippet = {
7+
markup: `<div id=app />`,
88
css: ``,
9-
javascript: ``,
9+
javascript: `import htm from 'htm'`,
10+
};
11+
12+
const snippet = {
13+
markup: `<div id=app />`,
14+
css: `div {color: red;}`,
15+
javascript: `import { h, Component, render } from 'preact';
16+
import htm from 'htm';
17+
18+
const html = htm.bind(h);
19+
20+
const app = html\`<div>Hello World from Playground!</div>\`
21+
22+
render(app, document.getElementById('app'));
23+
`,
1024
};
1125

1226
describe("Editor", () => {
@@ -15,12 +29,33 @@ describe("Editor", () => {
1529
const { getByText } = render(
1630
<Editor
1731
width={40}
18-
code={initialSnippet}
32+
code={snippet}
1933
defaultTab={defaultTab}
2034
onChange={() => {}}
2135
/>
2236
);
2337
const button = getByText("CSS");
2438
expect(button.getAttribute("data-selected")).toBe("");
2539
});
40+
41+
it("should render only the tabs that provided code", () => {
42+
const defaultTab = "markup";
43+
const { getByText, queryByText } = render(
44+
<Editor
45+
width={40}
46+
code={emptySnippet}
47+
defaultTab={defaultTab}
48+
onChange={() => {}}
49+
/>
50+
);
51+
52+
const htmlTab = getByText("HTML");
53+
const javascriptTab = getByText("JS");
54+
55+
expect(htmlTab).toBeTruthy();
56+
expect(javascriptTab).toBeTruthy();
57+
58+
const cssTab = queryByText("CSS");
59+
expect(cssTab).toBeFalsy();
60+
});
2661
});

playground/src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export type IEditorTabs = "markup" | "css" | "javascript";
88
export type IResultTabs = "result" | "console";
99

1010
export interface ITabConfig<T> {
11+
code?: string;
1112
name: string;
1213
value: T;
1314
}

0 commit comments

Comments
 (0)