-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from TechniqueSoftware/feature/html-tags
HTML Tags
- Loading branch information
Showing
10 changed files
with
146 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
spec/spec.js | ||
*.md |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,59 +1,122 @@ | ||
# react-json-schema | ||
|
||
This library builds React elements from JSON by mapping JSON definitions to React components that you expose. The interest behind making this library is to allow non-programmers whip up a view using JSON, which can be stored and retrieved in a database. Use it as you'd like. (JSX not required) | ||
This library builds React elements from JSON by mapping JSON definitions to React components that you expose. The interest behind making this library is to allow non-programmers to construct a view using JSON, which can be stored and retrieved in a database. Use it as you'd like. | ||
|
||
### Documentation | ||
JSX is not a dependency for react-json-schema. | ||
|
||
For a quick reference, you can jump to [full example](#putting-it-all-together). | ||
|
||
The first thing you'll need to do is define your schema in JSON (or a JavaScript object literal). When doing so, there are two things to note: | ||
- A **component** key _must_ exist and be defined by a string or React Component | ||
- A **children** key _may_ exist to define sub-components | ||
### Documentation | ||
|
||
Next, we have to make sure that react-json-schema can create elements from component definitions. If a schema's **component** is defined by an string, you will need to expose the components included in the schema to react-json-schema. This can be done by calling `setComponentMap` with an object that has keys that match the strings in your schema, to the components are to be resolved by these strings. | ||
#### Schema | ||
|
||
Finally, you'll need to call `parseSchema` to create elements from your schema. Now you have React elements at your disposal! | ||
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: | ||
- **component**: _MUST_ exist and be defined by a string or React component (must be a string if describing a native HTML tag) | ||
- **children**: _MAY_ exist to define sub-components | ||
- **text**: _MAY_ exist to as a string to define inner HTML text (overrides children) | ||
|
||
Example (taken from /demo/index.jsx) | ||
Example JSON schema (ES6) | ||
```js | ||
const schema = { | ||
"component": "ContactForm", | ||
"title": "Tell us a little about yourself...", | ||
"children": [ | ||
{ | ||
"component": "StringField", | ||
"label": "What's your name", | ||
"help": "It's okay, don't be shy :)" | ||
"label": "What's your name?" | ||
}, | ||
{ | ||
"component": "a", | ||
"href": "#faq", | ||
"text": "I'm not sure why I'm filling this out" | ||
} | ||
] | ||
} | ||
``` | ||
|
||
Example JS literal (ES6) | ||
```js | ||
const schema = { | ||
"component": ContactForm, | ||
"title": "Tell us a little about yourself...", | ||
"children": [ | ||
{ | ||
"component": StringField, | ||
"label": "What's your name?" | ||
}, | ||
{ | ||
"component": "a", | ||
"href": "#faq", | ||
"text": "I'm not sure why I'm filling this out" | ||
} | ||
] | ||
} | ||
``` | ||
|
||
##### Dynamic Children and Keys | ||
|
||
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. | ||
|
||
/* es6 object literal shorthand */ | ||
#### Component Mapping | ||
|
||
React components need to be exposed to the react-json-schema so that the parser can create React elements. If the schema contains object literals with component references, the schema is exposing the React components and no additional configuration is needed. If the schema does not contain references to components, the components can be exposed via `setComponentMap`. | ||
|
||
Example for exposing non-exposed components (ES6) | ||
```js | ||
/* es6 object literal shorthand: { ContactForm } == { ContactForm: ContactForm } */ | ||
contactForm.setComponentMap({ ContactForm, StringField }); | ||
``` | ||
|
||
#### Parsing | ||
|
||
Use `parseSchema` to render React elements. It returns the root node. Note that if your schema's root is an array, you'll have to wrap the schema in an element. | ||
|
||
Example (ES6) | ||
```js | ||
/* If using ReactDOM (0.14+), else use React */ | ||
ReactDOM.render(contactForm.parseSchema(schema), | ||
document.getElementById('contact-form')); | ||
``` | ||
|
||
##### Rendering | ||
|
||
Also note react-json-schema also 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. | ||
|
||
#### Putting it All Together | ||
|
||
```js | ||
import ReactDOM from 'react-dom'; | ||
import ReactJsonSchema from 'react-json-schema'; | ||
|
||
import FormStore from './stores/FormStore'; | ||
import ContactForm from './components/ContactForm'; | ||
import StringField from './components/StringField'; | ||
|
||
// For this example, let's pretend I already have data and am ignorant of actions | ||
const schema = FormStore.getFormSchema(); | ||
const componentMap = { ContactForm, StringField } | ||
|
||
const contactForm = new ReactJsonSchema(); | ||
contactForm.setComponentMap(componentMap); | ||
|
||
React.render(contactForm.parseSchema(schema), | ||
document.getElementById('json-react-schema')); | ||
ReactDOM.render(contactForm.parseSchema(schema), | ||
document.getElementById('contact-form')); | ||
``` | ||
|
||
### Try the Demo | ||
|
||
To run the demo | ||
* have webpack and webpack-dev-server globally installed | ||
* `npm install` | ||
* `npm run demo` | ||
* The app will be served at http://localhost:8080 | ||
|
||
### Contribution and Code of Conduct | ||
|
||
Please use a linter that recognizes eslint rules | ||
|
||
* have webpack, webpack-dev-server and jasmine globally installed | ||
* `npm install` | ||
* `npm test` (Jasmine's test report will output in /spec/index.html) | ||
* `npm run build` | ||
|
||
### Roadmap | ||
|
||
* Support custom keys for children | ||
* Support native html tags as components, with the option to add custom tag definitions | ||
* Drop lodash dependency? |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.