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

refactor(context): Update to latest React Context API #391

Merged
merged 17 commits into from
Sep 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions .babelrc
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,6 @@
]
],
"env": {
"development": {
"plugins": ["flow-react-proptypes"]
},
"test": {
"plugins": [
[
Expand Down
9 changes: 2 additions & 7 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,7 @@
"jest": true,
"jasmine": true
},
"plugins": [
"react",
"flowtype"
],
"plugins": ["react", "flowtype"],
"settings": {
"react": {
"version": "15.5"
Expand All @@ -40,9 +37,7 @@
"import/no-unresolved": [
2,
{
"ignore": [
"gymnast"
]
"ignore": ["gymnast"]
}
],
"no-use-before-define": ["error", "nofunc"],
Expand Down
35 changes: 0 additions & 35 deletions flow-typed/npm/prop-types_v15.x.x.js

This file was deleted.

4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
"babel-cli": "6.26.0",
"babel-eslint": "8.2.6",
"babel-jest": "23.4.2",
"babel-plugin-flow-react-proptypes": "24.1.1",
"babel-plugin-lodash": "3.3.4",
"babel-plugin-macros": "2.4.0",
"babel-plugin-module-resolver": "3.1.1",
Expand Down Expand Up @@ -141,8 +140,7 @@
"verifyConditions": "condition-circle"
},
"peerDependencies": {
"prop-types": ">=15",
"react": ">=15",
"react": ">=16.3",
"react-dom": ">=15"
},
"dependencies": {
Expand Down
1 change: 0 additions & 1 deletion scripts/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ module.exports = {
externals: {
react: 'react',
'react-dom': 'react-dom',
'prop-types': 'prop-types',
},
module: {
rules: [
Expand Down
32 changes: 14 additions & 18 deletions src/asGrid/index.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
// @flow
import * as React from 'react'
import { compact } from 'lodash'
import type {
OneResolutionGrid,
ConfigProviderContext,
GridProps,
OneResolution,
} from '../types'
import type { OneResolutionGrid, GridProps, OneResolution } from '../types'
import { styles, getCol } from './grid.styles'
import { getValue } from '../utils'
import getCoreStyles from '../core'
import withResolution from '../withResolution'
import withContext from '../withContext'

const resolutionProperties = ['align', 'justify', 'size']

Expand All @@ -20,17 +16,15 @@ export default function asGrid(
props: $Shape<OneResolution>
) => $Shape<OneResolution> = props => props
): React.ComponentType<GridProps> {
function Grid(
{
align,
className,
justify,
size,
innerRef,
...restProps
}: OneResolutionGrid,
context: ConfigProviderContext
) {
function Grid({
align,
className,
justify,
size,
innerRef,
context,
...restProps
}: OneResolutionGrid) {
const props = getCoreStyles(mapDefaultProps(restProps), context)
const classes = compact([
styles.grid,
Expand All @@ -43,5 +37,7 @@ export default function asGrid(
return <Component ref={innerRef} {...props} className={classes.join(' ')} />
}

return withResolution(Grid, resolutionProperties)
const Resolution = withResolution(Grid, resolutionProperties)

return withContext(Resolution)
}
32 changes: 14 additions & 18 deletions src/asLayout/index.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
// @flow
import * as React from 'react'
import { compact } from 'lodash'
import type {
OneResolutionLayout,
ConfigProviderContext,
LayoutProps,
OneResolution,
} from '../types'
import type { OneResolutionLayout, LayoutProps, OneResolution } from '../types'
import getStyles from './layout.styles'
import getCoreStyles from '../core'
import { getValues } from '../utils/index'
import withResolution from '../withResolution'
import withContext from '../withContext'

const resolutionProperties = ['fixed', 'height', 'overflow']

Expand All @@ -20,17 +16,15 @@ export default function asLayout(
props: $Shape<OneResolution>
) => $Shape<OneResolution> = props => props
): React.ComponentType<LayoutProps> {
function Layout(
{
className,
fixed,
height,
overflow,
innerRef,
...restProps
}: OneResolutionLayout,
context: ConfigProviderContext
) {
function Layout({
className,
fixed,
height,
overflow,
innerRef,
context,
...restProps
}: OneResolutionLayout) {
const props = getCoreStyles(mapDefaultProps(restProps), context)
const styles = getStyles(getValues(context, restProps))
const classes = compact([
Expand All @@ -44,5 +38,7 @@ export default function asLayout(
return <Component ref={innerRef} {...props} className={classes.join(' ')} />
}

return withResolution(Layout, resolutionProperties)
const Resolution = withResolution(Layout, resolutionProperties)

return withContext(Resolution)
}
41 changes: 0 additions & 41 deletions src/configProvider/configProvider.spec.js

This file was deleted.

10 changes: 10 additions & 0 deletions src/configProvider/consumer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// @flow
import * as React from 'react'
import type { ConfigContextType } from '../types'
import Context from './context'

type Props = { children: (context: ConfigContextType) => ?React.Node }

export default function ConfigConsumer({ children }: Props) {
return <Context.Consumer>{context => children(context)}</Context.Consumer>
}
10 changes: 10 additions & 0 deletions src/configProvider/context.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// @flow
import * as React from 'react'
import type { ConfigContextType } from '../types'
import defaults from '../defaults'

const contextDefaults: ConfigContextType = defaults

const Context = React.createContext(contextDefaults)

export default Context
79 changes: 28 additions & 51 deletions src/configProvider/index.js
Original file line number Diff line number Diff line change
@@ -1,65 +1,42 @@
// @flow
import * as React from 'react'
import PropTypes from 'prop-types'
import type {
ConfigProviderContext,
DisplayAliases,
SpacingAliases,
} from '../types'
import defaults from '../defaults'
import type { ConfigContextType } from '../types'
import Context from './context'

type Props = {
base?: number,
type ProviderProps = {
...ConfigContextType,
children?: React.Node,
columns?: number,
displayAliases?: DisplayAliases,
fallbackDisplayKey?: string,
gutter?: number,
maxPageWidth?: number | 'none',
minPageWidth?: number,
pageMargin?: {
[string]: number,
},
spacingAliases?: SpacingAliases,
verticalGutter?: number,
}

export const ConfigContextPropTypes = {
gymnast: PropTypes.shape({
base: PropTypes.number,
columns: PropTypes.number,
displayAliases: PropTypes.shape({}),
fallbackDisplayKey: PropTypes.string,
gutter: PropTypes.number,
maxPageWidth: PropTypes.oneOf([PropTypes.number, 'none']),
minPageWidth: PropTypes.number,
pageMargin: PropTypes.shape({}),
spacingAliases: PropTypes.shape({}),
verticalGutter: PropTypes.number,
}),
}

export default class ConfigProvider extends React.Component<Props> {
static contextTypes = ConfigContextPropTypes
static childContextTypes = ConfigContextPropTypes
type ProviderState = ConfigContextType | {}

getChildContext(): ConfigProviderContext {
const {
gutter = defaults.gutter,
verticalGutter = gutter,
children,
...props
} = this.props
// This HOC takes a Provider component and wraps it around a Consumer
// component that provides non-defaulted values that have been set from preceding
// Provider components.
class ConfigProvider extends React.Component<ProviderProps, ProviderState> {
static getDerivedStateFromProps(props: ProviderProps, state: ProviderState) {
const { children, ...restProps } = props

return {
gymnast: {
gutter,
verticalGutter,
...props,
},
...state,
...restProps,
}
}

state = {}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Decided to keep this here, since getting warning via yarn test:

$ jest -u
 PASS  src/asCol/asCol.spec.js
 PASS  src/asLayout/layout.spec.js
 PASS  src/offset/offset.spec.js
 PASS  src/root/root.spec.js
 PASS  src/dev/dev.spec.js
 PASS  src/withResolution/withResolution.logic.spec.js
 PASS  src/core/core.spec.js
 PASS  src/withResolution/withResolution.spec.js
 PASS  src/log/log.spec.js
 PASS  src/withResolution/mediaQuery.spec.js
 PASS  src/errors/index.spec.js
 PASS  src/utils/utils.spec.js
 PASS  src/asGrid/grid.spec.js
 PASS  src/configProvider/index.spec.js
  ● Console

    console.error node_modules/fbjs/lib/warning.js:33
      Warning: ConfigProvider: Did not properly initialize state during construction. Expected state to be an object, but it was undefined.

 PASS  test/e2e/gymnast.spec.js
 PASS  src/withContext/index.spec.js
  ● Console

    console.error node_modules/fbjs/lib/warning.js:33
      Warning: ConfigProvider: Did not properly initialize state during construction. Expected state to be an object, but it was undefined.


Test Suites: 16 passed, 16 total
Tests:       3 skipped, 118 passed, 121 total
Snapshots:   4 passed, 4 total
Time:        4.683s
Ran all test suites.
✨  Done in 6.51s.


render() {
return this.props.children || null
return (
<Context.Consumer>
{(context: ConfigContextType) => {
const { children } = this.props
const value = { ...context, ...this.state }

return <Context.Provider value={value}>{children}</Context.Provider>
}}
</Context.Consumer>
)
}
}

export default ConfigProvider
Loading