-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(internal-utils): add deepFind internal util
Add deepFind internal util
- Loading branch information
Showing
4 changed files
with
64 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
packages/utils/internal-utils/src/react-children-utilities/deepFind.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import type { ReactNode } from 'react' | ||
import { Children, isValidElement } from 'react' | ||
|
||
import { hasComplexChildren } from './hasComplexChildren' | ||
|
||
// from https://github.com/fernandopasik/react-children-utilities | ||
function deepFind( | ||
children: ReactNode | ReactNode[], | ||
deepFindFn: (child: ReactNode, index?: number, children?: ReactNode[]) => boolean | ||
): ReactNode | undefined { | ||
// eslint-disable-next-line @typescript-eslint/init-declarations | ||
let found | ||
|
||
Children.toArray(children).find((child: ReactNode, index: number, findChildren: ReactNode[]) => { | ||
if (deepFindFn(child, index, findChildren)) { | ||
found = child | ||
|
||
return true | ||
} | ||
|
||
if (isValidElement(child) && hasComplexChildren(child)) { | ||
// Find inside the child that has children | ||
found = deepFind(child.props.children, deepFindFn) | ||
|
||
return typeof found !== 'undefined' | ||
} | ||
|
||
return false | ||
}) | ||
|
||
return found | ||
} | ||
|
||
export { deepFind } |
10 changes: 10 additions & 0 deletions
10
packages/utils/internal-utils/src/react-children-utilities/hasChildren.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import type { ReactElement, ReactNode } from 'react' | ||
import { isValidElement } from 'react' | ||
|
||
function hasChildren( | ||
element: ReactNode | ||
): element is ReactElement<{ children: ReactNode | ReactNode[] }> { | ||
return isValidElement<{ children?: ReactNode[] }>(element) && Boolean(element.props.children) | ||
} | ||
|
||
export { hasChildren } |
19 changes: 19 additions & 0 deletions
19
packages/utils/internal-utils/src/react-children-utilities/hasComplexChildren.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import type { ReactElement, ReactNode } from 'react' | ||
import { Children, isValidElement } from 'react' | ||
|
||
import { hasChildren } from './hasChildren' | ||
|
||
function hasComplexChildren( | ||
element: ReactNode | ||
): element is ReactElement<{ children: ReactNode | ReactNode[] }> { | ||
return ( | ||
isValidElement(element) && | ||
hasChildren(element) && | ||
Children.toArray(element.props.children).reduce( | ||
(response: boolean, child: ReactNode): boolean => response || isValidElement(child), | ||
false | ||
) | ||
) | ||
} | ||
|
||
export { hasComplexChildren } |