Skip to content
This repository was archived by the owner on Dec 16, 2021. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 23 additions & 16 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ function normalizeVNode(vnode) {
}

applyEventNormalization(vnode);

validatePropTypes(vnode);
return vnode;
}

Expand All @@ -318,6 +318,28 @@ function cloneElement(element, props, ...children) {
return normalizeVNode(preactCloneElement(...cloneArgs));
}

function validatePropTypes (vnode) {
let componentClass = typeof vnode.nodeName === 'function' ? vnode.nodeName : vnode.type;

if (typeof componentClass !== 'function') return;
let name = (
componentClass.prototype.displayName
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this seems odd - was componentClass.displayName working before?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The tests would fail. It was returning the function you use to construct the pfcs. This was before I was testing on 8. I'll adjust and try again.

|| componentClass.displayName
|| componentClass.name
);

let propTypes = componentClass.propTypes;

if (propTypes) {
let props = vnode.props;
for (let propKey in propTypes) {
if (propTypes.hasOwnProperty(propKey) && typeof propTypes[propKey] === 'function') {
let err = propTypes[propKey](props, propKey, name, 'prop');
if (err) console.error(new Error(err.message || err));
}
}
}
}

function isValidElement(element) {
return element && ((element instanceof VNode) || element.$$typeof===REACT_ELEMENT_TYPE);
Expand Down Expand Up @@ -509,21 +531,6 @@ function propsHook(props, context) {
props.children[0] = props.children;
}
}

// add proptype checking
if (DEV) {
let ctor = typeof this==='function' ? this : this.constructor,
propTypes = this.propTypes || ctor.propTypes;
if (propTypes) {
for (let prop in propTypes) {
if (propTypes.hasOwnProperty(prop) && typeof propTypes[prop]==='function') {
const displayName = this.displayName || ctor.name;
let err = propTypes[prop](props, prop, displayName, 'prop');
if (err) console.error(new Error(err.message || err));
}
}
}
}
}


Expand Down
22 changes: 22 additions & 0 deletions test/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,28 @@ describe('components', () => {
};
checkPropTypes(Foo2);
});

it.only('should handle function as children propTypes', () => {
function Foo (props) { return React.Children.only(props.children)(); }

Foo.propTypes = {
children: React.PropTypes.func.isRequired
};

sinon.stub(console, 'error');

React.render(<Foo/>, scratch);
expect(console.error).to.have.been.calledWithMatch({
message: 'Children.only() expects only one child.'
});

console.error.reset();

React.render(<Foo>{() => <div/>}</Foo>, scratch);
expect(console.error).not.to.have.been.called;

console.error.restore();
});
});

describe("mixins", () => {
Expand Down