mirror of
https://github.com/facebook/react.git
synced 2025-11-01 09:12:30 +00:00
Mention actual prop type in element type checker (#7319)
(cherry picked from commit 8bcea5310e)
This commit is contained in:
committed by
Paul O’Shannessy
parent
2debcef8f6
commit
2106bf5085
@@ -236,11 +236,13 @@ function createArrayOfTypeChecker(typeChecker) {
|
||||
|
||||
function createElementTypeChecker() {
|
||||
function validate(props, propName, componentName, location, propFullName) {
|
||||
if (!ReactElement.isValidElement(props[propName])) {
|
||||
var propValue = props[propName];
|
||||
if (!ReactElement.isValidElement(propValue)) {
|
||||
var locationName = ReactPropTypeLocationNames[location];
|
||||
var propType = getPropType(propValue);
|
||||
return new Error(
|
||||
`Invalid ${locationName} \`${propFullName}\` supplied to ` +
|
||||
`\`${componentName}\`, expected a single ReactElement.`
|
||||
`Invalid ${locationName} \`${propFullName}\` of type ` +
|
||||
`\`${propType}\` supplied to \`${componentName}\`, expected a single ReactElement.`
|
||||
);
|
||||
}
|
||||
return null;
|
||||
|
||||
@@ -356,12 +356,30 @@ describe('ReactPropTypes', function() {
|
||||
});
|
||||
|
||||
it('should not support multiple components or scalar values', () => {
|
||||
var message = 'Invalid prop `testProp` supplied to `testComponent`, ' +
|
||||
'expected a single ReactElement.';
|
||||
typeCheckFail(PropTypes.element, [<div />, <div />], message);
|
||||
typeCheckFail(PropTypes.element, 123, message);
|
||||
typeCheckFail(PropTypes.element, 'foo', message);
|
||||
typeCheckFail(PropTypes.element, false, message);
|
||||
typeCheckFail(
|
||||
PropTypes.element,
|
||||
[<div />, <div />],
|
||||
'Invalid prop `testProp` of type `array` supplied to `testComponent`, ' +
|
||||
'expected a single ReactElement.'
|
||||
);
|
||||
typeCheckFail(
|
||||
PropTypes.element,
|
||||
123,
|
||||
'Invalid prop `testProp` of type `number` supplied to `testComponent`, ' +
|
||||
'expected a single ReactElement.'
|
||||
);
|
||||
typeCheckFail(
|
||||
PropTypes.element,
|
||||
'foo',
|
||||
'Invalid prop `testProp` of type `string` supplied to `testComponent`, ' +
|
||||
'expected a single ReactElement.'
|
||||
);
|
||||
typeCheckFail(
|
||||
PropTypes.element,
|
||||
false,
|
||||
'Invalid prop `testProp` of type `boolean` supplied to `testComponent`, ' +
|
||||
'expected a single ReactElement.'
|
||||
);
|
||||
});
|
||||
|
||||
it('should be able to define a single child as label', () => {
|
||||
|
||||
Reference in New Issue
Block a user