Skip to content

Commit 321d8ba

Browse files
committed
feat: initial release
1 parent a4be597 commit 321d8ba

10 files changed

+5968
-0
lines changed

.babelrc

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"presets": ["@babel/preset-env", "@babel/preset-react"]
3+
}

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules/
2+
/dist/

.npmignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/*
2+
!/dist/**/*.js
3+
!/src/**/*.js
4+
*.test.js

.travis.yml

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
language: node_js
2+
3+
node_js:
4+
- 10
5+
6+
before_install:
7+
- curl -o- -L https://yarnpkg.com/install.sh | bash -s -- --version 1.10.1
8+
- export PATH="$HOME/.yarn/bin:$PATH"
9+
10+
notifications:
11+
email: false
12+
13+
cache:
14+
yarn: true
15+
directories:
16+
- 'node_modules'
17+
18+
git:
19+
depth: 5

LICENSE

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Copyright 2018 Smooth Code
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4+
5+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6+
7+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# react-flatten-children
2+
3+
[![License](https://img.shields.io/npm/l/react-flatten-children.svg)](https://github.com/smooth-code/react-flatten-children/blob/master/LICENSE)
4+
[![npm package](https://img.shields.io/npm/v/react-flatten-children/latest.svg)](https://www.npmjs.com/package/react-flatten-children)
5+
[![Build Status](https://img.shields.io/travis/smooth-code/react-flatten-children.svg)](https://travis-ci.org/smooth-code/react-flatten-children)
6+
[![Dependencies](https://img.shields.io/david/smooth-code/react-flatten-children.svg?path=packages%2Fcore)](https://david-dm.org/smooth-code/react-flatten-children?path=packages/core)
7+
[![DevDependencies](https://img.shields.io/david/dev/smooth-code/react-flatten-children.svg)](https://david-dm.org/smooth-code/react-flatten-children?type=dev)
8+
9+
React utility to flatten fragments 🗜.
10+
11+
```sh
12+
npm install react-flatten-children
13+
```
14+
15+
## Example
16+
17+
```js
18+
import React from 'react'
19+
import { Switch as BaseSwitch } from 'react-router'
20+
import flattenChildren from 'react-flatten-children'
21+
import PublicHome from './PublicHome'
22+
import PrivateHome from './PrivateHome'
23+
import Account from './Account'
24+
import Login from './Login'
25+
26+
// Create a fragment ready Switch
27+
const Switch = ({ children }) => (
28+
<BaseSwitch>{flattenChildren(children)}</BaseSwitch>
29+
)
30+
31+
const Routes = ({ isLoggedIn }) => (
32+
<Switch>
33+
{isLoggedIn ? (
34+
<>
35+
<Route exact path="/" component={PrivateHome} />
36+
<Route path="/account" component={Account} />
37+
</>
38+
) : (
39+
<>
40+
<Route exact path="/" component={PublicHome} />
41+
<Route path="/login" component={Login} />
42+
</>
43+
)}
44+
<Route path="/about" component={About} />
45+
<Redirect to="/" />
46+
</Switch>
47+
)
48+
49+
export default Routes
50+
```
51+
52+
👉 [Checkout an interactive example on CodeSandbox](https://codesandbox.io/s/nn6l3r30k0)
53+
54+
## Why?
55+
56+
In many cases you have to introspect children, it can be to [use the first route matching a path](https://reacttraining.com/react-router/web/api/Switch), extract the label of a tab, or another use case.
57+
58+
React considers fragments as children, even if it is in fact a group of children. This package flatten children and make your component API compatible with fragments. Users expect your library to be compatible with fragments. If you want to avoid tons of issues (see https://github.com/ReactTraining/react-router/issues/5917, https://github.com/ReactTraining/react-router/issues/5785), you should use it!
59+
60+
# License
61+
62+
MIT

package.json

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
"name": "react-flatten-children",
3+
"description": "React utility to flatten fragments.",
4+
"keywords": [
5+
"react",
6+
"utility",
7+
"children",
8+
"flatten",
9+
"fragment"
10+
],
11+
"version": "1.0.0",
12+
"author": "Greg Bergé <berge.greg@gmail.com>",
13+
"license": "MIT",
14+
"main": "dist/index.js",
15+
"umd:main": "dist/index.umd.js",
16+
"module": "dist/index.mjs",
17+
"source": "src/index.js",
18+
"scripts": {
19+
"test": "jest",
20+
"build": "microbundle --external react"
21+
},
22+
"devDependencies": {
23+
"@babel/core": "^7.1.2",
24+
"@babel/preset-env": "^7.1.0",
25+
"@babel/preset-react": "^7.0.0",
26+
"babel-core": "^7.0.0-0",
27+
"babel-jest": "^23.6.0",
28+
"jest": "^23.6.0",
29+
"microbundle": "^0.6.0",
30+
"react": "^16.5.2"
31+
}
32+
}

src/index.js

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import React from 'react'
2+
3+
function flattenChildren(children) {
4+
return React.Children.toArray(children).reduce((flatChildren, child) => {
5+
if (child.type === React.Fragment) {
6+
return flatChildren.concat(flattenChildren(child.props.children))
7+
}
8+
flatChildren.push(child)
9+
return flatChildren
10+
}, [])
11+
}
12+
13+
export default flattenChildren

src/index.test.js

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import React from 'react'
2+
import flattenChildren from '.'
3+
4+
const getChildren = element => element.props.children
5+
const getTypes = children => React.Children.map(children, child => child.type)
6+
7+
describe('flattenChildren', () => {
8+
it('should convert child to array', () => {
9+
const children = getChildren(<>Hello</>)
10+
expect(flattenChildren(children)).toEqual(React.Children.toArray(children))
11+
})
12+
13+
it('should convert children to array', () => {
14+
const children = getChildren(
15+
<>
16+
<a />
17+
<b />
18+
</>,
19+
)
20+
expect(flattenChildren(children)).toEqual(React.Children.toArray(children))
21+
})
22+
23+
it('should flatten fragments', () => {
24+
const input = getChildren(
25+
<>
26+
<>
27+
<a />
28+
<b />
29+
</>
30+
<>
31+
<c />
32+
</>
33+
<d />
34+
</>,
35+
)
36+
expect(getTypes(flattenChildren(input))).toEqual(['a', 'b', 'c', 'd'])
37+
})
38+
39+
it('should flatten nested fragments', () => {
40+
const input = getChildren(
41+
<>
42+
<>
43+
<a />
44+
<b />
45+
</>
46+
<>
47+
<c />
48+
<>
49+
<d />
50+
<e />
51+
<>
52+
<f />
53+
</>
54+
</>
55+
</>
56+
<g />
57+
</>,
58+
)
59+
expect(getTypes(flattenChildren(input))).toEqual([
60+
'a',
61+
'b',
62+
'c',
63+
'd',
64+
'e',
65+
'f',
66+
'g',
67+
])
68+
})
69+
})

0 commit comments

Comments
 (0)