Skip to content

Commit cff3bf8

Browse files
authored
Merge pull request #1 from adierkens/remove-child
Support removing children
2 parents 19c010f + f6bc999 commit cff3bf8

File tree

3 files changed

+51
-3
lines changed

3 files changed

+51
-3
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
"prepare": "is-ci || husky install"
1919
},
2020
"files": [
21-
"src",
22-
"dist",
21+
"src/",
22+
"dist/",
2323
"logo.png"
2424
],
2525
"devDependencies": {

src/__tests__/render.test.tsx

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,4 +114,25 @@ describe("refs", () => {
114114
expect.assertions(2);
115115
expect(await render(element)).toStrictEqual("Foo");
116116
});
117+
118+
it("removes items from a container", async () => {
119+
const Custom = () => {
120+
const [show, setShow] = React.useState(false);
121+
122+
React.useEffect(() => {
123+
setShow(true);
124+
}, [show]);
125+
126+
return (
127+
<array>
128+
<value>1</value>
129+
{show && <value>2</value>}
130+
</array>
131+
);
132+
};
133+
134+
const element = <Custom />;
135+
136+
expect(await render(element)).toStrictEqual(["1", "2"]);
137+
});
117138
});

src/host-config.ts

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,32 @@ function createInstance<T extends keyof JsonElements>(
8585
}
8686
}
8787

88+
/** remove a child from the node */
89+
function removeChild(parent: JsonNode, child: JsonNode) {
90+
switch (parent.type) {
91+
case "array":
92+
parent.items = parent.items.filter((c) => c !== child);
93+
break;
94+
95+
case "object":
96+
parent.properties = parent.properties.filter((c) => c !== child);
97+
break;
98+
99+
case "property":
100+
if (parent.valueNode === child) {
101+
parent.valueNode = undefined;
102+
}
103+
104+
break;
105+
case "value":
106+
parent.items = parent.items.filter((c) => c !== child) as any;
107+
108+
break;
109+
default:
110+
throw new Error("Unknown type");
111+
}
112+
}
113+
88114
export const hostConfig: HostConfig<
89115
keyof JsonElements,
90116
any,
@@ -119,7 +145,8 @@ export const hostConfig: HostConfig<
119145
},
120146

121147
clearContainer: (parent: JsonNode) => {},
122-
148+
removeChildFromContainer: removeChild,
149+
removeChild,
123150
finalizeInitialChildren: () => false,
124151
prepareUpdate: () => null,
125152
shouldSetTextContent: () => false,

0 commit comments

Comments
 (0)