recharts
Version:
React charts
84 lines (81 loc) • 4.31 kB
JavaScript
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
import * as React from 'react';
import { cloneElement, isValidElement } from 'react';
import { Layer } from '../container/Layer';
/**
* This is an abstraction for rendering a user defined prop for a customized shape in several forms.
*
* <Shape /> 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
*
* The concrete default shape is supplied by the caller so this helper does not
* import unrelated shapes and accidentally couple bundles together.
*/
function mergeShapeProps(option, props) {
return _objectSpread(_objectSpread({}, props), option);
}
export function getPropsFromShapeOption(option) {
if (/*#__PURE__*/isValidElement(option)) {
return option.props;
}
return option;
}
function renderWithShapeElement(option, props) {
return /*#__PURE__*/cloneElement(option, mergeShapeProps(getPropsFromShapeOption(option), props));
}
function getShapeIndex(shapeProps) {
if (!('index' in shapeProps)) {
return undefined;
}
var index = shapeProps.index;
return typeof index === 'number' || typeof index === 'string' ? index : undefined;
}
function isActiveShape(shapeProps) {
return 'isActive' in shapeProps && shapeProps.isActive === true;
}
/**
* Renders the user-provided active shape option while keeping each runtime branch aligned with its TypeScript type.
*
* The `option` prop supports four shapes:
* - React element: clone it and let its own props override the injected ones
* - function: call it with the forwarded props and optional index
* - plain object: merge it into the default shape props
* - boolean / undefined: ignore it and render the default shape with the forwarded props
*/
export function Shape(_ref) {
var option = _ref.option,
DefaultShape = _ref.DefaultShape,
shapeProps = _ref.shapeProps,
_ref$activeClassName = _ref.activeClassName,
activeClassName = _ref$activeClassName === void 0 ? 'recharts-active-shape' : _ref$activeClassName,
_ref$inActiveClassNam = _ref.inActiveClassName,
inActiveClassName = _ref$inActiveClassNam === void 0 ? 'recharts-shape' : _ref$inActiveClassNam;
var index = getShapeIndex(shapeProps);
var shape;
if (/*#__PURE__*/isValidElement(option)) {
shape = renderWithShapeElement(option, shapeProps);
} else if (option === DefaultShape) {
shape = /*#__PURE__*/React.createElement(DefaultShape, shapeProps);
} else if (typeof option === 'function') {
shape = option(shapeProps, index);
} else if (typeof option === 'object') {
shape = /*#__PURE__*/React.createElement(DefaultShape, mergeShapeProps(option, shapeProps));
} else {
shape = /*#__PURE__*/React.createElement(DefaultShape, shapeProps);
}
if (isActiveShape(shapeProps)) {
return /*#__PURE__*/React.createElement(Layer, {
className: activeClassName
}, shape);
}
return /*#__PURE__*/React.createElement(Layer, {
className: inActiveClassName
}, shape);
}