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

allow to return usevalue directly from components #54

Merged
merged 1 commit into from
Jun 9, 2024
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
43 changes: 42 additions & 1 deletion integration-tests/create-element.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import { screen } from "@testing-library/dom";
import userEvent from "@testing-library/user-event";

import { attachComponent, createElement } from "../src";
import {
attachComponent,
createElement,
createState,
type State,
} from "../src";

describe("createElement", () => {
let cleanup: Function | undefined;
Expand Down Expand Up @@ -156,4 +161,40 @@ describe("createElement", () => {

expect(screen.getByTestId("container").textContent).toBe("text 5");
});

test("support returning useValue directly from a component", async () => {
const user = userEvent.setup();
function App() {
const showState = createState(false);
return createElement("div", {
children: [
createElement("h1", { children: ["parent component"] }),
createElement("button", {
"data-testid": "button",
onClick: () => showState.setValue(true),
}),
createElement(StateComponent, { showState }),
],
});
}

function StateComponent({ showState }: { showState: State<boolean> }) {
return showState.useValue((shouldShowState) =>
shouldShowState
? createElement("div", {
"data-testid": "stateComponent",
children: "state component",
})
: null
);
}
cleanup = attachComponent({
htmlElement: document.body,
component: createElement(App),
});

expect(screen.queryByTestId("stateComponent")).not.toBeInTheDocument();
await user.click(screen.getByTestId("button"));
expect(screen.getByTestId("stateComponent")).toBeInTheDocument();
});
});
2 changes: 1 addition & 1 deletion src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export type ComponentAPI = {
export type ComponentFunction = (
props: VelesElementProps,
componentAPI: ComponentAPI
) => VelesElement | VelesComponent | string | null;
) => VelesElement | VelesComponent | VelesStringElement | string | null;

export type AttributeHelper<T> = {
(htmlElement: HTMLElement, attributeName: string, node: VelesElement): T;
Expand Down