You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

3 年之前
1234567891011121314151617181920212223242526272829303132333435363738394041
  1. /**
  2. * Wraps a singular React.PropTypes.[type] with
  3. * a console.warn call that is only called if the
  4. * prop is not undefined/null and is only called
  5. * once.
  6. * @param {Object} propType React.PropType type
  7. * @param {String} message Deprecation message
  8. * @return {Function} ReactPropTypes checkType
  9. */
  10. export function deprecate(propType, message) {
  11. let warned = false;
  12. return function(...args) {
  13. const [props, propName] = args;
  14. const prop = props[propName];
  15. if (prop !== undefined && prop !== null && !warned) {
  16. warned = true;
  17. console.warn(message);
  18. }
  19. return propType.call(this, ...args);
  20. };
  21. }
  22. /**
  23. * Returns a copy of `PropTypes` with an `isDeprecated`
  24. * method available on all top-level propType options.
  25. * @param {React.PropTypes} PropTypes
  26. * @return {React.PropTypes} newPropTypes
  27. */
  28. export function addIsDeprecated(PropTypes) {
  29. let newPropTypes = {...PropTypes};
  30. for (const type in newPropTypes) {
  31. if (newPropTypes.hasOwnProperty(type)) {
  32. let propType = newPropTypes[type];
  33. propType = propType.bind(newPropTypes);
  34. propType.isDeprecated = deprecate.bind(newPropTypes, propType);
  35. newPropTypes[type] = propType;
  36. }
  37. }
  38. return newPropTypes;
  39. }