import React, { cloneElement } from 'react'; import _ from 'lodash'; import Label, { ContentType } from './Label'; import Layer from '../container/Layer'; import { findAllByType } from '../util/ReactUtils'; import { getValueByDataKey } from '../util/ChartUtils'; import { filterProps, DataKey } from '../util/types'; interface Data { value?: number | string | Array; payload?: any; } interface Props { id?: string; data: Array; valueAccessor?: Function; clockWise?: boolean; dataKey?: DataKey; content?: ContentType; } const defaultProps = { valueAccessor: (entry: Data) => (_.isArray(entry.value) ? _.last(entry.value) : entry.value), }; function LabelList(props: Props) { const { data, valueAccessor, dataKey, clockWise, id, ...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; export default LabelList;