-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(context): Update to latest React Context API (#391)
refactor(context): Reworked internals of ConfigProvider to use to latest React Context Api, removed package prop-types from library, updated peerDependency to include >= react@16.3 BREAKING CHANGE: Removed package, prop-types, from library. Also, updated peerDependency to include >= react@16.3
- Loading branch information
1 parent
c7022d6
commit 1d31921
Showing
26 changed files
with
365 additions
and
272 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,9 +34,6 @@ | |
] | ||
], | ||
"env": { | ||
"development": { | ||
"plugins": ["flow-react-proptypes"] | ||
}, | ||
"test": { | ||
"plugins": [ | ||
[ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = {} | ||
|
||
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 |
Oops, something went wrong.