Best JavaScript code snippet using storybook-root
prop-types.js
Source:prop-types.js  
1/* eslint-disable react/no-unused-prop-types */2import React from 'react';3// @ts-ignore4import PropTypes, { string, shape } from 'prop-types';5import momentPropTypes from 'react-moment-proptypes';6import { PRESET_SHAPE, SOME_PROP_TYPES } from './ext';7const NAMED_OBJECT = {8  text: PropTypes.string.isRequired,9  value: PropTypes.string.isRequired,10};11const ANOTHER_OBJECT = {12  foo: PropTypes.string,13  bar: PropTypes.string,14};15const NAMED_SHAPE = PropTypes.shape({16  foo: PropTypes.string,17});18export const POSITIONS = ['top-left', 'top-right', 'top-center'];19const FunctionalComponent = () => {20  return <div>FunctionnalComponent!</div>;21};22class ClassComponent extends React.PureComponent {23  render() {24    return <div>ClassComponent!</div>;25  }26}27function concat(a, b) {28  return a + b;29}30function customPropType() {31  return null;32}33const nestedCustomPropType = {34  custom: customPropType,35};36const SOME_INLINE_PROP_TYPES = {37  /**38   * Hey Hey!39   */40  inlineString: PropTypes.string,41  inlineBool: PropTypes.bool,42  inlineNumber: PropTypes.number,43  inlineObj: PropTypes.shape({44    foo: PropTypes.string,45  }),46  inlineArray: PropTypes.arrayOf(PropTypes.number),47  inlineArrayOfObjects: PropTypes.arrayOf({ foo: PropTypes.string }),48  inlineFunctionalElement: PropTypes.element,49  inlineFunctionalElementInline: PropTypes.element,50  inlineFunctionalElementInlineReturningNull: PropTypes.element,51  inlineHtmlElement: PropTypes.element,52  inlineFunctionalElementInlineWithProps: PropTypes.element,53  inlineFunctionalElementNamedInline: PropTypes.element,54  inlineClassElement: PropTypes.element,55  inlineClassElementWithProps: PropTypes.element,56  inlineClassElementWithChildren: PropTypes.element,57  inlineClassElementInline: PropTypes.element,58  inlineFunc: PropTypes.func,59};60const SOME_INLINE_DEFAULT_PROPS = {61  inlineString: 'Inline prop default value',62  inlineBool: true,63  inlineNumber: 10,64  inlineObj: { foo: 'bar' },65  inlineArray: [1, 2, 3],66  inlineArrayOfObjects: [67    { foo: 'bar' },68    { foo: 'bar' },69    { foo: 'bar' },70    { foo: 'bar' },71    { foo: 'bar' },72  ],73  inlineFunctionalElement: <FunctionalComponent />,74  inlineFunctionalElementInline: () => {75    return <div>Inlined FunctionnalComponent!</div>;76  },77  inlineFunctionalElementInlineReturningNull: () => {78    return null;79  },80  inlineHtmlElement: <div>Hey!</div>,81  // eslint-disable-next-line react/prop-types82  inlineFunctionalElementInlineWithProps: ({ foo }) => {83    return <div>{foo}</div>;84  },85  inlineFunctionalElementNamedInline: function InlinedFunctionalComponent() {86    return <div>Inlined FunctionnalComponent!</div>;87  },88  inlineClassElement: <ClassComponent />,89  inlineClassElementWithProps: <ClassComponent className="toto" />,90  inlineClassElementWithChildren: (91    <ClassComponent>92      <div>hey!</div>93    </ClassComponent>94  ),95  inlineClassElementInline: class InlinedClassComponent extends React.PureComponent {96    render() {97      return <div>Inlined ClassComponent!</div>;98    }99  },100  inlineFunc: function add(a, b) {101    return a + b;102  },103};104export const PropTypesProps = () => <div>PropTypes!</div>;105PropTypesProps.propTypes = {106  // eslint-disable-next-line react/forbid-prop-types107  any: PropTypes.any,108  bool: PropTypes.bool,109  string: PropTypes.string,110  func: PropTypes.func,111  /**112   * A function with JSDoc tags.113   *114   * @param {string} foo - A foo value.115   * @param {number} bar - A bar value.116   * @returns {ComplexObject} - Returns a complex object.117   */118  funcWithJsDoc: PropTypes.func,119  /**120   * @param {string} foo - A foo value.121   * @param {number} bar - A bar value.122   * @param {number} bar1 - A bar value.123   * @param {number} bar2 - A bar value.124   * @param {number} bar3 - A bar value.125   * @param {number} bar4 - A bar value.126   * @param {number} bar5 - A bar value.127   * @returns {ComplexObject} - Returns a complex object.128   */129  semiLongFuncWithJsDoc: PropTypes.func,130  /**131   * @param {string} foo - A foo value.132   * @param {number} bar - A bar value.133   * @param {number} bar1 - A bar value.134   * @param {number} bar2 - A bar value.135   * @param {number} bar3 - A bar value.136   * @param {number} bar4 - A bar value.137   * @param {number} bar5 - A bar value.138   * @param {number} bar6 - A bar value.139   * @param {number} bar7 - A bar value.140   * @param {number} bar8 - A bar value.141   * @param {number} bar9 - A bar value.142   * @param {number} bar10 - A bar value.143   * @returns {ComplexObject} - Returns a complex object.144   */145  veryLongFuncWithJsDoc: PropTypes.func,146  namedDefaultFunc: PropTypes.func,147  number: PropTypes.number,148  /**149   * Plain object propType (use shape!!)150   */151  obj: PropTypes.object, // eslint-disable-line react/forbid-prop-types152  symbol: PropTypes.symbol,153  node: PropTypes.node,154  useCustomPropType: customPropType,155  useNestedCustomPropType: nestedCustomPropType.custom,156  externalMomentPropType: momentPropTypes.momentObj,157  functionalElement: PropTypes.element,158  functionalElementInline: PropTypes.element,159  functionalElementNamedInline: PropTypes.element,160  classElement: PropTypes.element,161  classElementInline: PropTypes.element,162  functionalElementType: PropTypes.elementType,163  classElementType: PropTypes.elementType,164  elementWithProps: PropTypes.elementType,165  /**166   * `instanceOf` is also supported and the custom type will be shown instead of `instanceOf`167   */168  instanceOf: PropTypes.instanceOf(Set),169  /**170   * `oneOf` is basically an Enum which is also supported but can be pretty big.171   */172  oneOfString: PropTypes.oneOf(['News', 'Photos']),173  oneOfNumeric: PropTypes.oneOf([0, 1, 2, 3]),174  oneOfShapes: PropTypes.oneOf([175    PropTypes.shape({ foo: PropTypes.string }),176    PropTypes.shape({ bar: PropTypes.number }),177  ]),178  oneOfComplexShapes: PropTypes.oneOf([179    PropTypes.shape({180      /**181       *  Just an internal propType for a shape.182       *  It's also required, and as you can see it supports multi-line comments!183       */184      id: PropTypes.number.isRequired,185      /**186       *  A simple non-required function187       */188      func: PropTypes.func,189      /**190       * An `arrayOf` shape191       */192      arr: PropTypes.arrayOf(193        PropTypes.shape({194          /**195           * 5-level deep propType definition and still works.196           */197          index: PropTypes.number.isRequired,198        })199      ),200    }),201    shape({ bar: PropTypes.number }),202  ]),203  oneOfComplexType: PropTypes.oneOf([NAMED_OBJECT, ANOTHER_OBJECT]),204  oneOfComponents: PropTypes.oneOf([FunctionalComponent, ClassComponent]),205  oneOfEval: PropTypes.oneOf((() => ['News', 'Photos'])()),206  oneOfVar: PropTypes.oneOf(POSITIONS),207  oneOfNested: PropTypes.oneOf(['News', ['bottom-left', 'botton-center', 'bottom-right']]),208  oneOfNestedSimpleInlineObject: PropTypes.oneOf(['News', [{ foo: PropTypes.string }]]),209  oneOfNestedComplexInlineObject: PropTypes.oneOf([210    'News',211    [{ nested: { foo: PropTypes.string } }],212  ]),213  oneOfNestedComplexShape: PropTypes.oneOf([214    'News',215    [{ nested: PropTypes.shape({ foo: PropTypes.string }) }],216  ]),217  /**218   *  A multi-type prop is also valid and is displayed as `Union<String|Message>`219   */220  oneOfType: PropTypes.oneOfType([PropTypes.string, PropTypes.instanceOf(Set)]),221  /**222   * array of a primitive type223   */224  arrayOfPrimitive: PropTypes.arrayOf(PropTypes.number),225  arrayOfNamedObject: PropTypes.arrayOf(NAMED_OBJECT),226  arrayOfShortInlineObject: PropTypes.arrayOf({227    foo: PropTypes.string,228  }),229  arrayOfInlineObject: PropTypes.arrayOf({230    text: PropTypes.string.isRequired,231    value: PropTypes.string.isRequired,232  }),233  arrayOfComplexInlineObject: PropTypes.arrayOf({234    text: PropTypes.string.isRequired,235    value: PropTypes.string.isRequired,236    shape: {237      id: PropTypes.string.isRequired,238      age: PropTypes.number.isRequired,239    },240  }),241  arrayOfShortShape: PropTypes.arrayOf(242    PropTypes.shape({243      bar: PropTypes.string,244    })245  ),246  arrayOfComplexShape: PropTypes.arrayOf(247    PropTypes.shape({248      /**249       *  Just an internal propType for a shape.250       *  It's also required, and as you can see it supports multi-line comments!251       */252      id: PropTypes.number.isRequired,253      /**254       *  A simple non-required function255       */256      func: PropTypes.func,257      /**258       * An `arrayOf` shape259       */260      arr: PropTypes.arrayOf(261        PropTypes.shape({262          /**263           * 5-level deep propType definition and still works.264           */265          index: PropTypes.number.isRequired,266        })267      ),268    })269  ),270  arrayExternalShape: PropTypes.arrayOf(PropTypes.shape(PRESET_SHAPE)),271  /**272   *  A simple `objectOf` propType.273   */274  simpleObjectOf: PropTypes.objectOf(PropTypes.number),275  objectOfShortInlineObject: PropTypes.objectOf({276    foo: PropTypes.string,277  }),278  objectOfInlineObject: PropTypes.objectOf({279    foo: PropTypes.string,280    bar: PropTypes.string,281    barry: PropTypes.string,282  }),283  objectOfShortShape: PropTypes.objectOf(284    PropTypes.shape({285      foo: string,286    })287  ),288  /**289   *  A very complex `objectOf` propType.290   */291  objectOfComplexShape: PropTypes.objectOf(292    PropTypes.shape({293      /**294       *  Just an internal propType for a shape.295       *  It's also required, and as you can see it supports multi-line comments!296       */297      id: PropTypes.number.isRequired,298      /**299       *  A simple non-required function300       */301      func: PropTypes.func,302      /**303       * An `arrayOf` shape304       */305      arr: PropTypes.arrayOf(306        PropTypes.shape({307          /**308           * 5-level deep propType definition and still works.309           */310          index: PropTypes.number.isRequired,311        })312      ),313    })314  ),315  namedObjectOf: PropTypes.objectOf(NAMED_OBJECT),316  shapeShort: PropTypes.shape({317    foo: string,318  }),319  shapeLong: PropTypes.shape({320    foo: string,321    prop1: string,322    prop2: string,323    prop3: string,324    prop4: string,325    prop5: string,326    prop6: string,327    prop7: string,328  }),329  /**330   * propType for shape with nested arrayOf331   *332   * Also, multi-line description333   */334  shapeComplex: PropTypes.shape({335    /**336     *  Just an internal propType for a shape.337     *  It's also required, and as you can see it supports multi-line comments!338     */339    id: PropTypes.number.isRequired,340    /**341     *  A simple non-required function342     */343    func: PropTypes.func,344    /**345     * An `arrayOf` shape346     */347    arr: PropTypes.arrayOf(348      PropTypes.shape({349        /**350         * 5-level deep propType definition and still works.351         */352        index: PropTypes.number.isRequired,353      })354    ),355    shape: PropTypes.shape({356      shape: PropTypes.shape({357        foo: PropTypes.string,358        oneOf: PropTypes.oneOf(['one', 'two']),359      }),360    }),361    oneOf: PropTypes.oneOf(['one', 'two']),362  }),363  shapeWithArray: PropTypes.shape({364    arr: PropTypes.arrayOf({ foo: PropTypes.string }),365  }),366  namedShape: NAMED_SHAPE,367  namedObjectInShape: PropTypes.shape(NAMED_OBJECT),368  exact: PropTypes.exact({369    name: PropTypes.string,370    quantity: PropTypes.number,371  }),372  namedExact: PropTypes.exact(NAMED_OBJECT),373  /**374   * test string with a comment that has375   * two identical lines376   * two identical lines377   */378  optionalString: PropTypes.string,379  requiredString: PropTypes.string.isRequired,380  nullDefaultValue: PropTypes.string,381  undefinedDefaultValue: PropTypes.string,382  ...SOME_INLINE_PROP_TYPES,383  ...SOME_PROP_TYPES,384};385PropTypesProps.defaultProps = {386  any: 'Default any',387  bool: false,388  string: 'Default string',389  func: () => {},390  funcWithJsDoc: (foo, bar) => {391    // eslint-disable-next-line392    const yo = window.document;393    // eslint-disable-next-line394    const pouf = souffle;395    return { foo, bar };396  },397  namedDefaultFunc: concat,398  number: 5,399  obj: {400    key: 'value',401  },402  symbol: Symbol('Default symbol'),403  node: <div>Hello!</div>,404  functionalElement: <FunctionalComponent className="toto" />,405  functionalElementInline: () => {406    return <div>Inlined FunctionnalComponent!</div>;407  },408  functionalElementNamedInline: function InlinedFunctionalComponent() {409    return <div>Inlined FunctionnalComponent!</div>;410  },411  classElement: <ClassComponent />,412  classElementInline: class InlinedClassComponent extends React.PureComponent {413    render() {414      return <div>Inlined ClassComponent!</div>;415    }416  },417  functionalElementType: FunctionalComponent,418  classElementType: ClassComponent,419  elementWithProps: <ClassComponent className="w8 h8 fill-marine-500" />,420  instanceOf: new Set(),421  oneOfString: 'News',422  oneOfNumeric: 1,423  oneOfShapes: { foo: 'bar' },424  oneOfComplexShapes: {425    thing: {426      id: 2,427      func: () => {},428      arr: [],429    },430  },431  oneOfComplexType: { text: 'foo', value: 'bar' },432  oneOfComponents: <FunctionalComponent />,433  oneOfEval: 'Photos',434  oneOfVar: 'top-right',435  oneOfNested: 'top-right',436  oneOfType: 'hello',437  arrayOfPrimitive: [1, 2, 3],438  arrayOfString: ['0px', '0px'],439  arrayOfNamedObject: [{ text: 'foo', value: 'bar' }],440  arrayOfShortInlineObject: [{ foo: 'bar' }],441  arrayOfInlineObject: [{ text: 'foo', value: 'bar' }],442  arrayOfComplexInlineObject: [{ text: 'foo', value: 'bar' }],443  arrayOfShortShape: [{ bar: 'foo' }],444  arrayOfComplexShape: [445    {446      thing: {447        id: 2,448        func: () => {},449        arr: [],450      },451    },452  ],453  simpleObjectOf: { key: 1 },454  objectOfShortInlineObject: { foo: 'bar' },455  objectOfInlineObject: { foo: 'bar', bar: 'foo' },456  objectOfShortShape: { foo: 'bar' },457  objectOfComplexShape: {458    thing: {459      id: 2,460      func: () => {},461      arr: [],462    },463  },464  namedObjectOf: { text: 'foo', value: 'bar' },465  shapeShort: { foo: 'bar' },466  shapeComplex: {467    id: 3,468    func: () => {},469    arr: [],470    shape: {471      shape: {472        foo: 'bar',473      },474    },475  },476  namedShape: { foo: 'bar' },477  namedObjectInShape: { text: 'foo', value: 'bar' },478  exact: { name: 'foo', quantity: 2 },479  namedExact: { text: 'foo', value: 'bar' },480  optionalString: 'Default String',481  nullDefaultValue: null,482  undefinedDefaultValue: undefined,483  ...SOME_INLINE_DEFAULT_PROPS,...Using AI Code Generation
1import React from 'react';2import { storiesOf } from '@storybook/react';3import { withInfo } from '@storybook/addon-info';4import { action } from '@storybook/addon-actions';5import { Button } from '@storybook/react/demo';6storiesOf('Button', module)7  .addDecorator((story, context) => withInfo('common info')(story)(context))8  .add('with text', () => (9    <Button onClick={action('clicked')}>Hello Button</Button>10  .add('with some emoji', () => (11    <Button onClick={action('clicked')}>😀 😎 👍 💯</Button>12  ));13import React from 'react';14import { storiesOf } from '@storybook/react';15import { withInfo } from '@storybook/addon-info';16import { action } from '@storybook/addon-actions';17import { Button } from '@storybook/react/demo';18storiesOf('Button', module)19  .addDecorator((story, context) => withInfo('common info')(story)(context))20  .add('with text', () => (21    <Button onClick={action('clicked')}>Hello Button</Button>22  .add('with some emoji', () => (23    <Button onClick={action('clicked')}>😀 😎 👍 💯</Button>24  ));25import React from 'react';26import { storiesOf } from '@storybook/react';27import { withInfo } from '@storybook/addon-info';28import { action } from '@storybook/addon-actions';29import { Button } from '@storybook/react/demo';30storiesOf('Button', module)31  .addDecorator((story, context) => withInfo('common info')(story)(context))32  .add('with text', () => (33    <Button onClick={action('clicked')}>Hello Button</Button>34  .add('with some emoji', () => (35    <Button onClick={action('clicked')}>😀 😎 👍 💯</Button>36  ));37import React from 'react';38import { storiesOf } from '@storybook/react';39import { withInfo } from '@storybook/addon-info';40import { action } from '@Using AI Code Generation
1import { PropTypesProps } from 'storybook-root';2import { PropTypesProps } from 'storybook-root';3import { PropTypesProps } from 'storybook-root';4import { PropTypesProps } from 'storybook-root';5import { PropTypesProps } from 'storybook-root';6import { PropTypesProps } from 'storybook-root';7import { PropTypesProps } from 'storybook-root';8import { PropTypesProps } from 'storybook-root';9import { PropTypesProps } from 'storybook-root';10import { PropTypesProps } from 'storybook-root';11import { PropTypesProps } from 'storybook-root';12import { PropTypesProps } from 'storybook-root';13import { PropTypesProps } from 'storybook-root';14import { PropTypesProps } from 'storybook-root';15import { PropTypesProps } from 'storybook-root';16import { PropTypesProps } from 'storybook-root';17import { PropTypesProps } from 'storybook-root';18import { PropTypesProps } from 'storybook-root';19import { PropTypesProps } from 'storybook-root';20import { PropTypesProps } from 'storybook-root';21import { PropTypesProps } from 'storybook-root';22import { PropTypesProps } from 'storybook-root';23import { PropTypesProps } from 'storybook-root';Using AI Code Generation
1import PropTypes from 'prop-types';2import { storiesOf } from '@storybook/react';3import { withInfo } from '@storybook/addon-info';4import Button from '../src/components/Button';5const stories = storiesOf('Button', module);6stories.add(7  withInfo({8  })(() => <Button>Click Here</Button>)9);Using AI Code Generation
1import { PropTypesProps } from 'storybook-root-decorator';2PropTypesProps.propTypes = {3  fifteenthProp: PropTypes.oneOf(['News', 'Photos']),4  sixteenthProp: PropTypes.oneOf(['News', 'Photos']).isRequired,5  seventeenthProp: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),6  eighteenthProp: PropTypes.oneOfType([PropTypes.string, PropTypes.number]).isRequired,7  nineteenthProp: PropTypes.arrayOf(PropTypes.number),8  twentiethProp: PropTypes.arrayOf(PropTypes.number).isRequired,9  twentyFirstProp: PropTypes.objectOf(PropTypes.number),10  twentySecondProp: PropTypes.objectOf(PropTypes.number).isRequired,11  twentyThirdProp: PropTypes.shape({12  }),13  twentyFourthProp: PropTypes.shape({Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
