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 ES6 class attrs in styled-component #1751

Merged

Conversation

valerybugakov
Copy link
Member

A small fix to allow passing of the ES6 classes using .attrs()
I was trying to pass component prop to redux-form Field component, but got the error message:

Uncaught TypeError: Class constructor TextInput cannot be invoked without 'new'

It was mistakenly treated like a prop-factory.

@valerybugakov valerybugakov changed the title Allow Class attr in styled-component Allow ES6 class attrs in styled-component May 22, 2018
// @flow

export default function isClass(target: any) {
return typeof target === 'function' && /^\s*class\s+/.test(target.toString())
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think this will work for transpiled classes?

Copy link
Member Author

Choose a reason for hiding this comment

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

Right, forgot about that and according to babel/babel#5640 it's not possible right now.

@@ -83,7 +84,8 @@ export default (ComponentStyle: Function, constructWithOptions: Function) => {
this.attrs = Object.keys(attrs).reduce((acc, key) => {
const attr = attrs[key]
// eslint-disable-next-line no-param-reassign
acc[key] = typeof attr === 'function' ? attr(context) : attr
acc[key] =
typeof attr === 'function' && !isClass(attr) ? attr(context) : attr
Copy link
Contributor

Choose a reason for hiding this comment

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

Instead let's use one of the methods from the react-is package. I think it'll work better.

Copy link
Member Author

Choose a reason for hiding this comment

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

It seems that it's not possible too. With this package we can only check if attr is a valid element type for React but it will accept functions too.

Also: facebook/react#12578

Copy link
Contributor

Choose a reason for hiding this comment

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

Well if we’re just concerned with classes, can we simply check if React.Component is in the inheritance chain for the function?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, it's better than nothing. Will update PR.

@valerybugakov
Copy link
Member Author

@probablyup PR updated

* master:
  Update styled-components.d.ts
  jest 23
  v3.3.0
  sort imports, use the new displayName util
  migrate displayName generation to a util
  update changelog for pull request 1753
  support x attributes
  suppress false positive flow errors
  add the same for StyledNativeComponent
  fix syntax issue in eslintrc
  fix typo
  consoleWarn -> nativeWarn
  hoist non react & sc statics
  Fix flow
  Set testURL to silence JSDOM relative url error
  Upgrade Jest to 22
@@ -42,8 +43,12 @@ export default (constructWithOptions: Function, InlineStyle: Function) => {

this.attrs = Object.keys(attrs).reduce((acc, key) => {
const attr = attrs[key]
const extendsReactComponent = hasInInheritanceChain(attr, Component)
Copy link
Contributor

Choose a reason for hiding this comment

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

let's inline this, no need to do the work unless attr is confirmed to be a function

Copy link
Member Author

Choose a reason for hiding this comment

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

updated

quantizor
quantizor previously approved these changes May 29, 2018
Copy link
Contributor

@quantizor quantizor left a comment

Choose a reason for hiding this comment

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

LGTM aside from the one request

* master:
  just only run lint on travis
  update eslint and fix new linting errors
  use a newer node on appveyor
  bump flow-bin version to 0.73.0
  add changelog entry for styled-components#1748
  remove lodash flow-typed defs
  chore: upgrade flow-bin to latest
@valerybugakov
Copy link
Member Author

I haven't changed anything related to types in this PR. Flow should be happy as it is in master branch.
@probablyup do you have an idea why it might fail?

@quantizor
Copy link
Contributor

@valerybugakov I think if you merge from master it should go away - did a lot of CI changes last night. Also, please make sure that test-results.json stays deleted from git.

* master:
  fix bundlesize param name
  adjust changelog to reflect the new PR number
  remove unactionable danger message
  run prettier on dangerfile
  remove test-results.json from source control
  update danger, travis & appveyor configs, bundlesize
@valerybugakov
Copy link
Member Author

@probablyup deleted test-results.json and merged master. Still getting some errors from flow.

@quantizor
Copy link
Contributor

Dang, ok. If you can't figure out what flow wants you to add a type for, feel free to throw a // $FlowFixMe above the offending line.

@valerybugakov
Copy link
Member Author

Yeah, flow is pretty mysterious sometimes, bypassed errors for now.

@quantizor
Copy link
Contributor

@valerybugakov try merging from master - I turned off danger for the moment so travis should stop being cranky

since travis is not allowing encrypted variables to be available
to forks, it makes running danger impossible for most PRs

I'm going to look into alternate services making use of the new
Github Apps API that don't have this restriction.
@valerybugakov
Copy link
Member Author

@probablyup let's merge until new conflicts have arrived :P

@quantizor
Copy link
Contributor

ha no doubt

@quantizor quantizor merged commit 24b03e7 into styled-components:master Jun 1, 2018
@mxstbr
Copy link
Member

mxstbr commented Jun 1, 2018

Thank you so much for helping us improve styled-components! Based on our Community Guidelines every person that has a PR of any kind merged is offered an invitation to the styled-components organization—that is you right now!

Should you accept, you'll get write access to the main repository. (and a fancy styled-components logo on your GitHub profile!) You'll be able to label and close issues, merge pull requests, create new branches, etc. We want you to help us out with those things, so don't be scared to do them! Make sure to read our contribution and community guidelines, which explains all of this in more detail. Of course, if you have any questions just let me know!

@quantizor
Copy link
Contributor

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

3 participants