Skip to content

Commit

Permalink
feat: update function toArray to use a native functions of react18
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffdrumgod committed Apr 4, 2023
1 parent 541e4e9 commit 7ef35dc
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Field.tsx
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
32 changes: 32 additions & 0 deletions src/utils/toChildrenArray.ts
Original file line number Diff line number Diff line change
@@ -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;
}

0 comments on commit 7ef35dc

Please sign in to comment.