Skip to content

Commit

Permalink
Add context argument to compat/Children map api
Browse files Browse the repository at this point in the history
React's children api map function supports a "context" argument that we
don't.
See preactjs#2644.
  • Loading branch information
ParSal123 committed Jan 8, 2023
1 parent d527b4b commit 3dc2524
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 8 deletions.
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

0 comments on commit 3dc2524

Please sign in to comment.