diff --git a/src/Field.tsx b/src/Field.tsx index 25428bd7..0b1cfabb 100644 --- a/src/Field.tsx +++ b/src/Field.tsx @@ -1,6 +1,6 @@ -import toChildrenArray from 'rc-util/es/Children/toArray'; import warning from 'rc-util/es/warning'; import * as React from 'react'; +import toChildrenArray from './utils/toChildrenArray'; import type { FieldEntity, FormInstance, diff --git a/src/utils/toChildrenArray.ts b/src/utils/toChildrenArray.ts new file mode 100644 index 00000000..08ac577b --- /dev/null +++ b/src/utils/toChildrenArray.ts @@ -0,0 +1,32 @@ +import React, { ReactNode } from 'react'; + +interface ToArrayOptions { + keepEmpty?: boolean; +} + +export default function toChildrenArray( + children: ReactNode, + option: ToArrayOptions = {}, +): ReactNode[] { + let ret: ReactNode[] = []; + + if (!option.keepEmpty) { + children = React.Children.toArray(children).filter( + child => child !== undefined && child !== null, + ); + } else { + children = React.Children.toArray(children); + } + + ret = (children as ReactNode[]).flatMap(child => { + if (Array.isArray(child)) { + return toChildrenArray(child, option); + } else if (React.isValidElement(child) && child.type === React.Fragment && child.props) { + return toChildrenArray(child.props.children, option); + } else { + return child; + } + }); + + return ret; +}