add proptype
This commit is contained in:
parent
28d823cfbf
commit
46213ae083
|
@ -0,0 +1,22 @@
|
|||
import createChainableTypeChecker from './utils/createChainableTypeChecker';
|
||||
|
||||
export default function all(...validators) {
|
||||
function allPropTypes(...args) {
|
||||
let error = null;
|
||||
|
||||
validators.forEach(validator => {
|
||||
if (error != null) {
|
||||
return;
|
||||
}
|
||||
|
||||
const result = validator(...args);
|
||||
if (result != null) {
|
||||
error = result;
|
||||
}
|
||||
});
|
||||
|
||||
return error;
|
||||
}
|
||||
|
||||
return createChainableTypeChecker(allPropTypes);
|
||||
}
|
|
@ -0,0 +1,32 @@
|
|||
import React from 'react';
|
||||
|
||||
import createChainableTypeChecker from './utils/createChainableTypeChecker';
|
||||
|
||||
function validate(props, propName, componentName, location, propFullName) {
|
||||
const propValue = props[propName];
|
||||
const propType = typeof propValue;
|
||||
|
||||
if (React.isValidElement(propValue)) {
|
||||
return new Error(
|
||||
`Invalid ${location} \`${propFullName}\` of type ReactElement ` +
|
||||
`supplied to \`${componentName}\`, expected a ReactComponent or a ` +
|
||||
'DOMElement. You can usually obtain a ReactComponent or DOMElement ' +
|
||||
'from a ReactElement by attaching a ref to it.'
|
||||
);
|
||||
}
|
||||
|
||||
if (
|
||||
(propType !== 'object' || typeof propValue.render !== 'function') &&
|
||||
propValue.nodeType !== 1
|
||||
) {
|
||||
return new Error(
|
||||
`Invalid ${location} \`${propFullName}\` of value \`${propValue}\` ` +
|
||||
`supplied to \`${componentName}\`, expected a ReactComponent or a ` +
|
||||
'DOMElement.'
|
||||
);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
export default createChainableTypeChecker(validate);
|
|
@ -0,0 +1,35 @@
|
|||
import warning from 'warning';
|
||||
|
||||
let warned = {};
|
||||
|
||||
export default function deprecated(validator, reason) {
|
||||
return function validate(
|
||||
props, propName, componentName, location, propFullName, ...args
|
||||
) {
|
||||
const componentNameSafe = componentName || '<<anonymous>>';
|
||||
const propFullNameSafe = propFullName || propName;
|
||||
|
||||
if (props[propName] != null) {
|
||||
const messageKey = `${componentName}.${propName}`;
|
||||
|
||||
warning(warned[messageKey],
|
||||
`The ${location} \`${propFullNameSafe}\` of ` +
|
||||
`\`${componentNameSafe}\` is deprecated. ${reason}.`
|
||||
);
|
||||
|
||||
warned[messageKey] = true;
|
||||
}
|
||||
|
||||
return validator(
|
||||
props, propName, componentName, location, propFullName, ...args
|
||||
);
|
||||
};
|
||||
}
|
||||
|
||||
/* eslint-disable no-underscore-dangle */
|
||||
function _resetWarned() {
|
||||
warned = {};
|
||||
}
|
||||
|
||||
deprecated._resetWarned = _resetWarned;
|
||||
/* eslint-enable no-underscore-dangle */
|
|
@ -0,0 +1,28 @@
|
|||
import React from 'react';
|
||||
|
||||
import createChainableTypeChecker from './utils/createChainableTypeChecker';
|
||||
|
||||
function elementType(props, propName, componentName, location, propFullName) {
|
||||
const propValue = props[propName];
|
||||
const propType = typeof propValue;
|
||||
|
||||
if (React.isValidElement(propValue)) {
|
||||
return new Error(
|
||||
`Invalid ${location} \`${propFullName}\` of type ReactElement ` +
|
||||
`supplied to \`${componentName}\`, expected an element type (a string ` +
|
||||
'or a ReactClass).'
|
||||
);
|
||||
}
|
||||
|
||||
if (propType !== 'function' && propType !== 'string') {
|
||||
return new Error(
|
||||
`Invalid ${location} \`${propFullName}\` of value \`${propValue}\` ` +
|
||||
`supplied to \`${componentName}\`, expected an element type (a string ` +
|
||||
'or a ReactClass).'
|
||||
);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
export default createChainableTypeChecker(elementType);
|
|
@ -1 +1,6 @@
|
|||
export all from './all';
|
||||
export componentOrElement from './componentOrElement';
|
||||
export deprecated from './deprecated';
|
||||
export elementType from './elementType';
|
||||
export isRequiredForA11y from './isRequiredForA11y';
|
||||
export splitComponent from './splitComponent';
|
||||
|
|
|
@ -0,0 +1,20 @@
|
|||
export default function isRequiredForA11y(validator) {
|
||||
return function validate(
|
||||
props, propName, componentName, location, propFullName, ...args
|
||||
) {
|
||||
const componentNameSafe = componentName || '<<anonymous>>';
|
||||
const propFullNameSafe = propFullName || propName;
|
||||
|
||||
if (props[propName] == null) {
|
||||
return new Error(
|
||||
`The ${location} \`${propFullNameSafe}\` is required to make ` +
|
||||
`\`${componentNameSafe}\` accessible for users of assistive ` +
|
||||
'technologies such as screen readers.'
|
||||
);
|
||||
}
|
||||
|
||||
return validator(
|
||||
props, propName, componentName, location, propFullName, ...args
|
||||
);
|
||||
};
|
||||
}
|
|
@ -0,0 +1,50 @@
|
|||
/**
|
||||
* Copyright 2013-present, Facebook, Inc.
|
||||
* All rights reserved.
|
||||
*
|
||||
* This source code is licensed under the BSD-style license found in the
|
||||
* LICENSE file in the root directory of this source tree. An additional grant
|
||||
* of patent rights can be found in the PATENTS file in the same directory.
|
||||
*/
|
||||
|
||||
// Mostly taken from ReactPropTypes.
|
||||
|
||||
export default function createChainableTypeChecker(validate) {
|
||||
function checkType(
|
||||
isRequired,
|
||||
props,
|
||||
propName,
|
||||
componentName,
|
||||
location,
|
||||
propFullName,
|
||||
...args
|
||||
) {
|
||||
const componentNameSafe = componentName || '<<anonymous>>';
|
||||
const propFullNameSafe = propFullName || propName;
|
||||
|
||||
if (props[propName] == null) {
|
||||
if (isRequired) {
|
||||
return new Error(
|
||||
`Required ${location} \`${propFullNameSafe}\` was not specified ` +
|
||||
`in \`${componentNameSafe}\`.`
|
||||
);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
return validate(
|
||||
props,
|
||||
propName,
|
||||
componentNameSafe,
|
||||
location,
|
||||
propFullNameSafe,
|
||||
...args
|
||||
);
|
||||
}
|
||||
|
||||
const chainedCheckType = checkType.bind(null, false);
|
||||
chainedCheckType.isRequired = checkType.bind(null, true);
|
||||
|
||||
return chainedCheckType;
|
||||
}
|
|
@ -7,13 +7,15 @@
|
|||
"node": ">=4.0.0"
|
||||
},
|
||||
"homepage": "https://github.com/tinper-bee/tinper-bee-core",
|
||||
"author": "",
|
||||
"author": "zhoubyc",
|
||||
"repository": "http://github.com/tinper-bee/tinper-bee-core",
|
||||
"bugs": "https://github.com/tinper-bee/tinper-bee-core/issues",
|
||||
"keywords": "tinper bee react util",
|
||||
"scripts": {
|
||||
"test": "test"
|
||||
},
|
||||
"author": "zhoubyc",
|
||||
"license": "MIT"
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"warning": "^3.0.0"
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue