85 lines
2.6 KiB
JavaScript
85 lines
2.6 KiB
JavaScript
import get from 'es-toolkit/compat/get';
|
|
import { Children } from 'react';
|
|
import { isFragment } from 'react-is';
|
|
import { isNullish } from './DataUtils';
|
|
export var SCALE_TYPES = ['auto', 'linear', 'pow', 'sqrt', 'log', 'identity', 'time', 'band', 'point', 'ordinal', 'quantile', 'quantize', 'utc', 'sequential', 'threshold'];
|
|
|
|
/**
|
|
* @deprecated instead find another approach that does not depend on displayName.
|
|
* Get the display name of a component
|
|
* @param {Object} Comp Specified Component
|
|
* @return {String} Display name of Component
|
|
*/
|
|
export var getDisplayName = Comp => {
|
|
if (typeof Comp === 'string') {
|
|
return Comp;
|
|
}
|
|
if (!Comp) {
|
|
return '';
|
|
}
|
|
return Comp.displayName || Comp.name || 'Component';
|
|
};
|
|
|
|
// `toArray` gets called multiple times during the render
|
|
// so we can memoize last invocation (since reference to `children` is the same)
|
|
var lastChildren = null;
|
|
var lastResult = null;
|
|
|
|
/**
|
|
* @deprecated instead find another approach that does not require reading React Elements from DOM.
|
|
*
|
|
* @param children do not use
|
|
* @return deprecated do not use
|
|
*/
|
|
export var toArray = children => {
|
|
if (children === lastChildren && Array.isArray(lastResult)) {
|
|
return lastResult;
|
|
}
|
|
var result = [];
|
|
Children.forEach(children, child => {
|
|
if (isNullish(child)) return;
|
|
if (isFragment(child)) {
|
|
result = result.concat(toArray(child.props.children));
|
|
} else {
|
|
// @ts-expect-error this could still be Iterable<ReactNode> and TS does not like that
|
|
result.push(child);
|
|
}
|
|
});
|
|
lastResult = result;
|
|
lastChildren = children;
|
|
return result;
|
|
};
|
|
|
|
/**
|
|
* @deprecated instead find another approach that does not require reading React Elements from DOM.
|
|
*
|
|
* Find and return all matched children by type.
|
|
* `type` must be a React.ComponentType
|
|
*
|
|
* @param children do not use
|
|
* @param type do not use
|
|
* @return deprecated do not use
|
|
*/
|
|
export function findAllByType(children, type) {
|
|
var result = [];
|
|
var types = [];
|
|
if (Array.isArray(type)) {
|
|
types = type.map(t => getDisplayName(t));
|
|
} else {
|
|
types = [getDisplayName(type)];
|
|
}
|
|
toArray(children).forEach(child => {
|
|
// @ts-expect-error toArray and lodash.get are not compatible. Let's get rid of the whole findAllByType function
|
|
var childType = get(child, 'type.displayName') || get(child, 'type.name');
|
|
if (childType && types.indexOf(childType) !== -1) {
|
|
result.push(child);
|
|
}
|
|
});
|
|
return result;
|
|
}
|
|
export var isClipDot = dot => {
|
|
if (dot && typeof dot === 'object' && 'clipDot' in dot) {
|
|
return Boolean(dot.clipDot);
|
|
}
|
|
return true;
|
|
}; |