Skip to content
Merged
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
6 changes: 6 additions & 0 deletions .changeset/readonly-for-inputs.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@preact/signals": patch
"@preact/signals-react": patch
---

Allow `<For>` to accept readonly arrays and signals containing readonly arrays.
14 changes: 8 additions & 6 deletions packages/preact/utils/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,13 @@ export function Show<T = boolean>(

Show.displayName = "Show";

type ForEach<T> =
| ReadonlyArray<T>

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This means that we'd now accept normal arrays as a valid type as well. The const list + listValue logic already supported, but is there a concern with supporting it explicitly?

| Signal<ReadonlyArray<T>>
| ReadonlySignal<ReadonlyArray<T>>;

interface ForProps<T> {
each:
| Signal<Array<T>>
| ReadonlySignal<Array<T>>
| (() => Array<T> | Signal<Array<T>> | ReadonlySignal<Array<T>>);
each: ForEach<T> | (() => ForEach<T>);
fallback?: ComponentChildren;
getKey?: (item: T, index: number) => string | number;
children: (value: T, index: number) => ComponentChildren;
Expand All @@ -41,8 +43,8 @@ interface ForProps<T> {
export function For<T>(props: ForProps<T>): ComponentChildren | null {
const cache = useMemo(() => new Map(), []);
const list = (typeof props.each === "function" ? props.each() : props.each) as
| Signal<Array<T>>
| Array<T>;
| Signal<ReadonlyArray<T>>
| ReadonlyArray<T>;

const listValue = list instanceof Signal ? list.value : list;

Expand Down
24 changes: 24 additions & 0 deletions packages/preact/utils/test/browser/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,30 @@ describe("@preact/signals-utils", () => {
);
});

it("Should accept readonly list types", () => {
const plain: readonly string[] = ["foo", "bar"];
const list = signal<readonly string[]>(["baz", "qux"]);
const computedList = computed((): readonly string[] => list.value);
const Paragraph = (p: any) => <p>{p.children}</p>;

act(() => {
render(
<div>
<For each={plain}>{item => <Paragraph>{item}</Paragraph>}</For>
<For each={list}>{item => <Paragraph>{item}</Paragraph>}</For>
<For each={() => computedList}>
{item => <Paragraph>{item}</Paragraph>}
</For>
</div>,
scratch
);
});

expect(scratch.innerHTML).to.eq(
"<div><p>foo</p><p>bar</p><p>baz</p><p>qux</p><p>baz</p><p>qux</p></div>"
);
});

it("Should pass updated indexes to reused items after removal", () => {
const alice = { id: "a", label: "Alice" };
const bob = { id: "b", label: "Bob" };
Expand Down
14 changes: 8 additions & 6 deletions packages/react/utils/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,13 @@ export function Show<T = boolean>(props: ShowProps<T>): JSX.Element | null {

Show.displayName = "Show";

type ForEach<T> =
| ReadonlyArray<T>
| Signal<ReadonlyArray<T>>
| ReadonlySignal<ReadonlyArray<T>>;

interface ForProps<T> {
each:
| Signal<Array<T>>
| ReadonlySignal<Array<T>>
| (() => Array<T> | Signal<Array<T>> | ReadonlySignal<Array<T>>);
each: ForEach<T> | (() => ForEach<T>);
fallback?: ReactNode;
getKey?: (item: T, index: number) => string | number;
children: (value: T, index: number) => ReactNode;
Expand All @@ -49,8 +51,8 @@ export function For<T>(props: ForProps<T>): JSX.Element | null {
useSignals();
const cache = useMemo(() => new Map(), []);
const list = (typeof props.each === "function" ? props.each() : props.each) as
| Signal<Array<T>>
| Array<T>;
| Signal<ReadonlyArray<T>>
| ReadonlyArray<T>;

const listValue = list instanceof Signal ? list.value : list;

Expand Down
23 changes: 23 additions & 0 deletions packages/react/utils/test/browser/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,29 @@ describe("@preact/signals-react-utils", () => {
);
});

it("Should accept readonly list types", async () => {
const plain: readonly string[] = ["foo", "bar"];
const list = signal<readonly string[]>(["baz", "qux"]);
const computedList = computed((): readonly string[] => list.value);
const Paragraph = (p: any) => <p>{p.children}</p>;

await act(() => {
render(
<div>
<For each={plain}>{item => <Paragraph>{item}</Paragraph>}</For>
<For each={list}>{item => <Paragraph>{item}</Paragraph>}</For>
<For each={() => computedList}>
{item => <Paragraph>{item}</Paragraph>}
</For>
</div>
);
});

expect(scratch.innerHTML).to.eq(
"<div><p>foo</p><p>bar</p><p>baz</p><p>qux</p><p>baz</p><p>qux</p></div>"
);
});

it("Should pass updated indexes to reused items after removal", async () => {
const alice = { id: "a", label: "Alice" };
const bob = { id: "b", label: "Bob" };
Expand Down