| [a762898] | 1 | import get from 'es-toolkit/compat/get';
|
|---|
| 2 | import { Children } from 'react';
|
|---|
| 3 | import { isFragment } from 'react-is';
|
|---|
| 4 | import { isNullish } from './DataUtils';
|
|---|
| 5 | export var SCALE_TYPES = ['auto', 'linear', 'pow', 'sqrt', 'log', 'identity', 'time', 'band', 'point', 'ordinal', 'quantile', 'quantize', 'utc', 'sequential', 'threshold'];
|
|---|
| 6 |
|
|---|
| 7 | /**
|
|---|
| 8 | * @deprecated instead find another approach that does not depend on displayName.
|
|---|
| 9 | * Get the display name of a component
|
|---|
| 10 | * @param {Object} Comp Specified Component
|
|---|
| 11 | * @return {String} Display name of Component
|
|---|
| 12 | */
|
|---|
| 13 | export var getDisplayName = Comp => {
|
|---|
| 14 | if (typeof Comp === 'string') {
|
|---|
| 15 | return Comp;
|
|---|
| 16 | }
|
|---|
| 17 | if (!Comp) {
|
|---|
| 18 | return '';
|
|---|
| 19 | }
|
|---|
| 20 | return Comp.displayName || Comp.name || 'Component';
|
|---|
| 21 | };
|
|---|
| 22 |
|
|---|
| 23 | // `toArray` gets called multiple times during the render
|
|---|
| 24 | // so we can memoize last invocation (since reference to `children` is the same)
|
|---|
| 25 | var lastChildren = null;
|
|---|
| 26 | var lastResult = null;
|
|---|
| 27 |
|
|---|
| 28 | /**
|
|---|
| 29 | * @deprecated instead find another approach that does not require reading React Elements from DOM.
|
|---|
| 30 | *
|
|---|
| 31 | * @param children do not use
|
|---|
| 32 | * @return deprecated do not use
|
|---|
| 33 | */
|
|---|
| 34 | export var toArray = children => {
|
|---|
| 35 | if (children === lastChildren && Array.isArray(lastResult)) {
|
|---|
| 36 | return lastResult;
|
|---|
| 37 | }
|
|---|
| 38 | var result = [];
|
|---|
| 39 | Children.forEach(children, child => {
|
|---|
| 40 | if (isNullish(child)) return;
|
|---|
| 41 | if (isFragment(child)) {
|
|---|
| 42 | result = result.concat(toArray(child.props.children));
|
|---|
| 43 | } else {
|
|---|
| 44 | // @ts-expect-error this could still be Iterable<ReactNode> and TS does not like that
|
|---|
| 45 | result.push(child);
|
|---|
| 46 | }
|
|---|
| 47 | });
|
|---|
| 48 | lastResult = result;
|
|---|
| 49 | lastChildren = children;
|
|---|
| 50 | return result;
|
|---|
| 51 | };
|
|---|
| 52 |
|
|---|
| 53 | /**
|
|---|
| 54 | * @deprecated instead find another approach that does not require reading React Elements from DOM.
|
|---|
| 55 | *
|
|---|
| 56 | * Find and return all matched children by type.
|
|---|
| 57 | * `type` must be a React.ComponentType
|
|---|
| 58 | *
|
|---|
| 59 | * @param children do not use
|
|---|
| 60 | * @param type do not use
|
|---|
| 61 | * @return deprecated do not use
|
|---|
| 62 | */
|
|---|
| 63 | export function findAllByType(children, type) {
|
|---|
| 64 | var result = [];
|
|---|
| 65 | var types = [];
|
|---|
| 66 | if (Array.isArray(type)) {
|
|---|
| 67 | types = type.map(t => getDisplayName(t));
|
|---|
| 68 | } else {
|
|---|
| 69 | types = [getDisplayName(type)];
|
|---|
| 70 | }
|
|---|
| 71 | toArray(children).forEach(child => {
|
|---|
| 72 | // @ts-expect-error toArray and lodash.get are not compatible. Let's get rid of the whole findAllByType function
|
|---|
| 73 | var childType = get(child, 'type.displayName') || get(child, 'type.name');
|
|---|
| 74 | if (childType && types.indexOf(childType) !== -1) {
|
|---|
| 75 | result.push(child);
|
|---|
| 76 | }
|
|---|
| 77 | });
|
|---|
| 78 | return result;
|
|---|
| 79 | }
|
|---|
| 80 | export var isClipDot = dot => {
|
|---|
| 81 | if (dot && typeof dot === 'object' && 'clipDot' in dot) {
|
|---|
| 82 | return Boolean(dot.clipDot);
|
|---|
| 83 | }
|
|---|
| 84 | return true;
|
|---|
| 85 | }; |
|---|