@dhis2/prop-types
Advanced tools
Comparing version 1.2.0 to 1.2.1
@@ -8,6 +8,2 @@ "use strict"; | ||
var _react = _interopRequireDefault(require("react")); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
var instanceOfComponentFactory = function instanceOfComponentFactory(Component, isRequired) { | ||
@@ -17,29 +13,28 @@ return function (props, propSelector, // normally a propName, but when wrapped in arrayOf an index | ||
) { | ||
var children = props[propSelector]; | ||
var child = props[propSelector]; | ||
var propName = propFullName || propSelector; | ||
var count = _react.default.Children.count(children); | ||
var isWrappedInArrayOf = !!propFullName; | ||
var hasRenderableChild = child === 0 || !!child; | ||
var baseMsg = "Invalid prop `".concat(propName, "` supplied to `").concat(componentName, "`,"); | ||
if (isWrappedInArrayOf && count === 1 && children === '') { | ||
// When mapping over an empty array to render components react will return '' | ||
// So this is a valid case and should not produce an error | ||
return null; | ||
if (Array.isArray(child)) { | ||
return new Error("".concat(baseMsg, " expected a single component instance but received an array.")); | ||
} | ||
if (isRequired && count === 0) { | ||
return new Error("".concat(baseMsg, " this is a required prop, but no component instance was found")); | ||
if (!hasRenderableChild) { | ||
if (isRequired) { | ||
return new Error("".concat(baseMsg, " this is a required property but its value is `").concat(child, "`.")); | ||
} else { | ||
return null; | ||
} | ||
} | ||
if (count > 1) { | ||
return new Error("".concat(baseMsg, " expected 1 component instance, instead found ").concat(count, ".")); | ||
var expectedComponentName = typeof Component === 'string' ? Component : Component.name || Component.displayName; | ||
var foundComponentName = typeof child.type !== 'string' ? child.type : child.type && (child.type.name || child.type.displayName); | ||
if (!foundComponentName) { | ||
return new Error("".concat(baseMsg, " could not read component name. Property value does not look like a component instance.")); | ||
} | ||
if (children.type !== Component) { | ||
var expectedComponent = typeof Component === 'string' ? Component : Component.name || Component.displayName; | ||
var foundComponent = typeof children.type === 'string' // native elements | ||
? children.type : children.type.name || children.type.displayName; | ||
return new Error("".concat(baseMsg, " expected an instance of `").concat(expectedComponent, "` but found an instance of `").concat(foundComponent, "`.")); | ||
if (child.type !== Component) { | ||
return new Error("".concat(baseMsg, " expected an instance of `").concat(expectedComponentName, "` but found an instance of `").concat(foundComponentName, "`.")); | ||
} | ||
@@ -46,0 +41,0 @@ |
@@ -19,9 +19,4 @@ "use strict"; | ||
var propName = propFullName || propSelector; | ||
var isWrappedInArrayOf = !!propFullName; | ||
var baseMsg = "Invalid prop `".concat(propName, "` supplied to `").concat(componentName, "`,"); // Usage errors | ||
if (isWrappedInArrayOf) { | ||
return new Error("mutuallyExclusive is being wrapped in `arrayOf` for property `".concat(propName, "` on component `").concat(componentName, "`. This is not supported.")); | ||
} | ||
if (exlusivePropNames.length === 0) { | ||
@@ -28,0 +23,0 @@ return new Error("mutuallyExclusive was called without any arguments for property `".concat(propName, "` on component `").concat(componentName, "`. Please add the required arguments.")); |
@@ -1,31 +0,30 @@ | ||
import React from 'react'; | ||
const instanceOfComponentFactory = (Component, isRequired) => (props, propSelector, // normally a propName, but when wrapped in arrayOf an index | ||
componentName, _location, propFullName // normally null but a string like "propName[index]" when wrapped in arrayOf | ||
) => { | ||
const children = props[propSelector]; | ||
const child = props[propSelector]; | ||
const propName = propFullName || propSelector; | ||
const count = React.Children.count(children); | ||
const isWrappedInArrayOf = !!propFullName; | ||
const hasRenderableChild = child === 0 || !!child; | ||
const baseMsg = `Invalid prop \`${propName}\` supplied to \`${componentName}\`,`; | ||
if (isWrappedInArrayOf && count === 1 && children === '') { | ||
// When mapping over an empty array to render components react will return '' | ||
// So this is a valid case and should not produce an error | ||
return null; | ||
if (Array.isArray(child)) { | ||
return new Error(`${baseMsg} expected a single component instance but received an array.`); | ||
} | ||
if (isRequired && count === 0) { | ||
return new Error(`${baseMsg} this is a required prop, but no component instance was found`); | ||
if (!hasRenderableChild) { | ||
if (isRequired) { | ||
return new Error(`${baseMsg} this is a required property but its value is \`${child}\`.`); | ||
} else { | ||
return null; | ||
} | ||
} | ||
if (count > 1) { | ||
return new Error(`${baseMsg} expected 1 component instance, instead found ${count}.`); | ||
const expectedComponentName = typeof Component === 'string' ? Component : Component.name || Component.displayName; | ||
const foundComponentName = typeof child.type !== 'string' ? child.type : child.type && (child.type.name || child.type.displayName); | ||
if (!foundComponentName) { | ||
return new Error(`${baseMsg} could not read component name. Property value does not look like a component instance.`); | ||
} | ||
if (children.type !== Component) { | ||
const expectedComponent = typeof Component === 'string' ? Component : Component.name || Component.displayName; | ||
const foundComponent = typeof children.type === 'string' // native elements | ||
? children.type : children.type.name || children.type.displayName; | ||
return new Error(`${baseMsg} expected an instance of \`${expectedComponent}\` but found an instance of \`${foundComponent}\`.`); | ||
if (child.type !== Component) { | ||
return new Error(`${baseMsg} expected an instance of \`${expectedComponentName}\` but found an instance of \`${foundComponentName}\`.`); | ||
} | ||
@@ -32,0 +31,0 @@ |
@@ -7,9 +7,4 @@ import propTypes from 'prop-types'; | ||
const propName = propFullName || propSelector; | ||
const isWrappedInArrayOf = !!propFullName; | ||
const baseMsg = `Invalid prop \`${propName}\` supplied to \`${componentName}\`,`; // Usage errors | ||
if (isWrappedInArrayOf) { | ||
return new Error(`mutuallyExclusive is being wrapped in \`arrayOf\` for property \`${propName}\` on component \`${componentName}\`. This is not supported.`); | ||
} | ||
if (exlusivePropNames.length === 0) { | ||
@@ -16,0 +11,0 @@ return new Error(`mutuallyExclusive was called without any arguments for property \`${propName}\` on component \`${componentName}\`. Please add the required arguments.`); |
@@ -0,1 +1,8 @@ | ||
## [1.2.1](https://github.com/dhis2/prop-types/compare/v1.2.0...v1.2.1) (2019-10-14) | ||
### Bug Fixes | ||
* improve instanceOfComponent robustness and remove incorrect logic ([#60](https://github.com/dhis2/prop-types/issues/60)) ([7273d7b](https://github.com/dhis2/prop-types/commit/7273d7ba535ebde0931f36e3f9f276ebb60ccc2a)) | ||
# [1.2.0](https://github.com/dhis2/prop-types/compare/v1.1.1...v1.2.0) (2019-10-07) | ||
@@ -2,0 +9,0 @@ |
{ | ||
"name": "@dhis2/prop-types", | ||
"version": "1.2.0", | ||
"version": "1.2.1", | ||
"main": "./build/cjs/index.js", | ||
@@ -26,8 +26,8 @@ "module": "./build/es/index.js", | ||
"devDependencies": { | ||
"@babel/cli": "^7.6.2", | ||
"@babel/core": "^7.6.2", | ||
"@babel/preset-env": "^7.6.2", | ||
"@dhis2/cli-style": "4.1.3", | ||
"@babel/cli": "^7.6.4", | ||
"@babel/core": "^7.6.4", | ||
"@babel/preset-env": "^7.6.3", | ||
"@dhis2/cli-style": "4.2.0", | ||
"babel-eslint": "^10.0.3", | ||
"jsdoc-to-markdown": "^5.0.1" | ||
"jsdoc-to-markdown": "^5.0.2" | ||
}, | ||
@@ -34,0 +34,0 @@ "peerDependencies": { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
27292
423