Skip to content

Commit 80712b1

Browse files
authored
types: Widen util component types to accept all child node content (#795)
* types: Widen util component types to accept all child node content * docs: Add changeset * fix: Appease TS?
1 parent 75a9e9d commit 80712b1

File tree

3 files changed

+20
-14
lines changed

3 files changed

+20
-14
lines changed

.changeset/cold-snakes-give.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@preact/signals": patch
3+
"@preact/signals-react": patch
4+
---
5+
6+
Widen utility component types to accept ComponentChildren/ReactNodes as children and fallbacks

packages/preact/utils/src/index.tsx

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { ReadonlySignal, Signal } from "@preact/signals-core";
22
import { useSignal } from "@preact/signals";
3-
import { Fragment, createElement, JSX } from "preact";
3+
import { Fragment, createElement, ComponentChildren } from "preact";
44
import { useMemo } from "preact/hooks";
55

66
interface ShowProps<T = boolean> {
77
when: Signal<T> | ReadonlySignal<T> | (() => T);
8-
fallback?: JSX.Element;
9-
children: JSX.Element | ((value: NonNullable<T>) => JSX.Element);
8+
fallback?: ComponentChildren;
9+
children: ComponentChildren | ((value: NonNullable<T>) => ComponentChildren);
1010
}
1111

1212
const Item = (props: any) => {
@@ -15,7 +15,7 @@ const Item = (props: any) => {
1515
: props.children;
1616
};
1717

18-
export function Show<T = boolean>(props: ShowProps<T>): JSX.Element | null {
18+
export function Show<T = boolean>(props: ShowProps<T>): ComponentChildren | null {
1919
const value =
2020
typeof props.when === "function" ? props.when() : props.when.value;
2121
if (!value) return props.fallback || null;
@@ -27,11 +27,11 @@ interface ForProps<T> {
2727
| Signal<Array<T>>
2828
| ReadonlySignal<Array<T>>
2929
| (() => Signal<Array<T>> | ReadonlySignal<Array<T>>);
30-
fallback?: JSX.Element;
31-
children: (value: T, index: number) => JSX.Element;
30+
fallback?: ComponentChildren;
31+
children: (value: T, index: number) => ComponentChildren;
3232
}
3333

34-
export function For<T>(props: ForProps<T>): JSX.Element | null {
34+
export function For<T>(props: ForProps<T>): ComponentChildren | null {
3535
const cache = useMemo(() => new Map(), []);
3636
let list = (
3737
(typeof props.each === "function" ? props.each() : props.each) as Signal<

packages/react/utils/src/index.tsx

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { ReadonlySignal, Signal } from "@preact/signals-core";
22
import { useSignal } from "@preact/signals-react";
33
import { useSignals } from "@preact/signals-react/runtime";
4-
import { Fragment, createElement, useMemo } from "react";
4+
import { Fragment, createElement, useMemo, ReactNode } from "react";
55

66
interface ShowProps<T = boolean> {
77
when: Signal<T> | ReadonlySignal<T> | (() => T);
8-
fallback?: JSX.Element;
9-
children: JSX.Element | ((value: NonNullable<T>) => JSX.Element);
8+
fallback?: ReactNode;
9+
children: ReactNode | ((value: NonNullable<T>) => ReactNode);
1010
}
1111

1212
const Item = (props: any) => {
@@ -20,7 +20,7 @@ export function Show<T = boolean>(props: ShowProps<T>): JSX.Element | null {
2020
useSignals();
2121
const value =
2222
typeof props.when === "function" ? props.when() : props.when.value;
23-
if (!value) return props.fallback || null;
23+
if (!value) return (props.fallback as JSX.Element) || null;
2424
return <Item v={value} children={props.children} />;
2525
}
2626

@@ -29,8 +29,8 @@ interface ForProps<T> {
2929
| Signal<Array<T>>
3030
| ReadonlySignal<Array<T>>
3131
| (() => Signal<Array<T>> | ReadonlySignal<Array<T>>);
32-
fallback?: JSX.Element;
33-
children: (value: T, index: number) => JSX.Element;
32+
fallback?: ReactNode;
33+
children: (value: T, index: number) => ReactNode;
3434
}
3535

3636
export function For<T>(props: ForProps<T>): JSX.Element | null {
@@ -42,7 +42,7 @@ export function For<T>(props: ForProps<T>): JSX.Element | null {
4242
>
4343
).value;
4444

45-
if (!list.length) return props.fallback || null;
45+
if (!list.length) return (props.fallback as JSX.Element) || null;
4646

4747
const removed = new Set(cache.keys());
4848

0 commit comments

Comments
 (0)