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

Allow Link to render any valid component type #488

Merged
merged 4 commits into from Jul 19, 2019
Merged

Allow Link to render any valid component type #488

merged 4 commits into from Jul 19, 2019

Conversation

colebemis
Copy link
Contributor

@colebemis colebemis commented Jun 28, 2019

Problem

It is sometimes necessary to pass the Link component an as prop that is not a, button, input or summary. For example, when building a Gatsby site, I often want to render a Link as a GatsbyLink:

<Link as={GatsbyLink} to="/">...</Link>

With out current setup, the above example produces this warning:

image

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

  • Changed base branch to release branch
  • Tested in Chrome
  • Tested in Firefox
  • Tested in Safari
  • Tested in Edge

src/Link.js Outdated
`Invalid prop '${propName}' supplied to '${componentName}': the prop is not a valid React component`
)
}
},
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This function should probably be pulled out into the utils folder so it can be used to define propTypes for other components. I'm not sure what to call the function though. Any ideas @emplums @shawnbot?

Copy link

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) => {
Copy link
Contributor Author

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()

@colebemis colebemis marked this pull request as ready for review June 28, 2019 18:17
Copy link

@emplums emplums left a 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 divs 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`
)
}
},
Copy link

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?

@emplums emplums changed the base branch from master to release-13.2.0 July 19, 2019 18:36
@emplums emplums merged commit a1b9852 into release-13.2.0 Jul 19, 2019
@emplums emplums deleted the link branch July 19, 2019 18:36
@colebemis colebemis mentioned this pull request Jul 19, 2019
8 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants