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 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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ render((

## Use Without Webpack/Browserify

`preact-compat` and its single dependency [`proptypes`](https://git.io/proptypes) are both published as UMD modules as of `preact-compat` version `0.6`. This means you can use them via a `<script>` tag without issue:
`preact-compat` and its single dependency [`proptypes`](https://github.com/reactjs/prop-types) are both published as UMD modules as of `preact-compat` version `0.6`. This means you can use them via a `<script>` tag without issue:

```html
<script src="//unpkg.com/preact"></script>
Expand All @@ -168,7 +168,7 @@ You can see the above in action with this [JSFiddle Example](https://jsfiddle.ne

### PropTypes

`preact-compat` adds support for validating PropTypes out of the box. This can be disabled the same way it is when using React, by defining a global `process.env.NODE_ENV='production'`. PropType errors should work the same as in React - the [`proptypes`](https://git.io/proptypes) module used here is extracted verbatim from the React source into a standalone module.
`preact-compat` adds support for validating PropTypes out of the box. This can be disabled the same way it is when using React, by defining a global `process.env.NODE_ENV='production'`. PropType errors should work the same as in React - the [`proptypes`](https://github.com/reactjs/prop-types) module used here is extracted verbatim from the React source into a standalone module.

<img src="http://i.imgur.com/tGT7Dvw.png" width="650" alt="PropType validation example output" />

Expand Down
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.7",
"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
11 changes: 3 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 @@ -515,13 +515,8 @@ function propsHook(props, context) {
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));
}
}
const displayName = this.displayName || ctor.name;
PropTypes.checkPropTypes(propTypes, props, 'prop', displayName);
}
}
}
Expand Down
8 changes: 2 additions & 6 deletions test/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,19 +177,15 @@ describe('components', () => {
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('Failed prop type: The prop `func` is marked as required in `Foo`, but its value is `undefined`.');

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('Failed prop type: Invalid prop `bool` of type `string` supplied to `Foo`, expected `boolean`.');

console.error.restore();
}
Expand Down