Conditional prop for value another prop
camila-fran-gp opened this issue · 2 comments
camila-fran-gp commented
I need to condition a prop of type oneOf
according to the value of another prop, example:
Component.propTypes = {
propA: PropTypes.oneOf(['op1','op2','op3','op4', ]),
propB: (props) => {
if (props.propA === 'op1') {
return PropTypes.oneOf(['A1', 'A2', 'A3', 'A4'])
}
if (props.propA === 'op2') {
return PropTypes.oneOf(['B1', 'B2', 'B3', 'B4', 'B5'])
}
if(props.propA !== 'op1' || props.propA !== 'op2'){
return null //this prop should not exist
}
}
};
i tried with isRequiredIf
but it doesn't work :/
ljharb commented
https://npmjs.com/airbnb-prop-types has this, so it should be possible. I’d suggest looking at the code.
ljharb commented
Specifically, you can’t return oneOf directly - you have to invoke it and return the result. Don’t forget to pass every argument that your custom propType received.