[react-jss] Add flow typings#818
Conversation
| "zen-observable": "^0.6.0" | ||
| }, | ||
| "dependencies": { | ||
| "flow-copy-source": "^2.0.2" |
There was a problem hiding this comment.
We don't copy the source files. We already do the step to use flow typings.
There was a problem hiding this comment.
Fair enough, I didn't see the babel-plugin-transform-flow-strip-types babel plugin, removed
| @@ -0,0 +1,11 @@ | |||
| [ignore] | |||
There was a problem hiding this comment.
This file shouldn't exist because there is a flow config in the root for all packages
| "scripts": { | ||
| "prepare": "node ../../scripts/prepare.js" | ||
| "prepare": "node ../../scripts/prepare.js", | ||
| "flow": "flow" |
There was a problem hiding this comment.
Again, we don't need to run flow here as it's being run from the root directory
| const getStyles = (stylesOrCreator, theme) => { | ||
| type Props = { | ||
| classes: ?{}, | ||
| innerRef?: () => {}, |
There was a problem hiding this comment.
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.
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
| componentDidUpdate(prevProps, prevState) { | ||
| componentDidUpdate(prevProps: Props, prevState: State) { | ||
| // 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.
&& prevState.dynamicSheet should not be removed!
There was a problem hiding this comment.
Same diff reason, fixed
| RuleOptions, | ||
| JssStyle | ||
| } from 'jss/lib/types' | ||
| import RuleList from 'jss/lib/RuleList' |
There was a problem hiding this comment.
should be imported from jss as a type
| ToCssOptions, | ||
| RuleOptions, | ||
| JssStyle | ||
| } from 'jss/lib/types' |
There was a problem hiding this comment.
All of those should be imported from jss
| import RuleList from 'jss/lib/RuleList' | ||
| import {create} from '../jss' | ||
|
|
||
| export interface JssSheet { |
There was a problem hiding this comment.
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.
Should be a Jss instance and not a function which creates an instance
| // $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.
Why did you change the logic of this code?
There was a problem hiding this comment.
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
|
Also please add typings for the other files when you have time. Especially for the JssProvider |
| getDynamicStyles(styles) | ||
| ) | ||
| staticSheet[dynamicStylesNs] = dynamicStyles | ||
| } else dynamicStyles = staticSheet[dynamicStylesNs] |
There was a problem hiding this comment.
Again, no logic changes here please
| stylesOrCreator: StylesOrThemer, | ||
| InnerComponent: ComponentType<P>, | ||
| options: Options = {} | ||
| ): ComponentType<any> { |
There was a problem hiding this comment.
The returning component props should be typed exactly
| @@ -1,3 +1,5 @@ | |||
| // @flow | |||
| type Classes = {[string]: string} | |||
There was a problem hiding this comment.
Should be also imported from jss https://github.com/cssinjs/jss/blob/master/packages/jss/src/index.js#L33
| @@ -0,0 +1,19 @@ | |||
| // @flow | |||
| import type {StyleSheetOptions} from 'jss' | |||
| import jss from '../jss' | |||
There was a problem hiding this comment.
Import the Jss type also from jss
| 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.
Should be also imported from jss https://github.com/cssinjs/jss/blob/master/packages/jss/src/index.js#L33
|
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. |
| "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.
Should we use spaces instead of tabs?
There was a problem hiding this comment.
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.
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.
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
| * IProps: Props of the InnerComponent. | ||
| * OProps: The Outer props the HOC accepts. | ||
| * | ||
| * @param {Object|Function} stylesOrCreator |
There was a problem hiding this comment.
I think we can remove those jsdoc comments now
There was a problem hiding this comment.
Will do some cleanup in another PR. Have some small fixes etc. in mind
| /** | ||
| * Wrap a Component into a JSS Container Component. | ||
| * | ||
| * IProps: Props of the InnerComponent. |
There was a problem hiding this comment.
InnerProps would be probably a better name, no?
There was a problem hiding this comment.
already using InnerProps though, that's why I had to name it IProps 🤔
There was a problem hiding this comment.
lets find a better convention, maybe InnerPropsType?
| // @flow | ||
| import {Component, Children, type Element, type ElementType} from 'react' | ||
| import PropTypes from 'prop-types' | ||
| import type {Jss, generateClassName as GenerateClassNameType, SheetsRegistry} from 'jss' |
There was a problem hiding this comment.
why mapping from lower case to capital?
There was a problem hiding this comment.
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.
Once it starts uppercase you don't need to add Type suffix either
| }) | ||
| } | ||
|
|
||
| // $FlowFixMe InnerComponent can be class or stateless, the latter doesn't have a defaultProps property |
There was a problem hiding this comment.
it may have defaultProps property same way as class based
There was a problem hiding this comment.
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.
weird, we should look for an issue there for explanation or raise one .... really weird
There was a problem hiding this comment.
The explanation is in the comment above.
| 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.
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.
ThemeCreator seems a little bit misleading
* 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: