-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4d9457f
commit 55d1520
Showing
24 changed files
with
100 additions
and
95 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,24 @@ | ||
import { Children, FC, ReactElement, ReactNode } from 'react' | ||
import { Children, FC, isValidElement, JSXElementConstructor, ReactNode } from 'react' | ||
import './array.polyfill.flat' // for Mobile Safari 11 | ||
|
||
export function findComponent(nodes: ReactNode = [], componentType: FC): ReactElement | undefined { | ||
const elements = Children.toArray(nodes) as ReactElement[] | ||
return elements.find(({ type }) => type === componentType) || null | ||
} | ||
export const findComponent = (nodes: ReactNode = [], componentType: FC<any>): ReactNode | undefined => | ||
Children.toArray(nodes).find(child => | ||
isValidElement(child) && child.type === componentType) || null | ||
|
||
export function excludeComponent(nodes: ReactNode = [], componentType: FC): ReactElement[] { | ||
const elements = Children.toArray(nodes) as ReactElement[] | ||
return elements.filter(({ type }) => type !== componentType) | ||
} | ||
export const excludeComponent = (nodes: ReactNode = [], componentType: FC<any>): ReactNode[] => | ||
Children.toArray(nodes).filter(child => | ||
isValidElement(child) && child.type !== componentType) | ||
|
||
export function filterByType(nodes: ReactNode = [], componentType: FC | FC[]): ReactElement[] { | ||
const elements = Children.toArray(nodes) as ReactElement[] | ||
const types = [componentType].flat() | ||
return elements.filter(({ type }) => types.includes(type as any)) | ||
export const filterByType = (nodes: ReactNode = [], componentType: FC<any> | FC<any>[]): ReactNode[] => { | ||
const types = [componentType.toString()].flat() | ||
return Children | ||
.toArray(nodes) | ||
.filter(child => isValidElement(child) && types.includes(child.type as string)) | ||
} | ||
|
||
export function filterBy(nodes: ReactNode = [], predicate: (el: ReactElement) => boolean): ReactElement[] { | ||
const elements = Children.toArray(nodes) as ReactElement[] | ||
return elements.filter(predicate) | ||
} | ||
export const filterBy = (nodes: ReactNode = [], predicate: (el: ReactNode) => boolean): ReactNode[] => | ||
Children.toArray(nodes).filter(predicate) | ||
|
||
export const elementProps = (node: ReactNode): Record<string, any> => isValidElement(node) ? node.props : {} | ||
export const elementType = (node: ReactNode): string | JSXElementConstructor<any> | null => | ||
isValidElement(node) ? node.type : null |
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
Oops, something went wrong.