import React, { cloneElement, ReactElement, SVGProps } from 'react'; import _ from 'lodash'; import { Label, ContentType, Props as LabelProps } from './Label'; import { Layer } from '../container/Layer'; import { findAllByType } from '../util/ReactUtils'; import { getValueByDataKey } from '../util/ChartUtils'; import { filterProps, DataKey, ViewBox } from '../util/types'; interface Data { value?: number | string | Array; payload?: any; parentViewBox?: ViewBox; } interface LabelListProps { id?: string; data?: Array; valueAccessor?: Function; clockWise?: boolean; dataKey?: DataKey; content?: ContentType; textBreakAll?: boolean; position?: LabelProps['position']; angle?: number; formatter?: Function; } export type Props = SVGProps & LabelListProps; export type ImplicitLabelListType = | boolean | ReactElement | ((props: any) => ReactElement) | Props; const defaultProps = { valueAccessor: (entry: Data) => (_.isArray(entry.value) ? _.last(entry.value) : entry.value), }; export function LabelList(props: Props) { const { data, valueAccessor, dataKey, clockWise, id, textBreakAll, ...others } = props; if (!data || !data.length) { return null; } return ( {data.map((entry, index) => { const value = _.isNil(dataKey) ? valueAccessor(entry, index) : getValueByDataKey(entry && entry.payload, dataKey); const idProps = _.isNil(id) ? {} : { id: `${id}-${index}` }; return ( ); } LabelList.displayName = 'LabelList'; function parseLabelList(label: any, data: Array) { if (!label) { return null; } if (label === true) { return ; } if (React.isValidElement(label) || _.isFunction(label)) { return ; } if (_.isObject(label)) { return ; } return null; } function renderCallByParent(parentProps: any, data: Array, ckeckPropsLabel = true) { if (!parentProps || (!parentProps.children && ckeckPropsLabel && !parentProps.label)) { return null; } const { children } = parentProps; const explicitChilren = findAllByType(children, LabelList.displayName).map((child: any, index: number) => cloneElement(child, { data, key: `labelList-${index}`, }), ); if (!ckeckPropsLabel) { return explicitChilren; } const implicitLabelList = parseLabelList(parentProps.label, data); return [implicitLabelList, ...explicitChilren]; } LabelList.renderCallByParent = renderCallByParent; LabelList.defaultProps = defaultProps;