Skip to content

Commit 7ef35dc

Browse files
committed
feat: update function toArray to use a native functions of react18
1 parent 541e4e9 commit 7ef35dc

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

src/Field.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import toChildrenArray from 'rc-util/es/Children/toArray';
21
import warning from 'rc-util/es/warning';
32
import * as React from 'react';
3+
import toChildrenArray from './utils/toChildrenArray';
44
import type {
55
FieldEntity,
66
FormInstance,

src/utils/toChildrenArray.ts

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import React, { ReactNode } from 'react';
2+
3+
interface ToArrayOptions {
4+
keepEmpty?: boolean;
5+
}
6+
7+
export default function toChildrenArray(
8+
children: ReactNode,
9+
option: ToArrayOptions = {},
10+
): ReactNode[] {
11+
let ret: ReactNode[] = [];
12+
13+
if (!option.keepEmpty) {
14+
children = React.Children.toArray(children).filter(
15+
child => child !== undefined && child !== null,
16+
);
17+
} else {
18+
children = React.Children.toArray(children);
19+
}
20+
21+
ret = (children as ReactNode[]).flatMap(child => {
22+
if (Array.isArray(child)) {
23+
return toChildrenArray(child, option);
24+
} else if (React.isValidElement(child) && child.type === React.Fragment && child.props) {
25+
return toChildrenArray(child.props.children, option);
26+
} else {
27+
return child;
28+
}
29+
});
30+
31+
return ret;
32+
}

0 commit comments

Comments
 (0)