-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
fix: filter out functions in filterProps, remove type from valid SVGElementPropKeys #3327
Conversation
src/util/ReactUtils.ts
Outdated
(!_.isFunction(inputProps?.[key]) && | ||
((svgElementType && matchingElementTypeKeys.includes(key)) || SVGElementPropKeys.includes(key))) || |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for adding the comment. How about refactoring this into a separate function? Such a separate function could then be unit tested nicely.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, the point of the function is to filter props and this is the part that does the filtration. (imo) sometimes factoring out too much can lead to more confusion, but in this case something like isValidSpreadableProp
maybe would do for the check itself?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good cleanup, thank you! It would be nice to capture in a comment as to why TYPE in no longer in the list of SVGElementPropKeys.
Either way this already is an improvement.
src/util/ReactUtils.ts
Outdated
/** | ||
* Props are blindly spread onto SVG elements. This loop filters out properties that we don't want to spread. | ||
* Items filtered out are as follows: | ||
* - functions in properties that are SVG attributes |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
* - functions in properties that are SVG attributes | |
* - functions |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All functions are filtered out.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if includeEvents
is true then there are functions that are passed down. includeEvents
is only set to true when props are spread via our components (such as Curve) and never when props are spread onto an SVG element
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I do not see any issue and could not find any while testing.
Please do consider my refactoring suggestion.
Good to go.
Should have requested updates now! |
Description
Recharts has a
filterProps
function which was intended to filter out all properties that are not valid SVGProps or events that need passed down to children.Multiple times we have come across issues where Recharts defined props such as
type
(in the pastpoints
) overlap with traditional SVGProps (https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute)In these cases props get spread onto SVG elements that aren't intended or valid.
In this case the
type
prop is a valid SVG attribute. This never caused error in the past because the DOM will ignore attributes that are strings or numbers, but when adding a line or areatype
property that is customCurveFactory
from d3 such asthe DOM will throw an error "Invalid value for prop type on tag" because functions are not valid on HTML elements in the DOM.
type
from validSVGElementAttributes
- we do not use any elements that need itisFunction
check withinfilterProps
for attributes that are supposed to be one ofSVGElementAttributes
- these should never be functionsfilterProps
CurveFactory
in AreaChart demoRelated Issue
#3310
Motivation and Context
Resolve above described error. Prevent more errors with
type
in the future.How Has This Been Tested?
The true
type
SVG attribute can only be used with the following tags:None of which we use in Recharts.
CurveFactory
- ensure the new curve factory type is displayed correctly and that the error is not thrown.type
is passed down explicitly by the chart so it is okay that it gets filtered out infilterProps
CurveType
values such asmonotone
andlinear
Screenshots (if appropriate):
^ No error thrown like in issue but curve still reflects the given
CurveFactory
Types of changes
Checklist: