From 390219ccffc5ac3bb68f2df7639f7e48ef600216 Mon Sep 17 00:00:00 2001 From: zombiej Date: Sun, 23 Feb 2020 14:51:33 +0800 Subject: [PATCH 1/2] feat: Support fragement --- package.json | 1 + src/Children/toArray.ts | 20 ++++++++++++--- tests/toArray.test.js | 54 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 71 insertions(+), 4 deletions(-) create mode 100644 tests/toArray.test.js diff --git a/package.json b/package.json index 78d3e56a..db143fd3 100644 --- a/package.json +++ b/package.json @@ -34,6 +34,7 @@ "react-dom": "^15.0.0 || ^16.0.0" }, "devDependencies": { + "@types/enzyme": "^3.10.5", "@types/jest": "^24.0.23", "@types/react": "^16.9.3", "@types/react-dom": "^16.9.1", diff --git a/src/Children/toArray.ts b/src/Children/toArray.ts index 6e3ad84e..82277721 100644 --- a/src/Children/toArray.ts +++ b/src/Children/toArray.ts @@ -1,9 +1,21 @@ import React from 'react'; -export default function toArray(children: React.ReactNode): React.ReactElement[] { - const ret = []; - React.Children.forEach(children, c => { - ret.push(c); +function isFragment(node: React.ReactNode): boolean { + return node && (node as React.ReactElement).type === React.Fragment; +} + +export default function toArray( + children: React.ReactNode, +): React.ReactElement[] { + let ret: React.ReactElement[] = []; + + React.Children.forEach(children, (child: any) => { + if (isFragment(child) && child.props) { + ret = ret.concat(toArray(child.props.children)); + } else { + ret.push(child); + } }); + return ret; } diff --git a/tests/toArray.test.js b/tests/toArray.test.js new file mode 100644 index 00000000..ca566d6a --- /dev/null +++ b/tests/toArray.test.js @@ -0,0 +1,54 @@ +import React from 'react'; +import { mount } from 'enzyme'; +import toArray from '../src/Children/toArray'; + +describe('toArray', () => { + it('basic', () => { + const wrapper = mount( + , + ); + + const children = toArray(wrapper.props().children); + expect(children).toHaveLength(3); + expect(children.map(c => c.key)).toEqual(['1', '2', '3']); + }); + + it('Array', () => { + const wrapper = mount( + , + ); + + const children = toArray(wrapper.props().children); + expect(children).toHaveLength(3); + expect(children.map(c => c.key)).toEqual(['1', '2', '3']); + }); + + it('Fragment', () => { + const wrapper = mount( + , + ); + + const children = toArray(wrapper.props().children); + expect(children).toHaveLength(5); + expect(children.map(c => c.key)).toEqual(['1', '2', '3', '4', '5']); + }); +}); From b4c115fd08d3f13fadcca9420b2e509556494640 Mon Sep 17 00:00:00 2001 From: zombiej Date: Sun, 23 Feb 2020 17:08:39 +0800 Subject: [PATCH 2/2] use react-is --- package.json | 1 + src/Children/toArray.ts | 5 +---- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/package.json b/package.json index db143fd3..3229d1e0 100644 --- a/package.json +++ b/package.json @@ -53,6 +53,7 @@ "add-dom-event-listener": "^1.1.0", "babel-runtime": "6.x", "prop-types": "^15.5.10", + "react-is": "^16.12.0", "react-lifecycles-compat": "^3.0.4", "shallowequal": "^1.1.0" } diff --git a/src/Children/toArray.ts b/src/Children/toArray.ts index 82277721..d3b63f29 100644 --- a/src/Children/toArray.ts +++ b/src/Children/toArray.ts @@ -1,8 +1,5 @@ import React from 'react'; - -function isFragment(node: React.ReactNode): boolean { - return node && (node as React.ReactElement).type === React.Fragment; -} +import { isFragment } from 'react-is'; export default function toArray( children: React.ReactNode,