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

feat: added helper methods to the context: Provider and Consumer #161

Merged
merged 1 commit into from
Jul 9, 2023
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
15 changes: 15 additions & 0 deletions __tests__/html-parser/__snapshots__/render-to-html.test.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,21 @@ exports[`renderToHTML should correctly render jsx with arrays in between element
</div>"
`;

exports[`renderToHTML should properly handle context data Provider and Consumer 1`] = `
"<html>
<body>
<div id=\\"root\\">
<div id=\\"1\\" class=\\"foo\\">no value</div>
<span id=\\"span-1\\">no value</span>
<div id=\\"2\\" class=\\"foo\\">FOO</div>
<span id=\\"span-2\\">FOO</span>
<div id=\\"3\\" class=\\"foo\\">BAR</div>
<span id=\\"span-3\\">BAR</span>
</div>
</body>
</html>"
`;

exports[`renderToHTML should properly handle context data close-by providers should not interfere with each other 1`] = `
"<html>
<body>
Expand Down
44 changes: 44 additions & 0 deletions __tests__/html-parser/render-to-html.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -798,6 +798,50 @@ describe("renderToHTML", () => {

expect(html).toMatchSnapshot();
});

it("Provider and Consumer", () => {
const MagicalContext = defineContext<string>();

const Foo: JSXTE.Component<{ id: string }> = (props, { ctx }) => {
const value = ctx.get(MagicalContext) ?? "no value";
return (
<div id={props.id} class="foo">
{value}
</div>
);
};

const App = () => {
return (
<html>
<body>
<div id="root">
<Foo id={"1"} />
<MagicalContext.Consumer
render={(v) => <span id="span-1">{v ?? "no value"}</span>}
/>
<MagicalContext.Provider value="FOO">
<Foo id={"2"} />
<MagicalContext.Consumer
render={(v) => <span id="span-2">{v}</span>}
/>
<MagicalContext.Provider value="BAR">
<Foo id={"3"} />
<MagicalContext.Consumer
render={(v) => <span id="span-3">{v}</span>}
/>
</MagicalContext.Provider>
</MagicalContext.Provider>
</div>
</body>
</html>
);
};

const html = renderToHtml(<App />);

expect(html).toMatchSnapshot();
});
});

describe("ErrorBoundary", () => {
Expand Down
21 changes: 21 additions & 0 deletions src/component-api/component-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
jsxElemToHtmlSync,
type RendererInternalOptions,
} from "../html-parser/jsx-elem-to-html";
import { jsx } from "../jsx-runtime";

export class ContextAccessor {
public static clone(original: ContextAccessor): ContextAccessor {
Expand Down Expand Up @@ -146,6 +147,26 @@ export class ComponentApi {

export class ContextDefinition<T> {
id = Symbol();

Provider = (
props: JSXTE.PropsWithChildren<{
value: T;
}>,
componentApi: ComponentApi
) => {
componentApi.ctx.set(this, props.value);
return jsx("", { children: props.children });
};

Consumer = (
props: JSXTE.PropsWithChildren<{
render: (value?: T) => JSX.Element;
}>,
componentApi: ComponentApi
) => {
const value = componentApi.ctx.get(this);
return props.render(value);
};
}

export const defineContext = <T = unknown>() => new ContextDefinition<T>();