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

Adapt to React api: add context argument to compat/Children map api #3855

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions compat/src/Children.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { toChildArray } from 'preact';

const mapFn = (children, fn) => {
const mapFn = (children, fn, context) => {
if (children == null) return null;
return toChildArray(toChildArray(children).map(fn));
return toChildArray(toChildArray(children).map(fn.bind(context)));
};

// This API is completely unnecessary for Preact, so it's basically passthrough.
Expand Down
17 changes: 11 additions & 6 deletions compat/src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,26 +135,31 @@ declare namespace React {
interface MutableRefObject<T> {
current: T;
}

export type ForwardedRef<T> = ((instance: T | null) => void) | MutableRefObject<T | null> | null;

export type ForwardedRef<T> =
| ((instance: T | null) => void)
| MutableRefObject<T | null>
| null;

export function unstable_batchedUpdates(
callback: (arg?: any) => void,
arg?: any
): void;

export type PropsWithChildren<P = unknown> = P & {
children?: preact.ComponentChild | undefined
children?: preact.ComponentChild | undefined;
};

export const Children: {
map<T extends preact.ComponentChild, R>(
children: T | T[],
fn: (child: T, i: number) => R
fn: (child: T, i: number) => R,
context: any
): R[];
forEach<T extends preact.ComponentChild>(
children: T | T[],
fn: (child: T, i: number) => void
fn: (child: T, i: number) => void,
context: any
): void;
count: (children: preact.ComponentChildren) => number;
only: (children: preact.ComponentChildren) => preact.ComponentChild;
Expand Down
12 changes: 12 additions & 0 deletions compat/test/browser/Children.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
} from '../../../test/_util/helpers';
import { div, span } from '../../../test/_util/dom';
import React, { createElement, Children, render } from 'preact/compat';
import sinon from 'sinon';

describe('Children', () => {
/** @type {HTMLDivElement} */
Expand Down Expand Up @@ -106,6 +107,17 @@ describe('Children', () => {
expect(serializeHtml(scratch)).to.equal('<div><span>0</span></div>');
});

it('should propagate "this" context', () => {
const context = {};
const spy = sinon.spy(child => child); // noop
const Foo = ({ children }) => {
return React.Children.map(children, spy, context);
};
render(<Foo>foo</Foo>, scratch);

expect(spy.thisValues[0]).to.equal(context);
});

it('should flatten result', () => {
const ProblemChild = ({ children }) => {
return React.Children.map(children, child => {
Expand Down