| 1 | var _excluded = ["option", "shapeType", "activeClassName"];
|
|---|
| 2 | 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; }
|
|---|
| 3 | 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; }
|
|---|
| 4 | 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; }
|
|---|
| 5 | 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; }
|
|---|
| 6 | 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; }
|
|---|
| 7 | function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
|
|---|
| 8 | 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); }
|
|---|
| 9 | import * as React from 'react';
|
|---|
| 10 | import { cloneElement, isValidElement } from 'react';
|
|---|
| 11 | import isPlainObject from 'es-toolkit/compat/isPlainObject';
|
|---|
| 12 | import { Rectangle } from '../shape/Rectangle';
|
|---|
| 13 | import { Trapezoid } from '../shape/Trapezoid';
|
|---|
| 14 | import { Sector } from '../shape/Sector';
|
|---|
| 15 | import { Layer } from '../container/Layer';
|
|---|
| 16 | import { Symbols } from '../shape/Symbols';
|
|---|
| 17 | import { Curve } from '../shape/Curve';
|
|---|
| 18 |
|
|---|
| 19 | /**
|
|---|
| 20 | * This is an abstraction for rendering a user defined prop for a customized shape in several forms.
|
|---|
| 21 | *
|
|---|
| 22 | * <Shape /> is the root and will handle taking in:
|
|---|
| 23 | * - an object of svg properties
|
|---|
| 24 | * - a boolean
|
|---|
| 25 | * - a render prop(inline function that returns jsx)
|
|---|
| 26 | * - a React element
|
|---|
| 27 | *
|
|---|
| 28 | * <ShapeSelector /> is a subcomponent of <Shape /> and used to match a component
|
|---|
| 29 | * to the value of props.shapeType that is passed to the root.
|
|---|
| 30 | *
|
|---|
| 31 | */
|
|---|
| 32 |
|
|---|
| 33 | function defaultPropTransformer(option, props) {
|
|---|
| 34 | return _objectSpread(_objectSpread({}, props), option);
|
|---|
| 35 | }
|
|---|
| 36 | function isSymbolsProps(shapeType, _elementProps) {
|
|---|
| 37 | return shapeType === 'symbols';
|
|---|
| 38 | }
|
|---|
| 39 | function ShapeSelector(_ref) {
|
|---|
| 40 | var {
|
|---|
| 41 | shapeType,
|
|---|
| 42 | elementProps
|
|---|
| 43 | } = _ref;
|
|---|
| 44 | switch (shapeType) {
|
|---|
| 45 | case 'rectangle':
|
|---|
| 46 | return /*#__PURE__*/React.createElement(Rectangle, elementProps);
|
|---|
| 47 | case 'trapezoid':
|
|---|
| 48 | return /*#__PURE__*/React.createElement(Trapezoid, elementProps);
|
|---|
| 49 | case 'sector':
|
|---|
| 50 | return /*#__PURE__*/React.createElement(Sector, elementProps);
|
|---|
| 51 | case 'symbols':
|
|---|
| 52 | if (isSymbolsProps(shapeType, elementProps)) {
|
|---|
| 53 | return /*#__PURE__*/React.createElement(Symbols, elementProps);
|
|---|
| 54 | }
|
|---|
| 55 | break;
|
|---|
| 56 | case 'curve':
|
|---|
| 57 | return /*#__PURE__*/React.createElement(Curve, elementProps);
|
|---|
| 58 | default:
|
|---|
| 59 | return null;
|
|---|
| 60 | }
|
|---|
| 61 | }
|
|---|
| 62 | export function getPropsFromShapeOption(option) {
|
|---|
| 63 | if (/*#__PURE__*/isValidElement(option)) {
|
|---|
| 64 | return option.props;
|
|---|
| 65 | }
|
|---|
| 66 | return option;
|
|---|
| 67 | }
|
|---|
| 68 | export function Shape(_ref2) {
|
|---|
| 69 | var {
|
|---|
| 70 | option,
|
|---|
| 71 | shapeType,
|
|---|
| 72 | activeClassName = 'recharts-active-shape'
|
|---|
| 73 | } = _ref2,
|
|---|
| 74 | props = _objectWithoutProperties(_ref2, _excluded);
|
|---|
| 75 | var shape;
|
|---|
| 76 | if (/*#__PURE__*/isValidElement(option)) {
|
|---|
| 77 | // @ts-expect-error we can't know the type of cloned element props
|
|---|
| 78 | shape = /*#__PURE__*/cloneElement(option, _objectSpread(_objectSpread({}, props), getPropsFromShapeOption(option)));
|
|---|
| 79 | } else if (typeof option === 'function') {
|
|---|
| 80 | shape = option(props, props.index);
|
|---|
| 81 | } else if (isPlainObject(option) && typeof option !== 'boolean') {
|
|---|
| 82 | var nextProps = defaultPropTransformer(option, props);
|
|---|
| 83 | shape = /*#__PURE__*/React.createElement(ShapeSelector, {
|
|---|
| 84 | shapeType: shapeType,
|
|---|
| 85 | elementProps: nextProps
|
|---|
| 86 | });
|
|---|
| 87 | } else {
|
|---|
| 88 | var elementProps = props;
|
|---|
| 89 | shape = /*#__PURE__*/React.createElement(ShapeSelector, {
|
|---|
| 90 | shapeType: shapeType,
|
|---|
| 91 | elementProps: elementProps
|
|---|
| 92 | });
|
|---|
| 93 | }
|
|---|
| 94 | if (props.isActive) {
|
|---|
| 95 | return /*#__PURE__*/React.createElement(Layer, {
|
|---|
| 96 | className: activeClassName
|
|---|
| 97 | }, shape);
|
|---|
| 98 | }
|
|---|
| 99 | return shape;
|
|---|
| 100 | } |
|---|