Skip to content

Commit

Permalink
improve doCallFunction could custom key
Browse files Browse the repository at this point in the history
  • Loading branch information
HillLiu committed Jun 18, 2024
1 parent a58ea38 commit ec0e028
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 8 deletions.
2 changes: 1 addition & 1 deletion packages/reshow-build/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "1.0.1",
"version": "1.1.0",
"name": "reshow-build",
"repository": {
"type": "git",
Expand Down
25 changes: 25 additions & 0 deletions packages/reshow-build/src/__tests__/buildFunctionTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,29 @@ describe("Test build with key", () => {
buildComp?.props.children[0].key !== buildComp?.props.children[1].key
).be.true;
});

it("with custom key", () => {
const buildComp = build(() => <div key="div-key" />, {
doCallFunction: true,
})({ key: "foo" });
expect(buildComp?.key).to.equal("div-key");
});

it("with custom key=null (for trace spec behavior)", () => {
const buildComp = build(() => <div key={null} />, { doCallFunction: true })(
{ key: "foo" }
);
expect(buildComp?.key).to.equal("null");
});

it("with custom key=undefined", () => {
const buildComp = build(() => <div />, { doCallFunction: true })({
key: "foo",
});
expect(buildComp?.key).to.equal("foo");
const buildComp2 = build(() => <div key={undefined} />, {
doCallFunction: true,
})({ key: "foo" });
expect(buildComp2?.key).to.equal("foo");
});
});
13 changes: 6 additions & 7 deletions packages/reshow-build/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,12 @@ const buildFunc = (component, props, child, componentOption) => {
props.children = /** @type React.ReactElement*/ (child);
}
const el = component(props);
return isValidElement(el)
? null !== props.key
? buildReact(el, { key: props.key })
: el
: altWrap
? buildReact(altWrap, props, el)
: buildReact(el, props);
if (isValidElement(el)) {
const elKey = el.key || props.key;
return null != elKey ? buildReact(el, { key: elKey }) : el;
} else {
return altWrap ? buildReact(altWrap, props, el) : buildReact(el, props);
}
} catch (e) {
if (e.name === TYPE_ERROR) {
return buildReact(component, props, child);
Expand Down

0 comments on commit ec0e028

Please sign in to comment.