import type { CommonProps , SeriesProps } from '../types' ; import React , { ReactElement , ReactNode } from 'react' ; import { isFunction } from './isFunction' ; import { isNumber } from './isNumber' ; import { isPlainObject } from './isPlainObject' ; import { isUndefined } from './isUndefined' ; import { normalizeSeries } from './normalizeSeries' ; import { omitBy } from './omitBy' ; import { pick } from './pick' ; const limitsPropNames = [ 'maxX' , 'maxY' , 'minX' , 'minY' ] ; export function proxyChildren ( children , seriesProps = { } , extraProps = { } ) { const definedLimits = pick ( seriesProps , limitsPropNames ) ; const normalizedSeriesProps = normalizeSeries ( seriesProps ) ; const calculatedLimits = pick ( normalizedSeriesProps , limitsPropNames ) ; const { series : normalizedSeries } = normalizedSeriesProps ; return React . Children . map ( children , ( child ) => { if ( ! child || ! React . isValidElement ( child ) ) { return child ; } let cloneChildProps = { ... child . props } ; const childExtraProps = isFunction ( extraProps ) ? extraProps ( child ) : extraProps ; Object . keys ( childExtraProps ) . forEach ( extraPropName => { const extraPropValue = childExtraProps [ extraPropName ] ; if ( extraPropName in cloneChildProps ) { if ( isPlainObject ( extraPropValue ) && isPlainObject ( cloneChildProps [ extraPropName ] ) ) { cloneChildProps [ extraPropName ] = { ... extraPropValue , ... cloneChildProps [ extraPropName ] } ; } } else { cloneChildProps [ extraPropName ] = extraPropValue ; } } ) ; const definedChildLimits = pick ( child . props , limitsPropNames ) ; const childNormalizedSeriesProps = normalizeSeries ( { layerWidth : cloneChildProps . layerWidth , layerHeight : cloneChildProps . layerHeight , ... child . props } ) ; const calculatedChildLimits = pick ( childNormalizedSeriesProps , limitsPropNames ) ; cloneChildProps = { ... calculatedLimits , ... calculatedChildLimits , ... definedLimits , ... definedChildLimits , ... cloneChildProps } ; const { seriesIndex } = child . props ; if ( ! child . props . series && normalizedSeries ) { if ( isUndefined ( seriesIndex ) ) { cloneChildProps . series = normalizedSeries ; } else if ( isNumber ( seriesIndex ) ) { cloneChildProps . series = [ normalizedSeries [ seriesIndex ] ] ; } else if ( Array . isArray ( seriesIndex ) ) { cloneChildProps . series = seriesIndex . map ( index => normalizedSeries [ index ] ) ; } else if ( isFunction ( seriesIndex ) ) { cloneChildProps . series = normalizedSeries . filter ( seriesIndex ) ; } else { cloneChildProps . series = normalizedSeries ; } } else { cloneChildProps . series = childNormalizedSeriesProps . series ; } cloneChildProps . seriesNormalized = true ; return React . cloneElement ( child , omitBy ( cloneChildProps , isUndefined ) ) ; } ) ; }