Skip to content

Commit 25e1fc9

Browse files
authored
Merge pull request #25 from grrowl/react-support
React support
2 parents 220ab03 + 92edeb5 commit 25e1fc9

File tree

6 files changed

+68
-21
lines changed

6 files changed

+68
-21
lines changed

.github/workflows/test.yml

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ jobs:
1515
strategy:
1616
matrix:
1717
node-version: [16.x, 18.x, 20.x]
18+
react-version: [18, 19]
1819

1920
steps:
2021
- uses: actions/checkout@v4
@@ -23,6 +24,20 @@ jobs:
2324
with:
2425
node-version: ${{ matrix.node-version }}
2526

26-
- run: npm ci
27+
# Add caching
28+
- uses: actions/cache@v3
29+
id: npm-cache
30+
with:
31+
path: node_modules
32+
key: ${{ runner.os }}-node-${{ matrix.node-version }}-${{ hashFiles('**/package-lock.json') }}
33+
restore-keys: |
34+
${{ runner.os }}-node-${{ matrix.node-version }}-
35+
36+
# Only run npm ci if cache missed
37+
- if: steps.npm-cache.outputs.cache-hit != 'true'
38+
run: npm ci
39+
40+
# Override React versions
41+
- run: npm install --no-save react@^${{ matrix.react-version }} react-dom@^${{ matrix.react-version }} react-is@^${{ matrix.react-version }}
2742

2843
- run: npm test

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# 5.0.0
2+
3+
Move `react-is` to a peer dependency so we can support all react versions in a single version. (thanks @imjordanxd!)
4+
15
# 4.0.0
26

37
React 19 support! (thanks for the help @bmv437, @benface)

README.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
# react-keyed-flatten-children
22

3-
Similar to [React's built-in `Children.toArray` method](https://reactjs.org/docs/react-api.html#reactchildrentoarray), this utility takes children and returns them as an array for introspection or filtering. Different from `Children.toArray`, it will flatten arrays and `React.Fragment`s into a regular, one-dimensional array while ensuring element and fragment keys are preserved, unique, and stable between renders.
4-
5-
⚠️ As of v4.0.0, this library only supports React 19. If you're using React 18 or under, stay on the (still maintained) v3.2.0+.
3+
Similar to [React's built-in `Children.toArray` method](https://reactjs.org/docs/react-api.html#reactchildrentoarray), this utility takes children and returns them as an array for introspection or filtering. Different from `Children.toArray`, it will flatten nested arrays and `React.Fragment`s into a regular, one-dimensional array while ensuring element and fragment keys are preserved, unique, and stable between renders.
64

75
## getting started
86

7+
This library requires a version of `react-is` matching your React version be installed alongside this library as a peer dependency. So, if you're using React 19:
8+
99
```
10-
npm install react-keyed-flatten-children
10+
npm install react-keyed-flatten-children react-is@19
1111
```
1212

13+
If you're using React 18 or lower:
14+
1315
```
14-
yarn add react-keyed-flatten-children
16+
npm install react-keyed-flatten-children react-is@18
1517
```
1618

1719
## why?

index.spec.tsx

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,39 @@ test("simple children", function (t) {
5656
);
5757
});
5858

59-
test.onFinish(cleanup);
59+
test("nested arrays and fragments with mixed content", function (t) {
60+
t.plan(2);
61+
62+
const { container } = render(
63+
<Assert
64+
assert={(result) => {
65+
t.equal(result.length, 8, "array length");
66+
t.deepEqual(
67+
result.map((c: any) => c.key),
68+
[
69+
".0",
70+
undefined,
71+
".1:$a",
72+
undefined,
73+
".1:2:$b",
74+
undefined,
75+
undefined,
76+
".2",
77+
],
78+
"element keys"
79+
);
80+
}}
81+
>
82+
<span>start</span>
83+
{[
84+
"one",
85+
<span key="a">nested element</span>,
86+
["two", <span key="b">deep element</span>, "three", 4],
87+
]}
88+
<span>end</span>
89+
</Assert>
90+
);
91+
});
6092

6193
test("conditional children", function (t) {
6294
t.plan(4);
@@ -206,3 +238,5 @@ test("renders through to react", function (t) {
206238
"element keys"
207239
);
208240
});
241+
242+
test.onFinish(cleanup);

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,10 @@
2121
"test": "tsx index.spec.tsx",
2222
"prepublishOnly": "npm run build"
2323
},
24-
"keywords": [
25-
"react",
26-
"fragment",
27-
"flatten",
28-
"children",
29-
"utility"
30-
],
24+
"keywords": ["react", "fragment", "flatten", "children", "utility"],
3125
"author": "Tom McKenzie <[email protected]>",
3226
"license": "MIT",
33-
"files": [
34-
"dist/"
35-
],
27+
"files": ["dist/"],
3628
"devDependencies": {
3729
"@testing-library/dom": "^10.4.0",
3830
"@testing-library/react": "^16.2.0",
@@ -51,7 +43,7 @@
5143
"typescript": "^5.0.4"
5244
},
5345
"peerDependencies": {
54-
"react": "^18.0.0 || ^19.0.0",
55-
"react-is": "^18.0.0 || ^19.0.0"
46+
"react": ">=18.0.0",
47+
"react-is": ">=18.0.0"
5648
}
5749
}

0 commit comments

Comments
 (0)