facebook / prop-types

Runtime type checking for React props and similar objects
MIT License
4.48k stars 356 forks source link

Add Disabled or None type #315

Open arxeiss opened 4 years ago

arxeiss commented 4 years ago

When I create a custom component input and I'm passing all props from the parent, sometimes I want to disable some of them. In the example below I don't want to allow the user to be able to override type. But it is not possible now. I would like to add something like PT.disabled to warn users, that this prop is prohibited to change.

const NumberInput = (props) => {
  const { type, className, ...rest } = props;

  return <input
    type="number"
    {...rest}
    className={ classnames(inputStyles.number) + ' ' + className }
  />;
};

NumberInput.propTypes = {
  className: PT.string,
  type: PT.disabled, // Here, warn user type is not available property
};

export default NumberInput;

Would it be possible to add something like this?

ljharb commented 4 years ago

https://npmjs.com/airbnb-prop-types has this as restrictedProp.

I don't think it's likely to be worth adding new stuff here; I'd suggest using that library instead.