You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
For 0.3.0+, you must use React 0.14.0+. You may use 0.2.0 for older versions.
11
+
### Full Documentation
12
12
13
-
### Documentation
13
+
*[Schema](#schema)
14
+
*[Dynamic Children and Keys](#dynamic-children-and-keys)
15
+
*[Component Mapping](#component-mapping)
16
+
*[Rendering](#rendering)
17
+
*[Complete Example](#complete-example)
14
18
15
19
#### Schema
16
20
17
21
The primary resource needed is a defined schema in JSON or a JavaScript object literal. It's recommended that schema attributes mainly define React component props. The parser explicitly handles the following attributes:
18
22
-**component**: _MUST_ exist and be defined by a string or React component (must be a string if describing a native HTML tag)
19
23
-**children**: _MAY_ exist to define sub-components
20
24
-**text**: _MAY_ exist to as a string to define inner HTML text (overrides children)
25
+
-**key**: _MAY_ exist to define a key for dynamic children
21
26
22
27
Example JSON schema (ES6)
23
28
```js
24
29
constschema= {
25
-
"component":"ContactForm",
26
-
"title":"Tell us a little about yourself...",
30
+
"component":"CommentList",
27
31
"children": [
28
32
{
29
-
"component":"StringField",
30
-
"label":"What's your name?"
33
+
"component":"Comment",
34
+
"author":"Pete Hunt",
35
+
"children":"This is one comment"
36
+
},
37
+
{
38
+
"component":"Comment",
39
+
"author":"Jordan Walke",
40
+
"children":"This is *another* comment"
31
41
},
32
42
{
33
43
"component":"a",
34
-
"href":"#faq",
35
-
"text":"I'm not sure why I'm filling this out"
44
+
"href":"#help",
45
+
"text":"I need help"
36
46
}
37
47
]
38
-
}
48
+
};
39
49
```
40
50
41
51
Example JS literal (ES6)
42
52
```js
43
-
constschema= {
44
-
"component": ContactForm,
45
-
"title":"Tell us a little about yourself...",
46
-
"children": [
47
-
{
48
-
"component": StringField,
49
-
"label":"What's your name?"
50
-
},
51
-
{
52
-
"component":"a",
53
-
"href":"#faq",
54
-
"text":"I'm not sure why I'm filling this out"
55
-
}
56
-
]
57
-
}
53
+
...
54
+
{
55
+
"component":Comment, // literal
56
+
"author":"Pete Hunt",
57
+
"children":"This is one comment"
58
+
},
59
+
...
58
60
```
59
61
60
62
##### Dynamic Children and Keys
61
63
62
-
When arrays of components exist (like children), react-json-schema will resolve a key for the element based on the array index, which follows the rules for [dynamic children](https://facebook.github.io/react/docs/multiple-components.html#dynamic-children). Custom keys cannot be defined at this time.
64
+
When arrays of components exist (like children), react-json-schema will resolve a key for the element, which follows the rules for [dynamic children](https://facebook.github.io/react/docs/multiple-components.html#dynamic-children). It will either use a custom key if defined, or resolve a numeric key based on the array index.
Also note react-json-schema does not perform any rendering, so the method in which you want to render is up to you. For example, you can use ReactDOMServer.render, ReactDOM.renderToString, etc. if you'd like.
100
+
Since react-json-schema does not perform any rendering, the method in which you want to render is up to you. For example, you can use ReactDOMServer.render, ReactDOM.renderToString, etc. if you'd like.
87
101
88
-
#### Putting it All Together
102
+
#### Complete Example
89
103
90
104
```js
91
105
importReactDOMfrom'react-dom';
92
106
importReactJsonSchemafrom'react-json-schema';
93
107
94
-
importFormStorefrom'./stores/FormStore';
95
-
importContactFormfrom'./components/ContactForm';
96
-
importStringFieldfrom'./components/StringField';
108
+
importFormStorefrom'FormStore';
109
+
importCommentListfrom'CommentList';
110
+
importCommentfrom'Comment';
97
111
98
112
// For this example, let's pretend I already have data and am ignorant of actions
99
113
constschema=FormStore.getFormSchema();
100
-
constcomponentMap={ ContactForm, StringField }
114
+
constview=newReactJsonSchema();
101
115
102
-
constcontactForm=newReactJsonSchema();
103
-
contactForm.setComponentMap(componentMap);
116
+
view.setComponentMap({ CommentList, Comment });
104
117
105
-
ReactDOM.render(contactForm.parseSchema(schema),
106
-
document.getElementById('contact-form'));
118
+
ReactDOM.render(view.parseSchema(schema),
119
+
document.getElementById('content'));
107
120
```
108
121
109
122
### Try the Demo
@@ -122,4 +135,4 @@ Please use a linter that recognizes eslint rules
it('should consume a key defined in the schema\'s keys for the current sub-schema based on the current sub-schema\'s index to meet React\'s key expectation of multiple React elements.',()=>{
it('should assign a key to the current sub-schema based on the current sub-schema\'s index to meet React\'s key expectation of multiple React elements.',()=>{
0 commit comments