1 | /*
|
2 | * The MIT License (MIT)
|
3 | *
|
4 | * Copyright (c) 2015 - present Instructure, Inc.
|
5 | *
|
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
|
7 | * of this software and associated documentation files (the "Software"), to deal
|
8 | * in the Software without restriction, including without limitation the rights
|
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
10 | * copies of the Software, and to permit persons to whom the Software is
|
11 | * furnished to do so, subject to the following conditions:
|
12 | *
|
13 | * The above copyright notice and this permission notice shall be included in all
|
14 | * copies or substantial portions of the Software.
|
15 | *
|
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
22 | * SOFTWARE.
|
23 | */
|
24 | import { decorator } from '@instructure/ui-decorator';
|
25 | import { logWarn as warn } from '@instructure/console';
|
26 | /**
|
27 | * ---
|
28 | * category: utilities/react
|
29 | * ---
|
30 | * Flag React component props as hack props.
|
31 | * Warnings will display in the console when hack props are used.
|
32 | *
|
33 | * ```js
|
34 | * class Example extends Component {
|
35 | * static propTypes = {
|
36 | * currentProp: PropTypes.func
|
37 | * }
|
38 | * }
|
39 | * export default hack(['hackProp'])(Example)
|
40 | * ```
|
41 | *
|
42 | * @module hack
|
43 | * @param {array} hackProps
|
44 | * @param {string} message
|
45 | * @return {function} React component flagged as having hack props
|
46 | */
|
47 |
|
48 | const hack = process.env.NODE_ENV == 'production' ? () => Component => Component : decorator((ComposedComponent, hackProps, message) => {
|
49 | return class HackComponent extends ComposedComponent {
|
50 | componentDidMount() {
|
51 | if (hackProps) {
|
52 | warnHackProps(ComposedComponent.name, this.props, hackProps, message);
|
53 | }
|
54 |
|
55 | if (super.componentDidMount) {
|
56 | super.componentDidMount();
|
57 | }
|
58 | }
|
59 |
|
60 | componentDidUpdate(prevProps, prevState, prevContext) {
|
61 | if (hackProps) {
|
62 | warnHackProps(ComposedComponent.name, this.props, hackProps, message);
|
63 | }
|
64 |
|
65 | if (super.componentDidUpdate) {
|
66 | super.componentDidUpdate(prevProps, prevState, prevContext);
|
67 | }
|
68 | }
|
69 |
|
70 | };
|
71 | });
|
72 |
|
73 | function warnHackProps(name, props, hackProps) {
|
74 | let message = arguments.length > 3 && arguments[3] !== void 0 ? arguments[3] : '';
|
75 | hackProps.forEach(hackProp => {
|
76 | warn(typeof props[hackProp] === 'undefined', `[${name}] The \`${hackProp}\` prop is a temporary hack and will be removed in a future release. ${message}`);
|
77 | });
|
78 | }
|
79 |
|
80 | export default hack;
|
81 | export {
|
82 | /**
|
83 | * ---
|
84 | * category: utilities/react
|
85 | * ---
|
86 | * Flag React component props as hack props.
|
87 | * Warnings will display in the console when hack props are used.
|
88 | *
|
89 | * ```js
|
90 | * class Example extends Component {
|
91 | * static propTypes = {
|
92 | * currentProp: PropTypes.func
|
93 | * }
|
94 | * }
|
95 | * export default hack(['hackProp'])(Example)
|
96 | * ```
|
97 | *
|
98 | * @module hack
|
99 | * @param {array} hackProps
|
100 | * @param {string} message
|
101 | * @return {function} React component flagged as having hack props
|
102 | */
|
103 | hack }; |
\ | No newline at end of file |