UNPKG

4.05 kBJavaScriptView Raw
1"use strict";
2
3var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
5Object.defineProperty(exports, "__esModule", {
6 value: true
7});
8exports.shorthandPropType = shorthandPropType;
9exports.ThemeablePropTypes = exports.default = void 0;
10
11var _propTypes = _interopRequireDefault(require("prop-types"));
12
13var _ThemeablePropValues = require("./ThemeablePropValues.js");
14
15/*
16 * The MIT License (MIT)
17 *
18 * Copyright (c) 2015 - present Instructure, Inc.
19 *
20 * Permission is hereby granted, free of charge, to any person obtaining a copy
21 * of this software and associated documentation files (the "Software"), to deal
22 * in the Software without restriction, including without limitation the rights
23 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
24 * copies of the Software, and to permit persons to whom the Software is
25 * furnished to do so, subject to the following conditions:
26 *
27 * The above copyright notice and this permission notice shall be included in all
28 * copies or substantial portions of the Software.
29 *
30 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
31 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
32 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
33 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
34 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
35 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
36 * SOFTWARE.
37 */
38var SHADOW_TYPES = _ThemeablePropValues.ThemeablePropValues.SHADOW_TYPES,
39 STACKING_TYPES = _ThemeablePropValues.ThemeablePropValues.STACKING_TYPES,
40 BORDER_WIDTHS = _ThemeablePropValues.ThemeablePropValues.BORDER_WIDTHS,
41 BORDER_RADII = _ThemeablePropValues.ThemeablePropValues.BORDER_RADII,
42 BACKGROUNDS = _ThemeablePropValues.ThemeablePropValues.BACKGROUNDS,
43 SIZES = _ThemeablePropValues.ThemeablePropValues.SIZES,
44 SPACING = _ThemeablePropValues.ThemeablePropValues.SPACING;
45/**
46 * ---
47 * category: utilities/themes
48 * ---
49 * Custom prop types for themeable React components.
50 * @module ThemeablePropTypes
51 */
52
53var ThemeablePropTypes = {
54 shadow: _propTypes.default.oneOf(Object.values(SHADOW_TYPES)),
55 stacking: _propTypes.default.oneOf(Object.values(STACKING_TYPES)),
56 borderWidth: shorthandPropType(Object.values(BORDER_WIDTHS)),
57 borderRadius: shorthandPropType(Object.values(BORDER_RADII)),
58 background: _propTypes.default.oneOf(Object.values(BACKGROUNDS)),
59 size: _propTypes.default.oneOf(Object.values(SIZES)),
60 spacing: shorthandPropType(Object.values(SPACING))
61};
62exports.ThemeablePropTypes = ThemeablePropTypes;
63
64function shorthandPropType(validValues) {
65 return function (props, propName, componentName, location) {
66 var propValue = props[propName];
67
68 if (typeof propValue === 'undefined') {
69 return;
70 }
71
72 var propValueType = typeof propValue;
73
74 if (propValueType !== 'string') {
75 return new Error("Invalid ".concat(location, " `").concat(propName, "` of type `").concat(propValueType, "` supplied to `").concat(componentName, "`, expected ") + "a string.");
76 }
77
78 var propValues = propValue.split(' ');
79 var valuesLength = propValues.length;
80
81 if (valuesLength > 0 && valuesLength < 5) {
82 for (var i = 0; i < valuesLength; i++) {
83 var valueIndex = validValues.indexOf(propValues[i]);
84
85 if (valueIndex === -1) {
86 return new Error("Invalid ".concat(location, " `").concat(propName, "` `").concat(propValues[i], "` supplied to `").concat(componentName, "`, expected ") + "a one of `".concat(validValues.join(', '), "`."));
87 }
88 }
89 } else {
90 return new Error("Invalid ".concat(location, " `").concat(propName, "` `").concat(propValue, "` supplied to `").concat(componentName, "`, expected ") + "between one and four of the following valid values: `".concat(validValues.join(', '), "`."));
91 }
92 };
93}
94
95var _default = ThemeablePropTypes;
96exports.default = _default;
\No newline at end of file