ljharb / prop-types-tools

Custom React PropType validators
MIT License
671 stars 50 forks source link

[New] Add disallowedIf PropType #45

Closed mhchen closed 6 years ago

mhchen commented 6 years ago

disallowedIf: wraps a proptype and accepts another prop name and another prop type. If the other prop is provided and matches the given type, the validator fails. Functionally similar to mutuallyExclusiveProps and mutallyExclusivePropTypes but is more flexible in the type being compared (although less flexible in the number of props that can be compared).

Example usage

const propTypes = {
  other: PropTypes.oneOf(['bar', 'baz']),
  foo: disallowedIf(PropTypes.string, 'other', PropTypes.oneOf(['bar'])),
};

const defaultProps = {
  other: 'baz',
  foo: null,
};

Pass

<MyComponent foo="Hello" />

<MyComponent foo="Hello" other="baz" />

Fail

<MyComponent foo="Hello" other="bar" />