Skip to content
This repository has been archived by the owner on Aug 28, 2022. It is now read-only.

Commit

Permalink
fix(prop-types): support PropTypes from either React or prop-types mo…
Browse files Browse the repository at this point in the history
…dule (#61)

This ensures compatibility with React 16+ and his suppresses warnings for projects which do not include PropTypes or versions of React less than 15.5.
  • Loading branch information
eliperkins authored and atticoos committed Jul 31, 2017
1 parent 638ed1f commit 4493a8c
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 6 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"node-jsdom": "^3.1.5",
"nps": "^5.1.0",
"nps-utils": "^1.2.0",
"prop-types": "^15.5.10",
"react": "16.0.0-alpha.6",
"react-addons-test-utils": "^15.5.1",
"react-dom": "^15.5.4",
Expand Down
3 changes: 2 additions & 1 deletion src/__tests__/index.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* eslint func-style:0, react/prop-types:0 */
import 'react-native-mock-render/mock' // eslint-disable-line
import React from 'react'
import PropTypes from 'prop-types'
import {StyleSheet, View} from 'react-native'
import renderer from 'react-test-renderer'
import {shallow} from 'enzyme'
Expand Down Expand Up @@ -375,7 +376,7 @@ it('should accept user defined contextTypes', () => {
const dynamicStyles = jest.fn()
const Child = glamorous.view(dynamicStyles)
Child.contextTypes = {
fontSize: React.PropTypes.number,
fontSize: PropTypes.number,
}

const context = {
Expand Down
3 changes: 2 additions & 1 deletion src/__tests__/with-theme.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* eslint func-style:0, react/prop-types:0 */
import React, {PropTypes} from 'react'
import React from 'react'
import PropTypes from 'prop-types'
import {View} from 'react-native'
import renderer from 'react-test-renderer'

Expand Down
3 changes: 2 additions & 1 deletion src/create-glamorous.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import React, {PropTypes} from 'react'
import React from 'react'
import {StyleSheet} from 'react-native'
import {CHANNEL} from './constants'
import getStyles from './get-styles'
import PropTypes from './react-compat'

function prepareStyles(styles) {
return styles.filter(style => {
Expand Down
15 changes: 15 additions & 0 deletions src/react-compat.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import React from 'react'

// eslint-disable-next-line import/no-mutable-exports
let PropTypes

if (parseFloat(React.version.slice(0, 4)) >= 15.5) {
try {
PropTypes = require('prop-types')
} catch (error) {
// ignore
}
}
PropTypes = PropTypes || React.PropTypes

export default PropTypes
3 changes: 2 additions & 1 deletion src/theme-provider.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React, {PropTypes} from 'react'
import React from 'react'
import brcast from 'brcast'
import {CHANNEL} from './constants'
import PropTypes from './react-compat'

export default class ThemeProvider extends React.Component {
static childContextTypes = {
Expand Down
3 changes: 2 additions & 1 deletion src/with-theme.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React, {PropTypes} from 'react'
import React from 'react'
import {CHANNEL} from './constants'
import PropTypes from './react-compat'

function generateWarningMessage(componentName) {
// eslint-disable-next-line max-len
Expand Down
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5089,7 +5089,7 @@ promise@^7.1.1:
dependencies:
asap "~2.0.3"

prop-types@^15.5.4, prop-types@~15.5.7:
prop-types@^15.5.10, prop-types@^15.5.4, prop-types@~15.5.7:
version "15.5.10"
resolved "https://registry.yarnpkg.com/prop-types/-/prop-types-15.5.10.tgz#2797dfc3126182e3a95e3dfbb2e893ddd7456154"
dependencies:
Expand Down

0 comments on commit 4493a8c

Please sign in to comment.