Skip to content

Commit

Permalink
fix: use pascalCase Slice type for <SliceZone> resolver (#109)
Browse files Browse the repository at this point in the history
  • Loading branch information
angeloashmore committed Jan 13, 2022
1 parent 33dea53 commit a2f2c23
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 19 deletions.
39 changes: 30 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Expand Up @@ -82,6 +82,7 @@
"siroc": "^0.16.0",
"standard-version": "^9.3.2",
"ts-eager": "^2.0.2",
"type-fest": "^2.9.0",
"typescript": "^4.5.4"
},
"peerDependencies": {
Expand Down
8 changes: 5 additions & 3 deletions src/SliceZone.tsx
@@ -1,7 +1,9 @@
import * as React from "react";
import * as prismicT from "@prismicio/types";
import type { PascalCase } from "type-fest";

import { __PRODUCTION__ } from "./lib/__PRODUCTION__";
import { pascalCase } from "./lib/pascalCase";

/**
* The minimum required properties to represent a Prismic Slice for the
Expand Down Expand Up @@ -148,7 +150,7 @@ type SliceZoneResolverArgs<TSlice extends SliceLike = SliceLike> = {
/**
* The name of the Slice.
*/
sliceName: TSlice["slice_type"];
sliceName: PascalCase<TSlice["slice_type"]>;

/**
* The index of the Slice in the Slice Zone.
Expand Down Expand Up @@ -249,7 +251,7 @@ export const SliceZone = <TSlice extends SliceLike, TContext>({
if (resolver) {
const resolvedComp = resolver({
slice,
sliceName: slice.slice_type,
sliceName: pascalCase(slice.slice_type),
i: index,
});

Expand All @@ -270,7 +272,7 @@ export const SliceZone = <TSlice extends SliceLike, TContext>({
/>
);
});
}, [components, context, defaultComponent, slices]);
}, [components, context, defaultComponent, slices, resolver]);

return <>{renderedSlices}</>;
};
10 changes: 10 additions & 0 deletions src/lib/pascalCase.ts
@@ -0,0 +1,10 @@
import type { PascalCase } from "type-fest";

export const pascalCase = <Input>(input: string): PascalCase<Input> => {
const camelCased = input.replace(/(?:-|_)(\w)/g, (_, c) => {
return c ? c.toUpperCase() : "";
});

return (camelCased[0].toUpperCase() +
camelCased.slice(1)) as PascalCase<Input>;
};
35 changes: 28 additions & 7 deletions test/SliceZone.test.tsx
Expand Up @@ -172,18 +172,32 @@ test.skip("TODO component does not warn in production", () => {
});

test("renders components from a resolver function for backwards compatibility with next-slicezone", async (t) => {
const slices = [{ slice_type: "foo" }, { slice_type: "bar" }] as const;
const slices = [
{
slice_type: "foo_bar",
},
{
slice_type: "barFoo",
},
{
slice_type: "baz-qux",
},
] as const;

const resolver: SliceZoneResolver<typeof slices[number]> = ({
sliceName,
}) => {
switch (sliceName) {
case "foo": {
return (props) => <StringifySliceComponent id="foo" {...props} />;
case "FooBar": {
return (props) => <StringifySliceComponent id="foo_bar" {...props} />;
}

case "BarFoo": {
return (props) => <StringifySliceComponent id="barFoo" {...props} />;
}

case "bar": {
return (props) => <StringifySliceComponent id="bar" {...props} />;
case "BazQux": {
return (props) => <StringifySliceComponent id="baz-qux" {...props} />;
}
}
};
Expand All @@ -192,19 +206,26 @@ test("renders components from a resolver function for backwards compatibility wi
const expected = renderJSON(
<>
<StringifySliceComponent
id="foo"
id="foo_bar"
slice={slices[0]}
index={0}
slices={slices}
context={{}}
/>
<StringifySliceComponent
id="bar"
id="barFoo"
slice={slices[1]}
index={1}
slices={slices}
context={{}}
/>
<StringifySliceComponent
id="baz-qux"
slice={slices[2]}
index={2}
slices={slices}
context={{}}
/>
</>,
);

Expand Down

0 comments on commit a2f2c23

Please sign in to comment.