Skip to content

[react-jss] Add flow typings#818

Merged
HenriBeck merged 27 commits intocssinjs:masterfrom
gomesalexandre:react-jss-flow-typings
Aug 15, 2018
Merged

[react-jss] Add flow typings#818
HenriBeck merged 27 commits intocssinjs:masterfrom
gomesalexandre:react-jss-flow-typings

Conversation

@gomesalexandre
Copy link
Copy Markdown
Contributor

This is a work-in-progress PR, this first commit is pretty much just the work done before the switch to monorepo.

TODO:

  • 100% flow coverage for react-jss
  • adding script for copying source files

Comment thread package.json Outdated
"zen-observable": "^0.6.0"
},
"dependencies": {
"flow-copy-source": "^2.0.2"
Copy link
Copy Markdown
Member

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.

Copy link
Copy Markdown
Contributor Author

@gomesalexandre gomesalexandre Aug 8, 2018

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

Comment thread packages/react-jss/.flowconfig Outdated
@@ -0,0 +1,11 @@
[ignore]
Copy link
Copy Markdown
Member

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

Comment thread packages/react-jss/package.json Outdated
"scripts": {
"prepare": "node ../../scripts/prepare.js"
"prepare": "node ../../scripts/prepare.js",
"flow": "flow"
Copy link
Copy Markdown
Member

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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Removed

Comment thread packages/react-jss/src/createHoc.js Outdated
const getStyles = (stylesOrCreator, theme) => {
type Props = {
classes: ?{},
innerRef?: () => {},
Copy link
Copy Markdown
Member

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

Copy link
Copy Markdown
Contributor Author

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

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) {
Copy link
Copy Markdown
Member

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!

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Same diff reason, fixed

Comment thread packages/react-jss/src/types/index.js Outdated
RuleOptions,
JssStyle
} from 'jss/lib/types'
import RuleList from 'jss/lib/RuleList'
Copy link
Copy Markdown
Member

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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed

Comment thread packages/react-jss/src/types/index.js Outdated
ToCssOptions,
RuleOptions,
JssStyle
} from 'jss/lib/types'
Copy link
Copy Markdown
Member

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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed

Comment thread packages/react-jss/src/types/index.js Outdated
import RuleList from 'jss/lib/RuleList'
import {create} from '../jss'

export interface JssSheet {
Copy link
Copy Markdown
Member

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

Comment thread packages/react-jss/src/types/index.js Outdated
themeListener: any => any
},
inject?: Array<'classes' | 'themes' | 'sheet'>,
jss?: create,
Copy link
Copy Markdown
Member

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

// $FlowFixMe defaultProps are not defined in React$Component
const defaultClasses = InnerComponent.defaultProps ? InnerComponent.defaultProps.classes : {}
const classes = {...defaultClasses, ...sheet.classes, ...userClasses}

Copy link
Copy Markdown
Member

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?

Copy link
Copy Markdown
Contributor Author

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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Reverted

@HenriBeck
Copy link
Copy Markdown
Member

Also please add typings for the other files when you have time. Especially for the JssProvider

Comment thread packages/react-jss/src/createHoc.js Outdated
getDynamicStyles(styles)
)
staticSheet[dynamicStylesNs] = dynamicStyles
} else dynamicStyles = staticSheet[dynamicStylesNs]
Copy link
Copy Markdown
Member

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

Comment thread packages/react-jss/src/createHoc.js Outdated
stylesOrCreator: StylesOrThemer,
InnerComponent: ComponentType<P>,
options: Options = {}
): ComponentType<any> {
Copy link
Copy Markdown
Member

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

Comment thread packages/react-jss/src/compose.js Outdated
@@ -1,3 +1,5 @@
// @flow
type Classes = {[string]: string}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed

Comment thread packages/react-jss/src/types/index.js Outdated
@@ -0,0 +1,19 @@
// @flow
import type {StyleSheetOptions} from 'jss'
import jss from '../jss'
Copy link
Copy Markdown
Member

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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed

Comment thread packages/react-jss/src/types/index.js Outdated
export type Styles = {[string]: {}}
export type ThemerFn = (theme: Theme) => Styles
export type StylesOrThemer = Styles | ThemerFn
export type Classes<S> = {|
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed

@HenriBeck
Copy link
Copy Markdown
Member

Quick guideline on which props the HOC should accept:

  • All of the props of the injected Component
  • An additional innerRef prop which needs to be a function with the injected Component as an argument

I don't think we need to use the $Diff operator for props like classes because the user is actually allowed to overwrite those props.

@gomesalexandre
Copy link
Copy Markdown
Contributor Author

@HenriBeck Do we really need to init options to empty object literal {} if not passed to createHOC? It produces a bit of flow errors because of l.85 i.e
const {theming = defaultTheming, inject, jss: optionsJss, ...sheetOptions} = options.

Then of course, accessing a property of undefined options object with no default would produce errors but that could be mitigated.

@gomesalexandre
Copy link
Copy Markdown
Contributor Author

gomesalexandre commented Aug 9, 2018

@HenriBeck Regarding the HOC, the returned component is stripped of the classes prop isn't it ? That's the logic behing the use of the $Diff util here. But in that case I can simply use an intersection type instead of a Diff type

@HenriBeck
Copy link
Copy Markdown
Member

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.

Comment thread .eslintrc Outdated
"flowtype/define-flow-type": 1,
"flowtype/use-flow-type": 1
"flowtype/use-flow-type": 1,
"semi": ["error", "never"]
Copy link
Copy Markdown
Member

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?

Copy link
Copy Markdown
Member

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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

But yes, definetly spaces

Copy link
Copy Markdown
Contributor Author

@gomesalexandre gomesalexandre Aug 10, 2018

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 ?

Copy link
Copy Markdown
Member

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

@HenriBeck HenriBeck requested a review from kof August 12, 2018 16:00
@HenriBeck
Copy link
Copy Markdown
Member

@kof types should be done

@HenriBeck HenriBeck changed the title WIP PR: Add react-jss flow typings [react-jss] Add flow typings Aug 14, 2018
* IProps: Props of the InnerComponent.
* OProps: The Outer props the HOC accepts.
*
* @param {Object|Function} stylesOrCreator
Copy link
Copy Markdown
Member

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

Copy link
Copy Markdown
Member

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

Comment thread packages/react-jss/src/createHoc.js Outdated
/**
* Wrap a Component into a JSS Container Component.
*
* IProps: Props of the InnerComponent.
Copy link
Copy Markdown
Member

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?

Copy link
Copy Markdown
Member

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 🤔

Copy link
Copy Markdown
Member

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?

Comment thread packages/react-jss/src/JssProvider.js Outdated
// @flow
import {Component, Children, type Element, type ElementType} from 'react'
import PropTypes from 'prop-types'
import type {Jss, generateClassName as GenerateClassNameType, SheetsRegistry} from 'jss'
Copy link
Copy Markdown
Member

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?

Copy link
Copy Markdown
Member

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

Copy link
Copy Markdown
Member

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

@HenriBeck HenriBeck merged commit 1e9ba74 into cssinjs:master Aug 15, 2018
})
}

// $FlowFixMe InnerComponent can be class or stateless, the latter doesn't have a defaultProps property
Copy link
Copy Markdown
Member

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

Copy link
Copy Markdown
Member

@HenriBeck HenriBeck Aug 15, 2018

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

Copy link
Copy Markdown
Member

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

Copy link
Copy Markdown
Member

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.

Copy link
Copy Markdown
Member

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}
Copy link
Copy Markdown
Member

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
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

ThemerFn -> ThemeCreator

Copy link
Copy Markdown
Member

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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Maybe StylesCreator?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

my bad, yes StylesCreator

bhupinderbola pushed a commit to bhupinderbola/jss that referenced this pull request Sep 17, 2019
* 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
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.

4 participants