| | import get from 'es-toolkit/compat/get'; |
| |
|
| | import * as React from 'react'; |
| | import { Children, Component, FunctionComponent, isValidElement, ReactNode } from 'react'; |
| | import { isFragment } from 'react-is'; |
| | import { isNullish } from './DataUtils'; |
| | import { FilteredSvgElementType, FilteredElementKeyMap, SVGElementPropKeys, EventKeys, ActiveDotType } from './types'; |
| |
|
| | export const SCALE_TYPES = [ |
| | 'auto', |
| | 'linear', |
| | 'pow', |
| | 'sqrt', |
| | 'log', |
| | 'identity', |
| | 'time', |
| | 'band', |
| | 'point', |
| | 'ordinal', |
| | 'quantile', |
| | 'quantize', |
| | 'utc', |
| | 'sequential', |
| | 'threshold', |
| | ]; |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | export const getDisplayName = (Comp: React.ComponentType | string) => { |
| | if (typeof Comp === 'string') { |
| | return Comp; |
| | } |
| | if (!Comp) { |
| | return ''; |
| | } |
| | return Comp.displayName || Comp.name || 'Component'; |
| | }; |
| |
|
| | |
| | |
| | let lastChildren: ReactNode | null = null; |
| | let lastResult: ReactNode[] | null = null; |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | export const toArray = <T extends ReactNode>(children: T | T[]): T[] => { |
| | if (children === lastChildren && Array.isArray(lastResult)) { |
| | return lastResult as T[]; |
| | } |
| | let result: T[] = []; |
| | Children.forEach(children, child => { |
| | if (isNullish(child)) return; |
| | if (isFragment(child)) { |
| | result = result.concat(toArray(child.props.children)); |
| | } else { |
| | |
| | result.push(child); |
| | } |
| | }); |
| | lastResult = result; |
| | lastChildren = children; |
| | return result; |
| | }; |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | export function findAllByType< |
| | ComponentType extends React.ComponentType, |
| | DetailedElement = React.DetailedReactHTMLElement<React.ComponentProps<ComponentType>, HTMLElement>, |
| | >(children: ReactNode, type: ComponentType | ComponentType[]): DetailedElement[] { |
| | const result: DetailedElement[] = []; |
| | let types: string[] = []; |
| |
|
| | if (Array.isArray(type)) { |
| | types = type.map(t => getDisplayName(t)); |
| | } else { |
| | types = [getDisplayName(type)]; |
| | } |
| |
|
| | toArray(children).forEach(child => { |
| | const childType = get(child, 'type.displayName') || get(child, 'type.name'); |
| | |
| | if (types.indexOf(childType) !== -1) { |
| | result.push(child as DetailedElement); |
| | } |
| | }); |
| |
|
| | return result; |
| | } |
| |
|
| | export const isClipDot = (dot: ActiveDotType): boolean => { |
| | if (dot && typeof dot === 'object' && 'clipDot' in dot) { |
| | return Boolean(dot.clipDot); |
| | } |
| | return true; |
| | }; |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | export const isValidSpreadableProp = ( |
| | property: unknown, |
| | key: string, |
| | includeEvents?: boolean, |
| | svgElementType?: FilteredSvgElementType, |
| | ) => { |
| | |
| | |
| | |
| | |
| | |
| | const matchingElementTypeKeys = (svgElementType && FilteredElementKeyMap?.[svgElementType]) ?? []; |
| |
|
| | return ( |
| | key.startsWith('data-') || |
| | (typeof property !== 'function' && |
| | ((svgElementType && matchingElementTypeKeys.includes(key)) || SVGElementPropKeys.includes(key))) || |
| | (includeEvents && EventKeys.includes(key)) |
| | ); |
| | }; |
| |
|
| | export const filterProps = ( |
| | props: Record<string, any> | Component | FunctionComponent | boolean | unknown, |
| | includeEvents: boolean, |
| | svgElementType?: FilteredSvgElementType, |
| | ) => { |
| | if (!props || typeof props === 'function' || typeof props === 'boolean') { |
| | return null; |
| | } |
| |
|
| | let inputProps = props as Record<string, any>; |
| |
|
| | if (isValidElement(props)) { |
| | inputProps = props.props as Record<string, any>; |
| | } |
| |
|
| | if (typeof inputProps !== 'object' && typeof inputProps !== 'function') { |
| | return null; |
| | } |
| |
|
| | const out: Record<string, any> = {}; |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | Object.keys(inputProps).forEach(key => { |
| | if (isValidSpreadableProp(inputProps?.[key], key, includeEvents, svgElementType)) { |
| | out[key] = inputProps[key]; |
| | } |
| | }); |
| |
|
| | return out; |
| | }; |
| |
|