UNPKG

2.14 kBJavaScriptView Raw
1import _typeof from 'babel-runtime/helpers/typeof';
2import * as PT from 'prop-types';
3import { TIME_PICKER_TYPE, TIME_INPUT_TYPE } from './constant';
4import { datejs } from '../util';
5
6export var error = function error(propName, ComponentName) {
7 return new Error('Invalid prop ' + propName + ' supplied to ' + ComponentName + '. Validation failed.');
8};
9
10function checkType(type) {
11 return function (props, propName, componentName) {
12 var value = props[propName];
13 if (value) {
14 if (!Array.isArray(value)) {
15 value = [value];
16 }
17
18 if (!Array.isArray(type)) {
19 type = [type];
20 }
21
22 if (!value.every(function (v) {
23 return type.includes(typeof v === 'undefined' ? 'undefined' : _typeof(v));
24 })) {
25 throw error(propName, componentName);
26 }
27 }
28 };
29}
30
31var SharedPT = {
32 date: function date(props, propName, componentName) {
33 if (propName in props && !datejs(props.propName).isValid()) {
34 throw error(propName, componentName);
35 }
36 },
37 value: function value(props, propName, componentName) {
38 if (props[propName]) {
39 var value = props[propName];
40
41 if (props.type === TIME_PICKER_TYPE.RANGE && !Array.isArray(value)) {
42 throw error(propName, componentName);
43 } else if (!Array.isArray(value)) {
44 value = [value];
45 }
46
47 if (!value.every(function (v) {
48 return !v || datejs(v).isValid() || typeof v === 'string';
49 })) {
50 throw error(propName, componentName);
51 }
52 }
53 },
54
55 format: checkType(['string', 'function']),
56 inputValue: checkType('string'),
57 placeholder: checkType('string'),
58 readOnly: checkType('boolean'),
59 disabled: checkType('boolean'),
60
61 render: PT.oneOfType([PT.node, PT.func]),
62 type: PT.oneOf(Object.values(TIME_PICKER_TYPE)),
63 inputType: PT.oneOf(Object.values(TIME_INPUT_TYPE)),
64 size: PT.oneOf(['small', 'medium', 'large'])
65};
66
67export default SharedPT;
\No newline at end of file