-
-
Notifications
You must be signed in to change notification settings - Fork 2k
fix(#2644): adapt the react children api #2645
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
deleonio
wants to merge
9
commits into
preactjs:main
Choose a base branch
from
deleonio:fix/#2644
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
867fd81
fix(#2644): adapt the react children api
deleonio bda3f94
docs: add type annotation for d.ts
deleonio b1f4604
fix type annotation
deleonio e5a3a86
adapt tests
deleonio cd92f41
fix the context handling / this
deleonio a507b84
test: revert the changes
deleonio 9b6fda2
Merge branch 'master' into fix/#2644
8367c16
add a test
deleonio 8265ed8
Merge branch 'master' into fix/#2644
deleonio 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
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 | ||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -56,14 +56,14 @@ describe('Children', () => { | |||||||||||||||||||||||
|
||||||||||||||||||||||||
it('should throw if no children are passed', () => { | ||||||||||||||||||||||||
// eslint-disable-next-line prefer-arrow-callback | ||||||||||||||||||||||||
expect(function() { | ||||||||||||||||||||||||
expect(function () { | ||||||||||||||||||||||||
render(<Foo />, scratch); | ||||||||||||||||||||||||
}).to.throw(); | ||||||||||||||||||||||||
}); | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
it('should throw if more children are passed', () => { | ||||||||||||||||||||||||
// eslint-disable-next-line prefer-arrow-callback | ||||||||||||||||||||||||
expect(function() { | ||||||||||||||||||||||||
expect(function () { | ||||||||||||||||||||||||
render( | ||||||||||||||||||||||||
<Foo> | ||||||||||||||||||||||||
foo | ||||||||||||||||||||||||
|
@@ -77,7 +77,7 @@ describe('Children', () => { | |||||||||||||||||||||||
|
||||||||||||||||||||||||
describe('.map', () => { | ||||||||||||||||||||||||
function Foo(props) { | ||||||||||||||||||||||||
let children = Children.map(props.children, child => ( | ||||||||||||||||||||||||
let children = Children.map(props.children, (child) => ( | ||||||||||||||||||||||||
<span>{child}</span> | ||||||||||||||||||||||||
)); | ||||||||||||||||||||||||
return <div>{children}</div>; | ||||||||||||||||||||||||
|
@@ -99,17 +99,20 @@ describe('Children', () => { | |||||||||||||||||||||||
expect(serializeHtml(scratch)).to.equal('<div></div>'); | ||||||||||||||||||||||||
}); | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
it('should work with children as zero number', () => { | ||||||||||||||||||||||||
const testNumber = 0; | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
render(<Foo>{testNumber}</Foo>, scratch); | ||||||||||||||||||||||||
expect(serializeHtml(scratch)).to.equal('<div><span>0</span></div>'); | ||||||||||||||||||||||||
it('should propagate the this context', () => { | ||||||||||||||||||||||||
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. This is a great start, but I think the commit isn't complete. The test defines a few functions but never calls them anywhere. This will make the test always pass, even against current master and without the changes in this PR.
Suggested change
|
||||||||||||||||||||||||
const context = {}; | ||||||||||||||||||||||||
const fn = () => { | ||||||||||||||||||||||||
expect(this).to.equal(context); | ||||||||||||||||||||||||
}; | ||||||||||||||||||||||||
({ children }) => { | ||||||||||||||||||||||||
React.Children.map(children, fn, (context) => {}); | ||||||||||||||||||||||||
}; | ||||||||||||||||||||||||
}); | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
it('should flatten result', () => { | ||||||||||||||||||||||||
const ProblemChild = ({ children }) => { | ||||||||||||||||||||||||
return React.Children.map(children, child => { | ||||||||||||||||||||||||
return React.Children.map(child.props.children, x => x); | ||||||||||||||||||||||||
return React.Children.map(children, (child) => { | ||||||||||||||||||||||||
return React.Children.map(child.props.children, (x) => x); | ||||||||||||||||||||||||
}).filter(React.isValidElement); | ||||||||||||||||||||||||
}; | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
|
@@ -165,7 +168,7 @@ describe('Children', () => { | |||||||||||||||||||||||
describe('.forEach', () => { | ||||||||||||||||||||||||
function Foo(props) { | ||||||||||||||||||||||||
let children = []; | ||||||||||||||||||||||||
Children.forEach(props.children, child => | ||||||||||||||||||||||||
Children.forEach(props.children, (child) => | ||||||||||||||||||||||||
children.push(<span>{child}</span>) | ||||||||||||||||||||||||
); | ||||||||||||||||||||||||
return <div>{children}</div>; | ||||||||||||||||||||||||
|
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't generally use reduce(). I'd prefer to use
.map(fn.bind(ctx))
for size and perf reasons.