Awesome Open Source
Awesome Open Source

react-required-if

React PropType to conditionally add .isRequired based on other props

Install

$ npm install --save react-required-if

Usage

import React, {PropTypes} from 'react';
import requiredIf from 'react-required-if';

export default class Component extends React.Component {
  static propTypes = {
    disabled: PropTypes.bool,
    onClick: requiredIf(PropTypes.func, props => !props.disabled)
  };

  render() {
    return <button onClick={this.props.onClick}>Click Me</button>
  }
}

Result:

import React from 'react';
import {render} from 'react-dom';
import Component from './Component';

render(
  <div>
    <Component onClick={() => {})/> // ok
    <Component disabled={true}/> // ok
    <Component/> // NOooooooo
  </div>,
  document.getElementById('root')
);

Get A Weekly Email With Trending Projects For These Topics
No Spam. Unsubscribe easily at any time.
javascript (68,779
react (5,308
validation (313
props (26

Find Open Source By Browsing 7,000 Topics Across 59 Categories