-
Notifications
You must be signed in to change notification settings - Fork 597
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
Allow Link to render any valid component type #488
Conversation
src/Link.js
Outdated
`Invalid prop '${propName}' supplied to '${componentName}': the prop is not a valid React component` | ||
) | ||
} | ||
}, |
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.
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.
Agree, let's move this into Utils! Maybe call it isValidComponent
?
src/Link.js
Outdated
@@ -39,7 +40,13 @@ Link.defaultProps = { | |||
} | |||
|
|||
Link.propTypes = { | |||
as: PropTypes.oneOf(['a', 'button', 'input', 'summary']), | |||
as: (props, propName, componentName) => { |
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.
A custom validator function was necessary because oneOf([string, func])
doesn't allow components created with React.forwardRef()
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'm generally ok with this change, but the reason we restricted element types from the beginning was to prevent people from using div
s or other inaccessible elements for links. Can you add some documentation to the Link
component about the importance of using an accessible element type?
Requesting changes for the docs additions & for moving that function into the utils file
src/Link.js
Outdated
`Invalid prop '${propName}' supplied to '${componentName}': the prop is not a valid React component` | ||
) | ||
} | ||
}, |
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.
Agree, let's move this into Utils! Maybe call it isValidComponent
?
Problem
It is sometimes necessary to pass the
Link
component anas
prop that is nota
,button
,input
orsummary
. For example, when building a Gatsby site, I often want to render aLink
as aGatsbyLink
:With out current setup, the above example produces this warning:
Fix
I updated the propTypes definition for
as
to accept any valid React component type. I followed the approach outlined in this issue: facebook/react#5143 (comment).Merge checklist