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
Allow ES6 class attrs in styled-component #1751
Conversation
src/utils/isClass.js
Outdated
// @flow | ||
|
||
export default function isClass(target: any) { | ||
return typeof target === 'function' && /^\s*class\s+/.test(target.toString()) |
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 don't think this will work for transpiled classes?
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.
Right, forgot about that and according to babel/babel#5640 it's not possible right now.
src/models/StyledComponent.js
Outdated
@@ -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 |
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.
Instead let's use one of the methods from the react-is
package. I think it'll work better.
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.
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
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.
Well if we’re just concerned with classes, can we simply check if React.Component
is in the inheritance chain for the function?
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.
Yeah, it's better than nothing. Will update PR.
@probablyup PR updated |
66dafc2
to
cd6df77
Compare
* 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
src/models/StyledNativeComponent.js
Outdated
@@ -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) |
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.
let's inline this, no need to do the work unless attr
is confirmed to be a function
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.
updated
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.
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
I haven't changed anything related to types in this PR. Flow should be happy as it is in master branch. |
@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 |
* 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
@probablyup deleted |
Dang, ok. If you can't figure out what flow wants you to add a type for, feel free to throw a |
Yeah, flow is pretty mysterious sometimes, bypassed errors for now. |
@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.
@probablyup let's merge until new conflicts have arrived :P |
ha no doubt |
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 |
A small fix to allow passing of the ES6 classes using
.attrs()
I was trying to pass
component
prop toredux-form
Field
component, but got the error message:It was mistakenly treated like a
prop-factory
.