import * as React from 'react'; import { cloneElement, isValidElement, SVGProps } from 'react'; import isPlainObject from 'es-toolkit/compat/isPlainObject'; import { Rectangle } from '../shape/Rectangle'; import { Trapezoid } from '../shape/Trapezoid'; import { Sector } from '../shape/Sector'; import { Layer } from '../container/Layer'; import { Symbols, SymbolsProps } from '../shape/Symbols'; /** * This is an abstraction for rendering a user defined prop for a customized shape in several forms. * * is the root and will handle taking in: * - an object of svg properties * - a boolean * - a render prop(inline function that returns jsx) * - a React element * * is a subcomponent of and used to match a component * to the value of props.shapeType that is passed to the root. * */ type ShapeType = 'trapezoid' | 'rectangle' | 'sector' | 'symbols'; export type ShapeProps = { shapeType: ShapeType; option: OptionType; isActive?: boolean; activeClassName?: string; propTransformer?: (option: OptionType, props: unknown) => ShapePropsType; } & ExtraProps; function defaultPropTransformer(option: OptionType, props: ExtraProps) { return { ...props, ...option, } as unknown as ShapePropsType; } function isSymbolsProps(shapeType: ShapeType, _elementProps: unknown): _elementProps is SymbolsProps { return shapeType === 'symbols'; } function ShapeSelector({ shapeType, elementProps, }: { shapeType: ShapeType; elementProps: ShapePropsType; }): React.ReactNode { switch (shapeType) { case 'rectangle': return ; case 'trapezoid': return ; case 'sector': return ; case 'symbols': if (isSymbolsProps(shapeType, elementProps)) { return ; } break; default: return null; } } export function getPropsFromShapeOption(option: unknown): SVGProps { if (isValidElement(option)) { return option.props; } return option; } export function Shape({ option, shapeType, propTransformer = defaultPropTransformer, activeClassName = 'recharts-active-shape', isActive, ...props }: ShapeProps) { let shape: React.JSX.Element; if (isValidElement(option)) { shape = cloneElement(option, { ...props, ...getPropsFromShapeOption(option) }); } else if (typeof option === 'function') { shape = option(props); } else if (isPlainObject(option) && typeof option !== 'boolean') { const nextProps = propTransformer(option, props); shape = shapeType={shapeType} elementProps={nextProps} />; } else { const elementProps = props as unknown as ShapePropsType; shape = shapeType={shapeType} elementProps={elementProps} />; } if (isActive) { return {shape}; } return shape; }