@@ -13,23 +13,44 @@ $ npm install --save-dev react-generate-props
1313
1414Define your component's propTypes.
1515
16+ ``` jsx
17+ const Counter = ({ count }) => < div> {count}< / div>
18+ Counter .propTypes = { count: PropTypes .number .isRequired }
19+ export default Counter
20+ ```
21+
22+ Pass the component to this library. It will generate reasonable props based on the defined types.
23+
1624``` js
17- // react-component.jsx
25+ import generateProps from ' react-generate-props'
26+ import Counter from ' ./counter'
27+ generateProps (Counter)
28+ // => { count: 1 }
29+ ```
30+
31+ Use these default props to reduce boilerplate and prop type validation errors in your tests! :tada :
32+
33+ ## Example
34+
35+ A more in-depth example.
36+
37+ ``` jsx
38+ // component.jsx
1839
1940class Component extends React .Component {
2041 static propTypes = {
21- title: React . PropTypes .string .isRequired ,
22- count : React . PropTypes .number .isRequired ,
23- user: React . PropTypes .shape ({
24- loggedIn: React . PropTypes .bool .isRequired ,
25- name: React . PropTypes .string .isRequired
42+ title: PropTypes .string .isRequired ,
43+ followers : PropTypes .number .isRequired ,
44+ user: PropTypes .shape ({
45+ loggedIn: PropTypes .bool .isRequired ,
46+ name: PropTypes .string .isRequired
2647 }).isRequired
2748 }
2849
2950 render () {
3051 < div>
3152 < h1> {this .props .title }< / h1>
32- < small> {this .props .count }< / small>
53+ < small> {this .props .followers }< / small>
3354 {this .props .user .loggedIn && < p> Hello, {this .props .user .name }.< / p> }
3455 < / div>
3556 }
@@ -38,29 +59,112 @@ class Component extends React.Component {
3859export default Component
3960```
4061
41- =====
42-
43- Then, pass your component to this library. It will generate reasonable defaults for all of your component's required propTypes.
44-
4562``` js
4663// component-test.js
4764
4865import generateProps from ' react-generate-props'
49- import Component from ' ./react- component'
66+ import Component from ' ./component.jsx '
5067
5168const props = generateProps (Component)
52- assertEqual (props, { title: ' A String ' , count : 1 , user: { loggedIn: true , name: ' A String ' } })
69+ assertEqual (props, { title: ' string ' , followers : 1 , user: { loggedIn: true , name: ' string ' } })
5370
5471render (< Component {... props}/ > )
5572
5673/** *
5774Result:
5875
5976<div>
60- <h1>A String </h1>
77+ <h1>string </h1>
6178 <small>1</small>
62- <p>Hello, A String </p>
79+ <p>Hello, string </p>
6380</div>
6481
6582***/
6683```
84+
85+ ## API
86+
87+ The library takes two arguments.
88+
89+ ``` js
90+ generateProps (schema, opts)
91+ ```
92+
93+ #### ` schema ` : An Object, Function, or Class containing a PropTypes definition. All of the following are valid:
94+
95+ Plain Object
96+ ``` js
97+ const Counter = { count: PropTypes .number .isRequired }
98+ ```
99+
100+ Plain Object with a ` propTypes ` key
101+ ``` js
102+ const Counter = { propTypes: { count: PropTypes .number .isRequired } }
103+ ```
104+
105+ Functional Component
106+ ``` js
107+ const Counter = ({ counter }) => {/* ... */ }
108+ Counter .propTypes = { count: PropTypes .number .isRequired }
109+ ```
110+
111+ ` React.Component ` Class
112+ ``` js
113+ class Counter extends React .Component {
114+ static propTypes = { count: PropTypes .number .isRequired }
115+ }
116+ ```
117+
118+ In each of these cases, the effect would be the same
119+ ``` js
120+ generateProps (Counter)
121+ // => { count: 1 }
122+ ```
123+
124+ #### ` opts ` : An Object which may contain the following:
125+
126+ ``` js
127+ {
128+ required: true ,
129+ // default: true. When true, props marked as .isRequired will be generated.
130+
131+ optional: false ,
132+ // default: false. When true, props *not* marked as .isRequired will be generated.
133+
134+ generators: {
135+ // Can be used to override this lib's default generators.
136+ // Each key is a prop type, each value is a function,
137+ // each function receives its definition's arguments.
138+ bool : () => false ,
139+ function : () => spy (),
140+ instanceOf : (klass ) => new klass (),
141+ oneOf : (values ) => values[values .length - 1 ]
142+ }
143+ }
144+ ```
145+
146+ ## One More Example
147+
148+ ``` jsx
149+ const propTypes = {
150+ name: PropTypes .string .isRequired ,
151+ loggedIn: PropTypes .bool ,
152+ userType: PropTypes .oneOf ([' admin' , ' user' ]).isRequired
153+ }
154+
155+ generateProps (propTypes)
156+ // => { name: 'string', userType: 'admin' }
157+
158+ generateProps (propTypes, { optional: true })
159+ // => { name: 'string', loggedIn: true, userType: 'admin' }
160+
161+ generateProps (propTypes, {
162+ optional: true ,
163+ generators: {
164+ string : () => ' Alice' ,
165+ bool : () => false ,
166+ oneOf : (values ) => values[values .length - 1 ]
167+ }
168+ })
169+ // => { name: 'Alice', loggedIn: false, userType: 'user' }
170+ ```
0 commit comments