BVB Source Codes

prettier Show proptype_oneOfType.js Source code

Return Download prettier: download proptype_oneOfType.js Source code - Download prettier Source code - Type:.js
  1. /* @flow */
  2.  
  3. var React = require('react');
  4. var Example = React.createClass({
  5.   propTypes: {
  6.     prop: React.PropTypes.oneOfType([
  7.       React.PropTypes.string,
  8.       React.PropTypes.number
  9.     ]).isRequired
  10.   },
  11.   render() {
  12.     if (typeof this.props.prop === "string") {
  13.       return <div>{this.props.prop}</div>
  14.     } else {
  15.       return <div>{this.props.prop.toFixed(2)}</div>
  16.     }
  17.   }
  18. });
  19.  
  20. var ok_number = <Example prop={42} />;
  21. var ok_string = <Example prop="bar" />;
  22.  
  23. var fail_missing = <Example />;
  24. var fail_bool = <Example prop={true} />;
  25.  
  26. /* Since the proptype arguments are not required, React will actually allow
  27.    `null` and `undefined` elements in the `prop` prop, but Flow has currently
  28.    ignores the innter prop types' required flags. */
  29. var todo_required = <Example prop={null} />;
  30.  
  31. var OptionalExample = React.createClass({
  32.   propTypes: {
  33.     p: React.PropTypes.oneOfType([
  34.       React.PropTypes.string,
  35.     ]),
  36.   },
  37. });
  38.  
  39. (<OptionalExample />); // OK
  40. (<OptionalExample p="" />); // OK
  41. (<OptionalExample p={0} />); // error: number ~> string
  42.  
  43. var EmptyExample = React.createClass({
  44.   propTypes: {
  45.     nil: React.PropTypes.oneOfType([]), // i.e., `empty`
  46.   },
  47. });
  48.  
  49. (<EmptyExample nil={0} />); // number ~> empty
  50.  
  51. var AnyArrayExample = React.createClass({
  52.   propTypes: {
  53.     any: React.PropTypes.oneOfType((0:any)),
  54.   },
  55. });
  56.  
  57. (<AnyArrayExample any={0} />); // OK
  58.  
  59. var AnyElemExample = React.createClass({
  60.   propTypes: {
  61.     any: React.PropTypes.oneOfType([
  62.       React.PropTypes.string,
  63.       (0:any),
  64.     ]),
  65.   },
  66. });
  67.  
  68. (<AnyElemExample any={0} />); // OK
  69.  
  70. var DynamicArrayExample = React.createClass({
  71.   propTypes: {
  72.     dyn: React.PropTypes.oneOfType(([]: Array<Function>)),
  73.   },
  74. });
  75.  
  76. (<DynamicArrayExample dyn={0} />); // OK
  77.  
  78. var DynamicElemExample = React.createClass({
  79.   propTypes: {
  80.     dyn: React.PropTypes.oneOfType([
  81.       React.PropTypes.string,
  82.       (() => {}: Function),
  83.     ]),
  84.   },
  85. });
  86.  
  87. (<DynamicElemExample dyn={0} />); // OK
  88.  
  89. var InvalidArrayExample = React.createClass({
  90.   propTypes: {
  91.     p: React.PropTypes.oneOfType(0), // error: expected array, got 0
  92.   },
  93. });
  94.  
  95. (<InvalidArrayExample p={0} />); // OK, don't cascade errors
  96.  
  97. var InvalidElemExample = React.createClass({
  98.   propTypes: {
  99.     p: React.PropTypes.oneOfType([{}]), // error: expected prop type, got {}
  100.   },
  101. });
  102.  
  103. (<InvalidElemExample p={0} />); // OK, don't cascade errors
  104.  
downloadproptype_oneOfType.js Source code - Download prettier Source code
Related Source Codes/Software:
storybook - 2017-06-07
ionicons - The premium icon font for Ionic ... 2017-06-07
AsyncDisplayKit - Smooth asynchronous user interfaces for iOS apps. ... 2017-06-07
lottie-android - Render After Effects animations natively on Androi... 2017-06-07
parse-server - Parse-compatible API server module for Node/Expres... 2017-06-07
inferno - An extremely fast, React-like JavaScript library f... 2017-06-08
guetzli - Perceptual JPEG encoder 2017-06-08
cs-video-courses - List of Computer Science courses with video lectur... 2017-06-08
interviews - Everything you need to know to get the job. 2017-06-08
prepack - Prepack is a partial evaluator for JavaScript. Pre... 2017-06-08
CRYENGINE - CRYENGINE is a powerful real-time game development... 2017-06-11
postal - 2017-06-11
reactide - Reactide is the first dedicated IDE for React web ... 2017-06-11
rkt - rkt is a pod-native container engine for Linux. It... 2017-06-11
uWebSockets - Tiny WebSockets https://for... 2017-06-11
realworld - TodoMVC for the RealWorld - Exemplary fullstack Me... 2017-06-11
goreplay - GoReplay is an open-source tool for capturing and ... 2017-06-10
pyenv - Simple Python version management 2017-06-10
redux-saga - An alternative side effect model for Redux apps ... 2017-06-10
angular-starter - 2017-06-10

 Back to top