-
Notifications
You must be signed in to change notification settings - Fork 8
/
component-prop-types.js
60 lines (52 loc) · 2.12 KB
/
component-prop-types.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import Component from 'ember-component';
import PropTypes from 'prop-types';
const { checkPropTypes } = PropTypes;
/**
* Initializer is optimized for production using injected variables.
* These are either added to the window as globals during dev builds or injected into
* the build using UglifyJS during production builds.
*/
export function initialize() {
let propTypesExtends = {};
// Props validation included in dev only
// Add props validation checking during `didReceiveAttrs` hook with
// `checkPropTypes` utility method
if (NODE_ENV !== 'production') {
propTypesExtends.didReceiveAttrs = function() {
this._super(...arguments);
const propTypes = this.get('propTypes') || {};
// prop-types can handle calling `checkPropTypes` without any props, always call
checkPropTypes(
propTypes, // PropTypes definition
this.getProperties(...Object.keys(propTypes)), // Values to validate
'prop', // Type of validation that is occuring
this.toString() // Name of component for better debug messaging
);
};
}
// Default Props included by default, can be configured off
// Sets a default value for props during component initialization if undefined or
// null
if (INCLUDE_GET_DEFAULT_PROPS) {
propTypesExtends.init = function() {
this._super(...arguments);
// If default props are not configured, we're done
if (!this.get('getDefaultProps')) { return; }
let defaultProps = this.get('getDefaultProps')();
// If any prop with a default is undefined (undefined only), assign default
// value to prop. Do not set default for null, undefined means a variable has
// not been declared, null means a variable has been set to null.
for (let prop in defaultProps) {
if (this.get(prop) === undefined) { this.set(prop, defaultProps[prop]); }
}
};
}
// Only reopen Component class if adding props validation or getDefaultProps method
if (NODE_ENV !== 'production' || INCLUDE_GET_DEFAULT_PROPS) {
Component.reopen(propTypesExtends);
}
}
export default {
name: 'component-prop-types',
initialize
};