| 1 | var _excluded = ["valueAccessor"],
|
|---|
| 2 | _excluded2 = ["dataKey", "clockWise", "id", "textBreakAll", "zIndex"];
|
|---|
| 3 | function _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); }
|
|---|
| 4 | function _objectWithoutProperties(e, t) { if (null == e) return {}; var o, r, i = _objectWithoutPropertiesLoose(e, t); if (Object.getOwnPropertySymbols) { var n = Object.getOwnPropertySymbols(e); for (r = 0; r < n.length; r++) o = n[r], -1 === t.indexOf(o) && {}.propertyIsEnumerable.call(e, o) && (i[o] = e[o]); } return i; }
|
|---|
| 5 | function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (-1 !== e.indexOf(n)) continue; t[n] = r[n]; } return t; }
|
|---|
| 6 | import * as React from 'react';
|
|---|
| 7 | import { createContext, useContext } from 'react';
|
|---|
| 8 | import last from 'es-toolkit/compat/last';
|
|---|
| 9 | import { isLabelContentAFunction, Label } from './Label';
|
|---|
| 10 | import { Layer } from '../container/Layer';
|
|---|
| 11 | import { getValueByDataKey } from '../util/ChartUtils';
|
|---|
| 12 | import { isNullish } from '../util/DataUtils';
|
|---|
| 13 | import { svgPropertiesAndEvents } from '../util/svgPropertiesAndEvents';
|
|---|
| 14 | import { ZIndexLayer } from '../zIndex/ZIndexLayer';
|
|---|
| 15 | import { DefaultZIndexes } from '../zIndex/DefaultZIndexes';
|
|---|
| 16 |
|
|---|
| 17 | /**
|
|---|
| 18 | * This is public API because we expose it as the valueAccessor parameter.
|
|---|
| 19 | *
|
|---|
| 20 | * The properties of "viewBox" are repeated as the root props of the entry object.
|
|---|
| 21 | * So it doesn't matter if you read entry.x or entry.viewBox.x, they are the same.
|
|---|
| 22 | *
|
|---|
| 23 | * It's not necessary to pass redundant data, but we keep it for backward compatibility.
|
|---|
| 24 | */
|
|---|
| 25 |
|
|---|
| 26 | /**
|
|---|
| 27 | * LabelList props do not allow refs because the same props are reused in multiple elements so we don't have a good single place to ref to.
|
|---|
| 28 | */
|
|---|
| 29 |
|
|---|
| 30 | /**
|
|---|
| 31 | * This is the type accepted for the `label` prop on various graphical items.
|
|---|
| 32 | * It accepts:
|
|---|
| 33 | *
|
|---|
| 34 | * boolean:
|
|---|
| 35 | * true = labels show,
|
|---|
| 36 | * false = labels don't show
|
|---|
| 37 | * React element:
|
|---|
| 38 | * will be cloned with extra props
|
|---|
| 39 | * function:
|
|---|
| 40 | * is used as <Label content={function} />, so this will be called once for each individual label (so typically once for each data point)
|
|---|
| 41 | * object:
|
|---|
| 42 | * the props to be passed to a LabelList component
|
|---|
| 43 | *
|
|---|
| 44 | * @inline
|
|---|
| 45 | */
|
|---|
| 46 |
|
|---|
| 47 | var defaultAccessor = entry => Array.isArray(entry.value) ? last(entry.value) : entry.value;
|
|---|
| 48 | var CartesianLabelListContext = /*#__PURE__*/createContext(undefined);
|
|---|
| 49 | export var CartesianLabelListContextProvider = CartesianLabelListContext.Provider;
|
|---|
| 50 | var PolarLabelListContext = /*#__PURE__*/createContext(undefined);
|
|---|
| 51 | export var PolarLabelListContextProvider = PolarLabelListContext.Provider;
|
|---|
| 52 | function useCartesianLabelListContext() {
|
|---|
| 53 | return useContext(CartesianLabelListContext);
|
|---|
| 54 | }
|
|---|
| 55 | function usePolarLabelListContext() {
|
|---|
| 56 | return useContext(PolarLabelListContext);
|
|---|
| 57 | }
|
|---|
| 58 |
|
|---|
| 59 | /**
|
|---|
| 60 | * @consumes LabelListContext
|
|---|
| 61 | */
|
|---|
| 62 | export function LabelList(_ref) {
|
|---|
| 63 | var {
|
|---|
| 64 | valueAccessor = defaultAccessor
|
|---|
| 65 | } = _ref,
|
|---|
| 66 | restProps = _objectWithoutProperties(_ref, _excluded);
|
|---|
| 67 | var {
|
|---|
| 68 | dataKey,
|
|---|
| 69 | clockWise,
|
|---|
| 70 | id,
|
|---|
| 71 | textBreakAll,
|
|---|
| 72 | zIndex
|
|---|
| 73 | } = restProps,
|
|---|
| 74 | others = _objectWithoutProperties(restProps, _excluded2);
|
|---|
| 75 | var cartesianData = useCartesianLabelListContext();
|
|---|
| 76 | var polarData = usePolarLabelListContext();
|
|---|
| 77 | var data = cartesianData || polarData;
|
|---|
| 78 | if (!data || !data.length) {
|
|---|
| 79 | return null;
|
|---|
| 80 | }
|
|---|
| 81 | return /*#__PURE__*/React.createElement(ZIndexLayer, {
|
|---|
| 82 | zIndex: zIndex !== null && zIndex !== void 0 ? zIndex : DefaultZIndexes.label
|
|---|
| 83 | }, /*#__PURE__*/React.createElement(Layer, {
|
|---|
| 84 | className: "recharts-label-list"
|
|---|
| 85 | }, data.map((entry, index) => {
|
|---|
| 86 | var _restProps$fill;
|
|---|
| 87 | var value = isNullish(dataKey) ? valueAccessor(entry, index) : getValueByDataKey(entry.payload, dataKey);
|
|---|
| 88 | var idProps = isNullish(id) ? {} : {
|
|---|
| 89 | id: "".concat(id, "-").concat(index)
|
|---|
| 90 | };
|
|---|
| 91 | return /*#__PURE__*/React.createElement(Label, _extends({
|
|---|
| 92 | key: "label-".concat(index)
|
|---|
| 93 | }, svgPropertiesAndEvents(entry), others, idProps, {
|
|---|
| 94 | /*
|
|---|
| 95 | * Prefer to use the explicit fill from LabelList props.
|
|---|
| 96 | * Only in an absence of that, fall back to the fill of the entry.
|
|---|
| 97 | * The entry fill can be quite difficult to see especially in Bar, Pie, RadialBar in inside positions.
|
|---|
| 98 | * On the other hand it's quite convenient in Scatter, Line, or when the position is outside the Bar, Pie filled shapes.
|
|---|
| 99 | */
|
|---|
| 100 | fill: (_restProps$fill = restProps.fill) !== null && _restProps$fill !== void 0 ? _restProps$fill : entry.fill,
|
|---|
| 101 | parentViewBox: entry.parentViewBox,
|
|---|
| 102 | value: value,
|
|---|
| 103 | textBreakAll: textBreakAll,
|
|---|
| 104 | viewBox: entry.viewBox,
|
|---|
| 105 | index: index
|
|---|
| 106 | /*
|
|---|
| 107 | * Here we don't want to use the default Label zIndex,
|
|---|
| 108 | * we want it to inherit the zIndex of the LabelList itself
|
|---|
| 109 | * which means just rendering as a regular child, without portaling anywhere.
|
|---|
| 110 | */,
|
|---|
| 111 | zIndex: 0
|
|---|
| 112 | }));
|
|---|
| 113 | })));
|
|---|
| 114 | }
|
|---|
| 115 | LabelList.displayName = 'LabelList';
|
|---|
| 116 | export function LabelListFromLabelProp(_ref2) {
|
|---|
| 117 | var {
|
|---|
| 118 | label
|
|---|
| 119 | } = _ref2;
|
|---|
| 120 | if (!label) {
|
|---|
| 121 | return null;
|
|---|
| 122 | }
|
|---|
| 123 | if (label === true) {
|
|---|
| 124 | return /*#__PURE__*/React.createElement(LabelList, {
|
|---|
| 125 | key: "labelList-implicit"
|
|---|
| 126 | });
|
|---|
| 127 | }
|
|---|
| 128 | if (/*#__PURE__*/React.isValidElement(label) || isLabelContentAFunction(label)) {
|
|---|
| 129 | return /*#__PURE__*/React.createElement(LabelList, {
|
|---|
| 130 | key: "labelList-implicit",
|
|---|
| 131 | content: label
|
|---|
| 132 | });
|
|---|
| 133 | }
|
|---|
| 134 | if (typeof label === 'object') {
|
|---|
| 135 | return /*#__PURE__*/React.createElement(LabelList, _extends({
|
|---|
| 136 | key: "labelList-implicit"
|
|---|
| 137 | }, label, {
|
|---|
| 138 | type: String(label.type)
|
|---|
| 139 | }));
|
|---|
| 140 | }
|
|---|
| 141 | return null;
|
|---|
| 142 | } |
|---|