-
-
Notifications
You must be signed in to change notification settings - Fork 399
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
[react-jss] Add flow typings #818
[react-jss] Add flow typings #818
Conversation
package.json
Outdated
@@ -99,5 +99,8 @@ | |||
"sinon": "4.5.0", | |||
"webpack": "^2.3.3", | |||
"zen-observable": "^0.6.0" | |||
}, | |||
"dependencies": { | |||
"flow-copy-source": "^2.0.2" |
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.
We don't copy the source files. We already do the step to use flow typings.
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.
Fair enough, I didn't see the babel-plugin-transform-flow-strip-types
babel plugin, removed
packages/react-jss/.flowconfig
Outdated
@@ -0,0 +1,11 @@ | |||
[ignore] |
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.
This file shouldn't exist because there is a flow config in the root for all packages
packages/react-jss/package.json
Outdated
@@ -33,7 +33,8 @@ | |||
"decorator" | |||
], | |||
"scripts": { | |||
"prepare": "node ../../scripts/prepare.js" | |||
"prepare": "node ../../scripts/prepare.js", | |||
"flow": "flow" |
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.
Again, we don't need to run flow here as it's being run from the root directory
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.
Removed
packages/react-jss/src/createHoc.js
Outdated
const getStyles = (stylesOrCreator, theme) => { | ||
type Props = { | ||
classes: ?{}, | ||
innerRef?: () => {}, |
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.
should be (comp: ComponentType<{}> | null) => void
. You could make the component type more exact with moving the props into the generic types of createHOC
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.
Used (comp: ComponentType<{}> | null) => void
in the current implementation, will implement generics in the next PR, it's still a bit of overhead for me still
packages/react-jss/src/createHoc.js
Outdated
// We remove previous dynamicSheet only after new one was created to avoid FOUC. | ||
if (prevState.dynamicSheet !== this.state.dynamicSheet && prevState.dynamicSheet) { |
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.
&& prevState.dynamicSheet
should not be removed!
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.
Same diff reason, fixed
RuleOptions, | ||
JssStyle | ||
} from 'jss/lib/types' | ||
import RuleList from 'jss/lib/RuleList' |
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.
should be imported from jss
as a type
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.
Fixed
ToCssOptions, | ||
RuleOptions, | ||
JssStyle | ||
} from 'jss/lib/types' |
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 of those should be imported from jss
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.
Fixed
import RuleList from 'jss/lib/RuleList' | ||
import {create} from '../jss' | ||
|
||
export interface JssSheet { |
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.
No need to redefine the StyleSheet here. Just import it from jss
as a type
themeListener: any => any | ||
}, | ||
inject?: Array<'classes' | 'themes' | 'sheet'>, | ||
jss?: create, |
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.
Should be a Jss instance and not a function which creates an instance
packages/react-jss/src/createHoc.js
Outdated
const sheet = dynamicSheet || staticSheet | ||
// $FlowFixMe defaultProps are not defined in React$Component | ||
const defaultClasses = InnerComponent.defaultProps ? InnerComponent.defaultProps.classes : {} | ||
const classes = {...defaultClasses, ...sheet.classes, ...userClasses} | ||
|
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.
Why did you change the logic of this code?
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.
For now this is essentially a copy and paste from previous PR. Given the diff shows there are some changes (such as this one) that haven't been integrated in it, I'll take care of it top-priority, before working on anything else
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.
Reverted
Also please add typings for the other files when you have time. Especially for the JssProvider |
packages/react-jss/src/createHoc.js
Outdated
getDynamicStyles(styles) | ||
) | ||
staticSheet[dynamicStylesNs] = dynamicStyles | ||
} else dynamicStyles = staticSheet[dynamicStylesNs] |
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.
Again, no logic changes here please
packages/react-jss/src/createHoc.js
Outdated
stylesOrCreator: StylesOrThemer, | ||
InnerComponent: ComponentType<P>, | ||
options: Options = {} | ||
): ComponentType<any> { |
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.
The returning component props should be typed exactly
packages/react-jss/src/compose.js
Outdated
@@ -1,3 +1,5 @@ | |||
// @flow | |||
type Classes = {[string]: string} |
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.
Should be also imported from jss
https://github.com/cssinjs/jss/blob/master/packages/jss/src/index.js#L33
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.
Fixed
@@ -0,0 +1,19 @@ | |||
// @flow | |||
import type {StyleSheetOptions} from 'jss' | |||
import jss from '../jss' |
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.
Import the Jss
type also from jss
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.
Fixed
export type Styles = {[string]: {}} | ||
export type ThemerFn = (theme: Theme) => Styles | ||
export type StylesOrThemer = Styles | ThemerFn | ||
export type Classes<S> = {| |
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.
Should be also imported from jss
https://github.com/cssinjs/jss/blob/master/packages/jss/src/index.js#L33
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.
Fixed
Quick guideline on which props the HOC should accept:
I don't think we need to use the |
@HenriBeck Do we really need to init options to empty object literal Then of course, accessing a property of undefined |
@HenriBeck Regarding the HOC, the returned component is stripped of the |
No, I was wrong about that before, see https://github.com/cssinjs/jss/blob/master/packages/react-jss/src/createHoc.js#L191 User classes override the from the stylesheet. |
.eslintrc
Outdated
@@ -4,7 +4,8 @@ | |||
"rules": { | |||
"import/no-unresolved": 0, | |||
"flowtype/define-flow-type": 1, | |||
"flowtype/use-flow-type": 1 | |||
"flowtype/use-flow-type": 1, | |||
"semi": ["error", "never"] |
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.
Should we use spaces instead of tabs?
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 think this can be reverted anyway because prettier takes care of it. I actually write the code with semicolons and prettier removes them for me
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.
But yes, definetly spaces
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.
Using the indent rule would avoid such issue, I have automatic linting fixes on save in VSCode. Plus we would get errors thrown within the hook, should I add it ?
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.
We also run prettier in the precommit git hook so it's unnecessary
…rops utility types
- Used OuterProps and InnerProps types - Fixed the props of the returning component
- Made most props optional - Only allow one child
@kof types should be done |
…egistry after component unmounts
packages/react-jss/src/createHoc.js
Outdated
@@ -57,41 +74,49 @@ let managersCounter = 0 | |||
/** | |||
* Wrap a Component into a JSS Container Component. | |||
* | |||
* IProps: Props of the InnerComponent. | |||
* OProps: The Outer props the HOC accepts. | |||
* | |||
* @param {Object|Function} stylesOrCreator |
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 think we can remove those jsdoc comments now
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.
Will do some cleanup in another PR. Have some small fixes etc. in mind
packages/react-jss/src/createHoc.js
Outdated
@@ -57,41 +74,49 @@ let managersCounter = 0 | |||
/** | |||
* Wrap a Component into a JSS Container Component. | |||
* | |||
* IProps: Props of the InnerComponent. |
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.
InnerProps would be probably a better name, no?
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.
already using InnerProps though, that's why I had to name it IProps 🤔
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.
lets find a better convention, maybe InnerPropsType?
import PropTypes from 'prop-types' | ||
import type {Jss, generateClassName as GenerateClassNameType, SheetsRegistry} from 'jss' |
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.
why mapping from lower case to capital?
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.
ohh, I think we need to rename it to upper case in the core package in the first place
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.
Once it starts uppercase you don't need to add Type suffix either
@@ -176,6 +209,7 @@ export default (stylesOrCreator, InnerComponent, options = {}) => { | |||
}) | |||
} | |||
|
|||
// $FlowFixMe InnerComponent can be class or stateless, the latter doesn't have a defaultProps property |
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 may have defaultProps property same way as class based
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 but flow doesn't have defaultProps
typed on the class nor the functional component https://github.com/facebook/flow/blob/master/lib/react.js#L99
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.
weird, we should look for an issue there for explanation or raise one .... really weird
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.
The explanation is in the comment above.
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.
lol ... my day is over :)
b768b78919504fba9de2c03545c5cd3a?: {[key: number]: SheetsManager}, | ||
'6fc570d6bd61383819d0f9e7407c452d': StyleSheetFactoryOptions & {disableStylesGeneration?: boolean} | ||
} | ||
export type OuterProps<IP, C> = IP & {innerRef: (instance: ElementRef<C> | null) => void} |
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.
Lets not use these abbreviations, they are hard to understand
export type OuterProps<IP, C> = IP & {innerRef: (instance: ElementRef<C> | null) => void} | ||
export type Styles = {[string]: {}} | ||
export type ThemerFn = (theme: Theme) => Styles | ||
export type StylesOrThemer = Styles | ThemerFn |
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.
ThemerFn -> ThemeCreator
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.
ThemeCreator seems a little bit misleading
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.
Maybe StylesCreator?
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.
my bad, yes StylesCreator
* First commit * Fixes on createHOC and injectSheet * More fixes on createHOC and injectSheet * Better flow typings on createHOC * Moved types/index.js just to types.js and added InnerProps and OuterProps utility types * Simplified typings of injectSheet.js * Updated and fixed some typings in createHOC.js - Used OuterProps and InnerProps types - Fixed the props of the returning component * Removed unnecessary types in ns.js * Added flow flag to some untyped files * Exporting StyleSheetFactoryOptions now instead of StyleSheetOptions * Fix typing of getDisplayName * Added Context Type and updated Options to use StyleSheetFactoryOptions type * Moved types/index.js just to types.js and added * Improved Context Type * Updated createHoc to use Context type * Improved typing of the Theming option * Formatted code with prettier * Updated size snapshot and lockfile * Make options in injectSheet optional an default it to an empty object * Fixed types of the JssProvider props - Made most props optional - Only allow one child * Remove semi eslint rule * Removed unnecessary types * Remove unnecessary check * Updated a if statement and fixed removing dynamic sheet from global registry after component unmounts * Fixed naming of some type names to always start with an uppercase char * Renamed generic types of createHOC
This is a work-in-progress PR, this first commit is pretty much just the work done before the switch to monorepo.
TODO: