import React from 'react' import { AnimationProps, NativeEventHandlers, SvgAttributesGetters, tuple, } from '../lib/types' import { AnimatedDataset } from './AnimatedDataset' import { useSanitizedCascadingAnimation } from './Animation' import { partition, pick } from 'lodash-es' import { useCascadingStyle } from './Style' const splitSvgProps = ( props: SvgAttributesGetters & NativeEventHandlers ): [SvgAttributesGetters, NativeEventHandlers] => { const keys = Object.keys(props) as (keyof typeof props)[] const existingKeys = keys.filter((k) => typeof k !== 'undefined') const [fnKeys, valKeys] = partition( existingKeys, (key) => key.match(/^on[A-Z]/) !== null ) return tuple( pick(props, valKeys) as SvgAttributesGetters, pick(props, fnKeys) as NativeEventHandlers ) } export interface ElementsProps extends AnimationProps, SvgAttributesGetters, NativeEventHandlers { data: T[] tag: string } export function Elements({ tag, data, dataKey, delay, duration, enter, easing, ...props }: ElementsProps) { const animation = useSanitizedCascadingAnimation({ delay, duration, easing }) const [svgProps, events] = splitSvgProps(props) const attributes = { ...useCascadingStyle({}), ...svgProps } return ( dataKey?.(d, i) ?? i} attrs={attributes} events={events} init={enter} {...(animation as any)} /> ) }