-
Notifications
You must be signed in to change notification settings - Fork 11
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
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
bb75b23
Initiated new context api
10e6f5f
Initiated new context api
394f2a7
Fixed merge conflicts.
89bedb8
Refactored code to be more DRY
17d872c
Removed prop-types as a peer-dependency
0ca9ebe
Removed prop-types as a peer-dependency
2d5f4be
Fixed merge conflicts.
ee1fa3a
Removed 'gymnast' as a property inside context.
75d5fda
Made defaults read-only.
074ece6
Made defaults read-only.
4623a5b
Fixed merge conflicts.
246da50
Fixed merge conflicts.
20ca746
Merge branch 'denguyen/new-context-api' of https://github.com/gymnast…
5b44972
Replaced omit function with spread operation
14e1a40
Added react-dom back as a peer-dependency and refactor type for defaults
19e1eb4
Final refactoring.
6e0d937
Changed peerDependency so that gymnast now requires React@16.3
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Decided to keep this here, since getting warning via
yarn test
: