Repository navigation
fix: don't use onCleanup or create primitives inside ref callbacks #1144
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
Merged
+1,619
−119
Merged
Changes from 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
2f75087
fix(form): don't use onCleanup or create primitives inside ref callbacks
davedbase 2fc6337
fix(form): keep per-element state when one bind() callback is attache…
davedbase bde4588
chore(form): shorten comments
davedbase 25cf1bc
fix(form): create the ownerless sync root detached from the ref callb…
davedbase 6457c24
fix: don't use onCleanup or create primitives inside ref callbacks (a…
davedbase c4d64be
fix: address CodeRabbit review on the ref-callback PR
davedbase 59c42d1
fix: stop test globals leaking between files and fix the JSR type check
davedbase aa40140
docs(mediastream): show a waiting state in the createScreen example
davedbase File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| --- | ||
| "@solid-primitives/form": patch | ||
| --- | ||
|
|
||
| Fix `form.ref` and `form.bind()` throwing `CLEANUP_IN_FORBIDDEN_SCOPE` (and `PRIMITIVE_IN_EFFECT_CALLBACK`) when used as JSX `ref` callbacks (#1143). | ||
|
|
||
| Solid 2 runs a `ref` callback in a restricted context where `onCleanup` and creating roots, memos or effects are forbidden. `ref` and `bind` registered their listeners with `makeEventListener` (which calls `onCleanup`) and `bind` created its DOM-sync effect inside the callback. In a hydrated app the throw aborted hydration, so the submit handler was never attached and a native GET submission put the fields, including passwords, into the URL. | ||
|
|
||
| Listeners are now plain `addEventListener` calls inside the ref callback, which still returns its cleanup. The field → DOM sync effect is created when `form.bind("name")` is evaluated (the component body), and the listeners are also torn down with the owner of `createForm` / `bind()`, because the runtime ignores a ref callback's return value. The unused `@solid-primitives/event-listener` dependency is removed. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,161 @@ | ||
| import { describe, it, expect, vi, afterEach } from "vitest"; | ||
| import { flush } from "solid-js"; | ||
| import { render } from "@solidjs/web"; | ||
| import { createForm } from "../src/index.js"; | ||
|
|
||
| // Regression tests for https://github.com/solidjs-community/solid-primitives/issues/1143 | ||
| // | ||
| // The other tests call `form.bind("x")(el)` / `form.ref(el)` directly, which bypasses the context | ||
| // Solid runs real JSX `ref` callbacks in. In that context Solid 2 forbids `onCleanup` | ||
| // (CLEANUP_IN_FORBIDDEN_SCOPE) and creating roots, memos or effects (PRIMITIVE_IN_EFFECT_CALLBACK): | ||
| // a ref callback may only return its cleanup. These tests mount the documented usage through the | ||
| // compiled JSX so those guards are active. | ||
|
|
||
| let disposers: Array<() => void> = []; | ||
|
|
||
| function mount(App: () => any) { | ||
| const container = document.createElement("div"); | ||
| document.body.appendChild(container); | ||
| const dispose = render(App, container); | ||
| const unmount = () => { | ||
| dispose(); | ||
| container.remove(); | ||
| }; | ||
| disposers.push(unmount); | ||
| return { container, unmount }; | ||
| } | ||
|
|
||
| afterEach(() => { | ||
| disposers.forEach(d => d()); | ||
| disposers = []; | ||
| }); | ||
|
|
||
| describe("createForm as JSX ref callbacks", () => { | ||
| it("mounts the documented README usage without throwing", () => { | ||
| expect(() => | ||
| mount(() => { | ||
| const form = createForm({ fields: { email: { initial: "" } } }); | ||
| return ( | ||
| <form ref={form.ref}> | ||
| <input ref={form.bind("email")} /> | ||
| <button type="submit">Submit</button> | ||
| </form> | ||
| ); | ||
| }), | ||
| ).not.toThrow(); | ||
| }); | ||
|
|
||
| it("intercepts the native submit event", async () => { | ||
| const onSubmit = vi.fn(); | ||
| const { container } = mount(() => { | ||
| const form = createForm({ | ||
| fields: { email: { initial: "a@b.c" } }, | ||
| onSubmit, | ||
| }); | ||
| return ( | ||
| <form ref={form.ref}> | ||
| <input ref={form.bind("email")} /> | ||
| </form> | ||
| ); | ||
| }); | ||
|
|
||
| const formEl = container.querySelector("form")!; | ||
| const event = new Event("submit", { cancelable: true, bubbles: true }); | ||
| formEl.dispatchEvent(event); | ||
| await Promise.resolve(); | ||
| flush(); | ||
|
|
||
| expect(event.defaultPrevented).toBe(true); | ||
| expect(onSubmit).toHaveBeenCalledTimes(1); | ||
| expect(onSubmit.mock.calls[0]![0]).toEqual({ email: "a@b.c" }); | ||
| }); | ||
|
|
||
| it("syncs the DOM to the field in both directions", () => { | ||
| let form!: ReturnType<typeof createForm<{ email: { initial: string } }>>; | ||
| const { container } = mount(() => { | ||
| form = createForm({ fields: { email: { initial: "start" } } }); | ||
| return ( | ||
| <form ref={form.ref}> | ||
| <input ref={form.bind("email")} /> | ||
| </form> | ||
| ); | ||
| }); | ||
| const input = container.querySelector("input")!; | ||
|
|
||
| // initial value is written synchronously by the ref callback | ||
| expect(input.value).toBe("start"); | ||
|
|
||
| // DOM -> field | ||
| input.value = "typed"; | ||
| input.dispatchEvent(new Event("input", { bubbles: true })); | ||
| flush(); | ||
| expect(form.fields.email.value()).toBe("typed"); | ||
|
|
||
| // field -> DOM (the sync effect is created in the component body, not in the ref callback) | ||
| form.fields.email.setValue("from-code"); | ||
| flush(); | ||
| expect(input.value).toBe("from-code"); | ||
| }); | ||
|
|
||
| it("tracks touched state on blur", () => { | ||
| let form!: ReturnType<typeof createForm<{ email: { initial: string } }>>; | ||
| const { container } = mount(() => { | ||
| form = createForm({ fields: { email: { initial: "" } } }); | ||
| return <input ref={form.bind("email")} />; | ||
| }); | ||
| const input = container.querySelector("input")!; | ||
|
|
||
| expect(form.fields.email.touched()).toBe(false); | ||
| input.dispatchEvent(new Event("blur")); | ||
| flush(); | ||
| expect(form.fields.email.touched()).toBe(true); | ||
| }); | ||
|
|
||
| it("supports checkable inputs", () => { | ||
| let form!: ReturnType<typeof createForm<{ agree: { initial: boolean } }>>; | ||
| const { container } = mount(() => { | ||
| form = createForm({ fields: { agree: { initial: false } } }); | ||
| return <input type="checkbox" ref={form.bind("agree")} />; | ||
| }); | ||
| const input = container.querySelector("input")!; | ||
|
|
||
| input.checked = true; | ||
| input.dispatchEvent(new Event("change", { bubbles: true })); | ||
| flush(); | ||
| expect(form.fields.agree.value()).toBe(true); | ||
|
|
||
| form.fields.agree.setValue(false); | ||
| flush(); | ||
| expect(input.checked).toBe(false); | ||
| }); | ||
|
|
||
| it("removes every listener when the ref is cleaned up", () => { | ||
| let form!: ReturnType<typeof createForm<{ email: { initial: string } }>>; | ||
| const onSubmit = vi.fn(); | ||
| const { container, unmount } = mount(() => { | ||
| form = createForm({ fields: { email: { initial: "" } }, onSubmit }); | ||
| return ( | ||
| <form ref={form.ref}> | ||
| <input ref={form.bind("email")} /> | ||
| </form> | ||
| ); | ||
| }); | ||
| const formEl = container.querySelector("form")!; | ||
| const input = container.querySelector("input")!; | ||
|
|
||
| unmount(); | ||
|
|
||
| // listeners are gone: nothing reaches the form state or the submit handler any more | ||
| input.value = "after-unmount"; | ||
| input.dispatchEvent(new Event("input", { bubbles: true })); | ||
| input.dispatchEvent(new Event("blur")); | ||
| const event = new Event("submit", { cancelable: true, bubbles: true }); | ||
| formEl.dispatchEvent(event); | ||
| flush(); | ||
|
|
||
| expect(form.fields.email.value()).toBe(""); | ||
| expect(form.fields.email.touched()).toBe(false); | ||
| expect(event.defaultPrevented).toBe(false); | ||
| expect(onSubmit).not.toHaveBeenCalled(); | ||
| }); | ||
| }); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.