Skip to content

Commit d0ac991

Browse files
committed
More for the demo, build process, readme updates
1 parent b19a237 commit d0ac991

File tree

8 files changed

+188
-22
lines changed

8 files changed

+188
-22
lines changed

README.md

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,23 +35,25 @@ React.render(contactForm.parseSchema(schema),
3535
document.getElementById('json-react-schema'));
3636
```
3737

38-
### Contribution and Code of Conduct
39-
40-
Please use a linter that recognizes eslint rules
38+
### Try the Demo
4139

4240
To run the demo
4341
* have webpack and webpack-dev-server globally installed
4442
* `npm install`
45-
* `npm start`
43+
* `npm run demo`
4644
* The app will be served at http://localhost:8080
4745

48-
To run tests
46+
### Contribution and Code of Conduct
47+
48+
Please use a linter that recognizes eslint rules
49+
4950
* have webpack, webpack-dev-server and jasmine globally installed
5051
* `npm install`
51-
* `npm test`
52-
* Jasmine's test report will output in /spec/index.html
52+
* `npm test` (Jasmine's test report will output in /spec/index.html)
53+
* `npm run build`
5354

5455
### Roadmap
5556

5657
* Support custom keys for children
5758
* Support native html tags as components, with the option to add custom tag definitions
59+
* Drop lodash dependency?

demo/components/CheckboxField.jsx

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import React from 'react';
2+
import { Input } from 'react-bootstrap';
3+
4+
class CheckboxField extends React.Component {
5+
6+
constructor(props) {
7+
super(props);
8+
}
9+
10+
renderCheckboxes() {
11+
const checkboxes = [];
12+
this.props.checkboxes.forEach(function loop(checkbox, index) {
13+
checkboxes.push(
14+
<Input key={index} type="checkbox" label={checkbox.label} {...checkbox} />
15+
);
16+
});
17+
return checkboxes;
18+
}
19+
20+
render() {
21+
const checkboxes = this.renderCheckboxes();
22+
return (
23+
<div className="checkboxes">
24+
{checkboxes}
25+
</div>
26+
);
27+
}
28+
}
29+
30+
CheckboxField.propTypes = {
31+
checkboxes: React.PropTypes.arrayOf(
32+
React.PropTypes.shape({
33+
label: React.PropTypes.string.isRequired
34+
}).isRequired
35+
).isRequired
36+
};
37+
38+
export default CheckboxField;

demo/index.jsx

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,36 @@
11
import React from 'react';
2-
import ReactJsonSchema from '../lib/ReactJsonSchema';
32
import ContactForm from './components/ContactForm';
43
import StringField from './components/StringField';
4+
import CheckboxField from './components/CheckboxField';
5+
6+
// If a package dependency: import ReactJsonSchema from 'react-json-schema';
7+
import ReactJsonSchema from '../dist/react-json-schema';
58

69
const schema = {
710
'component': 'ContactForm',
8-
'title': 'Tell us a little about yourself...',
11+
'title': 'Tell us a little about yourself, we\'d appreciate it',
912
'children': [
1013
{
1114
'component': 'StringField',
1215
'label': 'What\'s your name',
1316
'help': 'It\'s okay, don\'t be shy :)'
17+
},
18+
{
19+
'component': 'CheckboxField',
20+
'checkboxes': [
21+
{
22+
'label': 'I\'m already checked!',
23+
'defaultChecked': true
24+
},
25+
{
26+
'label': 'Here\'s another'
27+
}
28+
]
1429
}
1530
]
1631
};
1732

18-
const componentMap = { ContactForm, StringField };
33+
const componentMap = { ContactForm, StringField, CheckboxField };
1934
const contactForm = new ReactJsonSchema();
2035
contactForm.setComponentMap(componentMap);
2136

dist/react-json-schema.js

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
'use strict';
2+
3+
Object.defineProperty(exports, '__esModule', {
4+
value: true
5+
});
6+
7+
var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ('value' in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })();
8+
9+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
10+
11+
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } }
12+
13+
var _react = require('react');
14+
15+
var _react2 = _interopRequireDefault(_react);
16+
17+
var _lodash = require('lodash');
18+
19+
var _lodash2 = _interopRequireDefault(_lodash);
20+
21+
var _componentMap = null;
22+
23+
var ReactJsonSchema = (function () {
24+
function ReactJsonSchema() {
25+
_classCallCheck(this, ReactJsonSchema);
26+
}
27+
28+
_createClass(ReactJsonSchema, [{
29+
key: 'parseSchema',
30+
value: function parseSchema(schema) {
31+
var element = null;
32+
var elements = null;
33+
if (_lodash2['default'].isArray(schema)) {
34+
elements = this.parseSubSchemas(schema);
35+
} else {
36+
element = this.createComponent(schema);
37+
}
38+
return element || elements;
39+
}
40+
}, {
41+
key: 'parseSubSchemas',
42+
value: function parseSubSchemas(subSchemas) {
43+
var _this = this;
44+
45+
var Components = [];
46+
_lodash2['default'].forEach(subSchemas, function (subSchema, index) {
47+
subSchema.key = index;
48+
Components.push(_this.parseSchema(subSchema));
49+
});
50+
return Components;
51+
}
52+
}, {
53+
key: 'createComponent',
54+
value: function createComponent(schema) {
55+
var props = _lodash2['default'].clone(schema);
56+
props = _lodash2['default'].omit(props, ['component', 'children']);
57+
var Component = this.resolveComponent(schema);
58+
var Children = this.resolveComponentChildren(schema);
59+
return _react2['default'].createElement(Component, props, Children);
60+
}
61+
}, {
62+
key: 'resolveComponent',
63+
value: function resolveComponent(schema) {
64+
var Component = null;
65+
if (_lodash2['default'].has(schema, 'component')) {
66+
if (_lodash2['default'].isObject(schema.component)) {
67+
Component = schema.component;
68+
} else if (_lodash2['default'].isString(schema.component)) {
69+
Component = _componentMap[schema.component];
70+
}
71+
} else {
72+
throw new Error('ReactJsonSchema could not resolve a component due to a missing component attribute in the schema.');
73+
}
74+
return Component;
75+
}
76+
}, {
77+
key: 'resolveComponentChildren',
78+
value: function resolveComponentChildren(schema) {
79+
return _lodash2['default'].has(schema, 'children') ? this.parseSchema(schema.children) : [];
80+
}
81+
}, {
82+
key: 'getComponentMap',
83+
value: function getComponentMap() {
84+
return _componentMap;
85+
}
86+
}, {
87+
key: 'setComponentMap',
88+
value: function setComponentMap(componentMap) {
89+
_componentMap = componentMap;
90+
}
91+
}]);
92+
93+
return ReactJsonSchema;
94+
})();
95+
96+
exports['default'] = ReactJsonSchema;
97+
module.exports = exports['default'];

dist/react-json-schema.min.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,33 @@
11
{
22
"name": "react-json-schema",
33
"version": "0.1.0",
4-
"description": "Write component schema in JSON to assemble a react node",
5-
"main": "index.js",
6-
"scripts": {
7-
"start": "webpack-dev-server --progress --colors --inline --config webpack.config.demo.js",
8-
"test": "webpack --progress --profile --colors --config webpack.config.spec.js"
9-
},
4+
"description": "Write component schema in JSON; parse to create react elements.",
105
"keywords": [
6+
"react",
117
"JSON",
12-
"React",
13-
"JSON",
14-
"Schema",
15-
"Components"
8+
"schema",
9+
"components"
1610
],
17-
"author": "elliottisonfire",
11+
"author": {
12+
"name": "elliottisonfire",
13+
"url": "http://elliottisonfire.com"
14+
},
1815
"license": "Apache-2.0",
16+
"bugs": {
17+
"url": "https://github.com/TechniqueSoftware/react-json-schema/issues"
18+
},
19+
"scripts": {
20+
"prebuild": "rm -rf dist && mkdir dist",
21+
"build": "babel lib/ReactJsonSchema.js -o dist/react-json-schema.js",
22+
"postbuild": "node_modules/uglify-js/bin/uglifyjs dist/react-json-schema.js -o dist/react-json-schema.min.js",
23+
"demo": "webpack-dev-server --progress --colors --inline --config webpack.config.demo.js",
24+
"test": "webpack --progress --profile --colors --config webpack.config.spec.js"
25+
},
26+
"main": "dist/react-json-schema.js",
27+
"files": [
28+
"lib",
29+
"dist"
30+
],
1931
"dependencies": {
2032
"lodash": "^3.10.1",
2133
"react": "^0.13.3"
@@ -32,6 +44,7 @@
3244
"jsx-loader": "^0.13.2",
3345
"path": "^0.12.7",
3446
"react-bootstrap": "^0.25.2",
47+
"uglify-js": "^2.4.24",
3548
"webpack": "^1.12.2",
3649
"webpack-dev-server": "^1.12.0"
3750
}
File renamed without changes.

spec/spec.entry.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export * from './react-json-schema.spec.js';
1+
export * from './ReactJsonSchemaSpec.js';

0 commit comments

Comments
 (0)