UNPKG

3.15 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.controllable = controllable;
7exports.default = void 0;
8
9/*
10 * The MIT License (MIT)
11 *
12 * Copyright (c) 2015 - present Instructure, Inc.
13 *
14 * Permission is hereby granted, free of charge, to any person obtaining a copy
15 * of this software and associated documentation files (the "Software"), to deal
16 * in the Software without restriction, including without limitation the rights
17 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
18 * copies of the Software, and to permit persons to whom the Software is
19 * furnished to do so, subject to the following conditions:
20 *
21 * The above copyright notice and this permission notice shall be included in all
22 * copies or substantial portions of the Software.
23 *
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
27 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
29 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30 * SOFTWARE.
31 */
32
33/**
34 * ---
35 * category: utilities/PropTypes
36 * ---
37 * Ensure that a corresponding handler function is provided for the given prop if the
38 * component does not manage its own state.
39 *
40 * ```js
41 * import { controllable } from '@instructure/ui-prop-types'
42 *
43 * class Foo extends Component {
44 * static propTypes = {
45 * selected: controllable(PropTypes.bool, 'onSelect', 'defaultSelected'),
46 * onSelect: PropTypes.func,
47 * defaultSelected: PropTypes.bool
48 * }
49 * ...
50 * ```
51 *
52 * This will throw an error if the 'selected' prop is supplied without a corresponding
53 * 'onSelect' handler and will recommend using 'defaultSelected' instead.
54 *
55 * @param {function} propType - validates the prop type. Returns null if valid, error otherwise
56 * @param {string} handlerName - name of the handler function
57 * @param {string} defaultPropName - name of the default prop
58 * @returns {Error} if designated prop is supplied without a corresponding handler function
59 */
60function controllable(propType) {
61 var handlerName = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : 'onChange';
62 var defaultPropName = arguments.length > 2 && arguments[2] !== void 0 ? arguments[2] : 'defaultValue';
63 return function (props, propName, componentName) {
64 var error = propType.apply(null, arguments);
65
66 if (error) {
67 return error;
68 }
69
70 if (props[propName] && typeof props[handlerName] !== 'function') {
71 return new Error(["You provided a '".concat(propName, "' prop without an '").concat(handlerName, "' handler on '").concat(componentName, "'. This will render a controlled component. If the component should be uncontrolled and manage its own state, use '").concat(defaultPropName, "'. Otherwise, set '").concat(handlerName, "'.")].join(''));
72 }
73 };
74}
75
76var _default = controllable;
77exports.default = _default;
\No newline at end of file