-
-
Notifications
You must be signed in to change notification settings - Fork 113
feat: add jsxBind
function to @preact/signals
#724
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
Open
aleclarson
wants to merge
9
commits into
preactjs:main
Choose a base branch
from
aleclarson:feat/jsx-bind
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
04a49ab
feat: add `bind()` function for inline computeds
aleclarson eab99b1
chore: rename to jsxBind
aleclarson c949987
chore(refactor): remove `JSXBind` component and `jsxBindPrototype` ob…
aleclarson 27706a1
chore: remove unused import
aleclarson 66d676a
use || operator
aleclarson d58a8aa
Update packages/preact/src/index.ts
aleclarson 4d14f3d
chore: fix indent size
aleclarson a4cb42a
chore(test): check that text ⇄ element toggling works as expected
aleclarson 4604dd9
chore: add changset
aleclarson 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
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,5 @@ | ||
--- | ||
"@preact/signals": minor | ||
--- | ||
|
||
Introduce the `jsxBind` function for inlined `computed` declarations as JSX attributes or JSX children. |
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 |
---|---|---|
@@ -1,4 +1,5 @@ | ||
import { | ||
jsxBind, | ||
computed, | ||
useComputed, | ||
useSignalEffect, | ||
|
@@ -724,6 +725,159 @@ describe("@preact/signals", () => { | |
}); | ||
}); | ||
|
||
describe("jsxBind", () => { | ||
it("should bind a callback to a JSX attribute", async () => { | ||
const count = signal(0); | ||
const double = signal(2); | ||
const spy = sinon.spy(); | ||
|
||
function App() { | ||
spy(); | ||
return ( | ||
<div data-value={jsxBind(() => count.value * double.value)}></div> | ||
); | ||
} | ||
|
||
render(<App />, scratch); | ||
expect(spy).to.have.been.calledOnce; | ||
expect(scratch.innerHTML).to.equal('<div data-value="0"></div>'); | ||
|
||
act(() => { | ||
count.value = 5; | ||
}); | ||
|
||
// Component should not re-render when only the bound value changes | ||
expect(spy).to.have.been.calledOnce; | ||
expect(scratch.innerHTML).to.equal('<div data-value="10"></div>'); | ||
|
||
act(() => { | ||
double.value = 3; | ||
}); | ||
|
||
expect(spy).to.have.been.calledOnce; | ||
expect(scratch.innerHTML).to.equal('<div data-value="15"></div>'); | ||
}); | ||
|
||
it("should bind a callback to a JSX child", async () => { | ||
const firstName = signal("John"); | ||
const lastName = signal("Doe"); | ||
const spy = sinon.spy(); | ||
|
||
function App() { | ||
spy(); | ||
return ( | ||
<div>{jsxBind(() => `${firstName.value} ${lastName.value}`)}</div> | ||
); | ||
} | ||
|
||
render(<App />, scratch); | ||
expect(spy).to.have.been.calledOnce; | ||
expect(scratch.innerHTML).to.equal("<div>John Doe</div>"); | ||
|
||
act(() => { | ||
firstName.value = "Jane"; | ||
}); | ||
|
||
// Component should not re-render when only the bound value changes | ||
expect(spy).to.have.been.calledOnce; | ||
expect(scratch.innerHTML).to.equal("<div>Jane Doe</div>"); | ||
}); | ||
|
||
it("should update bound values without re-rendering the component", async () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we add a test where we first return JSX and then text or something, similar to the normal SignalValue component There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done 👍 |
||
const count = signal(0); | ||
const enabled = signal(true); | ||
const renderSpy = sinon.spy(); | ||
const boundSpy = sinon.spy(() => | ||
enabled.value ? count.value : "disabled" | ||
); | ||
|
||
function App() { | ||
renderSpy(); | ||
return ( | ||
<button disabled={jsxBind(() => !enabled.value)}> | ||
{jsxBind(boundSpy)} | ||
</button> | ||
); | ||
} | ||
|
||
render(<App />, scratch); | ||
expect(renderSpy).to.have.been.calledOnce; | ||
expect(boundSpy).to.have.been.called; | ||
expect(scratch.innerHTML).to.equal("<button>0</button>"); | ||
|
||
act(() => { | ||
count.value = 5; | ||
}); | ||
|
||
expect(renderSpy).to.have.been.calledOnce; | ||
expect(boundSpy).to.have.been.calledTwice; | ||
expect(scratch.innerHTML).to.equal("<button>5</button>"); | ||
|
||
act(() => { | ||
enabled.value = false; | ||
}); | ||
|
||
expect(renderSpy).to.have.been.calledOnce; | ||
expect(scratch.innerHTML).to.equal( | ||
`<button disabled="">disabled</button>` | ||
); | ||
}); | ||
|
||
it("can toggle between JSX text and JSX element", async () => { | ||
const bold = signal(false); | ||
const label = signal("Hello"); | ||
const renderSpy = sinon.spy(); | ||
|
||
function App() { | ||
renderSpy(); | ||
return ( | ||
<div> | ||
{jsxBind(() => | ||
bold.value ? <strong>{label.value}</strong> : label.value | ||
)} | ||
</div> | ||
); | ||
} | ||
|
||
render(<App />, scratch); | ||
expect(renderSpy).to.have.been.calledOnce; | ||
expect(scratch.innerHTML).to.equal("<div>Hello</div>"); | ||
|
||
// Text-to-text update. | ||
act(() => { | ||
label.value = "Bonjour"; | ||
}); | ||
|
||
expect(renderSpy).to.have.been.calledOnce; | ||
expect(scratch.innerHTML).to.equal("<div>Bonjour</div>"); | ||
|
||
// Text-to-element update. | ||
act(() => { | ||
bold.value = true; | ||
}); | ||
|
||
expect(renderSpy).to.have.been.calledOnce; | ||
expect(scratch.innerHTML).to.equal("<div><strong>Bonjour</strong></div>"); | ||
|
||
// Element-to-element update. | ||
act(() => { | ||
label.value = "Pryvit"; | ||
}); | ||
|
||
expect(renderSpy).to.have.been.calledOnce; | ||
expect(scratch.innerHTML).to.equal("<div><strong>Pryvit</strong></div>"); | ||
|
||
// Element-to-text update. | ||
act(() => { | ||
label.value = "Hola"; | ||
bold.value = false; | ||
}); | ||
|
||
expect(renderSpy).to.have.been.calledOnce; | ||
expect(scratch.innerHTML).to.equal("<div>Hola</div>"); | ||
}); | ||
}); | ||
|
||
describe("hooks mixed with signals", () => { | ||
it("signals should not stop context from propagating", () => { | ||
const ctx = createContext({ test: "should-not-exist" }); | ||
|
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.