Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] - Recharts issue 3113 filterProps #4127

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
99 changes: 14 additions & 85 deletions src/util/ReactUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,14 @@ import { isFragment } from 'react-is';
import { DotProps } from '..';
import { isNumber } from './DataUtils';
import { shallowEqual } from './ShallowEqual';
import { FilteredSvgElementType, FilteredElementKeyMap, SVGElementPropKeys, EventKeys } from './types';
import { AreaDot } from '../cartesian/Area';
import { LineDot } from '../cartesian/Line';
import { getSVGElementAttributesInCamelCase, getSVGElementTags, findAllAttributesPresent } from './svgUtils/SVGUtils';
import { svgElementAttributes } from './svgUtils/SVGElementAttributes';

// @Types
import { FilteredSvgElementType, EventKeys } from './types';
import { SVGElementType } from './svgUtils/types';

const REACT_BROWSER_EVENT_MAP: Record<string, string> = {
click: 'onClick',
Expand Down Expand Up @@ -183,87 +188,7 @@ export const validateWidthHeight = (el: any): boolean => {
return true;
};

const SVG_TAGS: string[] = [
'a',
'altGlyph',
'altGlyphDef',
'altGlyphItem',
'animate',
'animateColor',
'animateMotion',
'animateTransform',
'circle',
'clipPath',
'color-profile',
'cursor',
'defs',
'desc',
'ellipse',
'feBlend',
'feColormatrix',
'feComponentTransfer',
'feComposite',
'feConvolveMatrix',
'feDiffuseLighting',
'feDisplacementMap',
'feDistantLight',
'feFlood',
'feFuncA',
'feFuncB',
'feFuncG',
'feFuncR',
'feGaussianBlur',
'feImage',
'feMerge',
'feMergeNode',
'feMorphology',
'feOffset',
'fePointLight',
'feSpecularLighting',
'feSpotLight',
'feTile',
'feTurbulence',
'filter',
'font',
'font-face',
'font-face-format',
'font-face-name',
'font-face-url',
'foreignObject',
'g',
'glyph',
'glyphRef',
'hkern',
'image',
'line',
'lineGradient',
'marker',
'mask',
'metadata',
'missing-glyph',
'mpath',
'path',
'pattern',
'polygon',
'polyline',
'radialGradient',
'rect',
'script',
'set',
'stop',
'style',
'svg',
'switch',
'symbol',
'text',
'textPath',
'title',
'tref',
'tspan',
'use',
'view',
'vkern',
];
const SVG_TAGS: string[] = getSVGElementTags(svgElementAttributes);

const isSvgElement = (child: any) => child && child.type && isString(child.type) && SVG_TAGS.indexOf(child.type) >= 0;

Expand All @@ -282,18 +207,22 @@ export const isValidSpreadableProp = (
property: unknown,
key: string,
includeEvents?: boolean,
svgElementType?: FilteredSvgElementType,
svgElementType?: SVGElementType,
) => {
/**
* If the svg element type is explicitly included, check against the filtered element key map
* to determine if there are attributes that should only exist on that element type.
* @todo Add an internal cjs version of https://github.com/wooorm/svg-element-attributes for full coverage.
*/
const matchingElementTypeKeys = FilteredElementKeyMap?.[svgElementType] ?? [];
const svgElementAttributesInCamelCase = getSVGElementAttributesInCamelCase(svgElementAttributes);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the props we accept are also in camel case for the same reason (react), why do we need to do any transformation here?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I created the getSVGElementAttributesInCamelCase cause the svgElementAttributes object structured as follows. I should have simply create the util to return the converted object without having to do the conversion multiple times.

circle: [
    'alignment-baseline',
    'baseline-shift',
    'clip',
    'clip-path',
    'clip-rule',
    'color',
    ...
    ]
    ```

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can probably do the conversion and once and keep it stored as camel case, we don't need to do it at runtime

const matchingElementTypeKeys = svgElementAttributesInCamelCase?.[svgElementType] ?? [];

const { polygon, polyline, svg, ...rest } = svgElementAttributesInCamelCase;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do these three need to be extracted out?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this was me, messing with things!

const svgAttributes = findAllAttributesPresent(rest);

return (
(!isFunction(property) &&
((svgElementType && matchingElementTypeKeys.includes(key)) || SVGElementPropKeys.includes(key))) ||
((svgElementType && matchingElementTypeKeys.includes(key)) || svgAttributes.includes(key))) ||
(includeEvents && EventKeys.includes(key))
);
};
Expand Down