-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#285] strictFunctionTypes: true, fix 21 errors
- Loading branch information
1 parent
1163391
commit 7cc9914
Showing
12 changed files
with
151 additions
and
88 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
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,24 +1,49 @@ | ||
import { Children, FC, isValidElement, JSXElementConstructor, ReactNode } from 'react' | ||
import { Children, FC, ForwardRefExoticComponent, isValidElement, JSXElementConstructor, ReactNode } from 'react' | ||
import './array.polyfill.flat' // for Mobile Safari 11 | ||
|
||
export const findComponent = (nodes: ReactNode = [], componentType: FC<any>): ReactNode | undefined => | ||
Children.toArray(nodes).find(child => | ||
isValidElement(child) && child.type === componentType) || null | ||
const componentName = | ||
(component: FC<any> | JSXElementConstructor<any> | ForwardRefExoticComponent<any> | string): string | undefined => { | ||
switch (typeof component) { | ||
case 'string': | ||
return component | ||
case 'function': | ||
return component.name | ||
case 'symbol': | ||
return (component as any).displayName | ||
default: | ||
return undefined | ||
} | ||
} | ||
|
||
export const excludeComponent = (nodes: ReactNode = [], componentType: FC<any>): ReactNode[] => | ||
Children.toArray(nodes).filter(child => | ||
isValidElement(child) && child.type !== componentType) | ||
// Returns props of a valid ReactElement | ||
export const elementProps = (node: ReactNode): Record<string, any> => isValidElement(node) ? node.props : {} | ||
|
||
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)) | ||
} | ||
// Returns component type name or null | ||
export const elementType = (node: ReactNode): string | null => | ||
isValidElement(node) ? componentName(node.type) : null | ||
|
||
// Finds first node of certain component type | ||
export const findComponent = (nodes: ReactNode = [], componentType: FC<any>): ReactNode | undefined => | ||
Children.toArray(nodes).find(child => elementType(child) === componentName(componentType)) || null | ||
|
||
// Filters nodes by 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 | ||
// Filters nodes by component type(s) | ||
export const filterByType = (nodes: ReactNode = [], componentType: FC<any> | FC<any>[]): ReactNode[] => { | ||
const types = [componentType] | ||
.flat() | ||
.map(comp => typeof comp === 'function' && comp.name) | ||
.filter(Boolean) | ||
return filterBy(nodes, child => types.includes(elementType(child))) | ||
} | ||
|
||
// Filters out nodes of certain component type(s) | ||
export const excludeComponent = (nodes: ReactNode = [], componentType: FC<any> | FC<any>[]): ReactNode[] => { | ||
const types = [componentType] | ||
.flat() | ||
.map(comp => typeof comp === 'function' && comp.name) | ||
.filter(Boolean) | ||
return filterBy(nodes, child => !types.includes(elementType(child))) | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,22 @@ | ||
import { CSSProperties, ReactNode } from 'react' | ||
import { PointerDirection } from '../common/tooltip-utils' | ||
import { Icon } from '../ebay-icon' | ||
|
||
export type Variant = 'default' | 'modal' | ||
|
||
export type EbayInfotipProps = { | ||
variant?: Variant; | ||
icon?: Icon; | ||
disabled?: boolean; | ||
initialExpanded?: boolean; | ||
pointer?: PointerDirection; | ||
overlayStyle?: CSSProperties; | ||
onExpand?: () => void; | ||
onCollapse?: () => void; | ||
a11yCloseText: string; | ||
'aria-label'?: string; | ||
className?: string; | ||
children?: ReactNode; | ||
a11yMaximizeText?:string; | ||
a11yMinimizeText?:string; | ||
} |
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.