Skip to content
This repository was archived by the owner on Dec 16, 2021. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@
"immutability-helper": "^2.1.2",
"preact-render-to-string": "^3.6.0",
"preact-transition-group": "^1.1.0",
"proptypes": "^0.14.3",
"prop-types": "^15.5.8",

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thought: maybe this should be a peer dependency now?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Up to you, but the prop-types docs recommends leaving it here ¯_(ツ)_/¯

https://github.com/reactjs/prop-types/#how-to-depend-on-this-package

"standalone-react-addons-pure-render-mixin": "^0.1.1"
}
}
2 changes: 1 addition & 1 deletion rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export default {
useStrict: false,
globals: {
'preact': 'preact',
'proptypes': 'PropTypes'
'prop-types': 'PropTypes'
},
plugins: [
format==='umd' && memory({
Expand Down
12 changes: 4 additions & 8 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import PropTypes from 'proptypes';
import PropTypes from 'prop-types';
import { render as preactRender, cloneElement as preactCloneElement, h, Component as PreactComponent, options } from 'preact';

const version = '15.1.0'; // trick libraries to think we are react
Expand Down Expand Up @@ -514,14 +514,10 @@ function propsHook(props, context) {
if (DEV) {
let ctor = typeof this==='function' ? this : this.constructor,
propTypes = this.propTypes || ctor.propTypes;
const displayName = this.displayName || ctor.name;

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));
}
}
PropTypes.checkPropTypes(propTypes, props, 'prop', displayName);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Ah - this is how they want us to validate! That makes more sense.

}
}
}
Expand Down
34 changes: 17 additions & 17 deletions test/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,23 +173,23 @@ describe('components', () => {
});

describe('propTypes', () => {
function checkPropTypes(Foo) {
function checkPropTypes(Foo, name = 'Foo') {
sinon.stub(console, 'error');

React.render(<Foo />, scratch);
expect(console.error).to.have.been.calledWithMatch({
message: 'Required prop `func` was not specified in `Foo`.'
});
expect(console.error).to.have.been.calledWithMatch(
'Warning: Failed prop type: The prop `func` is marked as required in `' + name + '`, but its value is `undefined`.'
);
expect(console.error).to.have.been.called;

console.error.reset();

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

React.render(<Foo func={()=>{}} bool="one" />, scratch);
expect(console.error).to.have.been.calledWithMatch({
message: 'Invalid prop `bool` of type `string` supplied to `Foo`, expected `boolean`.'
});
expect(console.error).to.have.been.calledWithMatch(
'Warning: Failed prop type: Invalid prop `bool` of type `string` supplied to `' + name + '`, expected `boolean`.'
);

console.error.restore();
}
Expand All @@ -209,32 +209,32 @@ describe('components', () => {
});

it('should support propTypes for createClass components', () => {
const Foo = React.createClass({
const Bar = React.createClass({
propTypes: {
func: React.PropTypes.func.isRequired,
bool: React.PropTypes.bool
},
render: () => <div />
});

checkPropTypes(Foo);
checkPropTypes(Bar, 'Bar');
});

it('should support propTypes for pure components', () => {
function Foo() { return <div />; }
Foo.propTypes = {
function Baz() { return <div />; }
Baz.propTypes = {
func: React.PropTypes.func.isRequired,
bool: React.PropTypes.bool
};
checkPropTypes(Foo);
checkPropTypes(Baz, 'Baz');

const Foo2 = () => <div />;
Foo2.displayName = 'Foo';
Foo2.propTypes = {
const Bip = () => <div />;
Bip.displayName = 'Bip';
Bip.propTypes = {
func: React.PropTypes.func.isRequired,
bool: React.PropTypes.bool
};
checkPropTypes(Foo2);
checkPropTypes(Bip, 'Bip');
});
});

Expand Down