Skip to content

Commit 50c0b78

Browse files
committed
Remove lodash dependency. Verify compatibility with React 15.3.0.
1 parent e7494ce commit 50c0b78

12 files changed

Lines changed: 122 additions & 73 deletions

.babelrc

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
{
2-
"presets": [
3-
"es2015"
4-
]
2+
"presets": [
3+
"es2015"
4+
],
5+
"plugins": [
6+
"transform-es2015-destructuring",
7+
"transform-object-rest-spread"
8+
]
59
}

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ To run the demo
117117

118118
Please use a linter that recognizes eslint rules
119119
* `npm install`
120-
* `npm test` (Jasmine's test report will output in /spec/index.html)
120+
* `npm test`
121121
* `npm run build`
122122

123123
### Roadmap

demo/components/CheckboxField.jsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React from 'react';
2-
import { Input } from 'react-bootstrap';
2+
import { FormGroup,Radio } from 'react-bootstrap';
33

44
class CheckboxField extends React.Component {
55

@@ -11,7 +11,7 @@ class CheckboxField extends React.Component {
1111
const checkboxes = [];
1212
this.props.checkboxes.forEach(function loop(checkbox, index) {
1313
checkboxes.push(
14-
<Input key={index} type="checkbox" label={checkbox.label} {...checkbox} />
14+
<Radio key={index} {...checkbox}>{checkbox.label}</Radio>
1515
);
1616
});
1717
return checkboxes;
@@ -20,9 +20,9 @@ class CheckboxField extends React.Component {
2020
render() {
2121
const checkboxes = this.renderCheckboxes();
2222
return (
23-
<div className="checkboxes">
23+
<FormGroup className="checkboxes">
2424
{checkboxes}
25-
</div>
25+
</FormGroup>
2626
);
2727
}
2828
}

demo/components/StringField.jsx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React from 'react';
2-
import { Input } from 'react-bootstrap';
2+
import { FormGroup, ControlLabel, FormControl, HelpBlock } from 'react-bootstrap';
33

44
const alphaRegex = /^$|[A-Z]+$/i;
55

@@ -17,14 +17,20 @@ class StringField extends React.Component {
1717
}
1818

1919
render() {
20+
const { label, name, help, ...rest } = this.props;
2021
return (
21-
<Input type="text" onChange={this.validateInput.bind(this)} label={this.props.label} help={this.props.help} />
22+
<FormGroup controlId={name}>
23+
<ControlLabel>{label}</ControlLabel>
24+
<FormControl componentClass="input" name={name} />
25+
{help && <HelpBlock>{help}</HelpBlock>}
26+
</FormGroup>
2227
);
2328
}
2429
}
2530

2631
StringField.propTypes = {
2732
label: React.PropTypes.string.isRequired,
33+
name: React.PropTypes.string.isRequired,
2834
help: React.PropTypes.string
2935
};
3036

demo/index.jsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ const formSchema = {
2222
{
2323
'component': 'StringField',
2424
'label': 'What\'s your name',
25+
'name': 'fullname',
2526
'help': 'It\'s okay, don\'t be shy :)'
2627
},
2728
{

dist/react-json-schema.js

Lines changed: 47 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
'use strict';
22

3-
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; }; }();
4-
53
Object.defineProperty(exports, "__esModule", {
64
value: true
75
});
86

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+
99
var _react = require('react');
1010

1111
var _ReactDOMFactories = require('react/lib/ReactDOMFactories');
1212

1313
var _ReactDOMFactories2 = _interopRequireDefault(_ReactDOMFactories);
1414

15-
var _lodash = require('lodash');
16-
1715
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
1816

17+
function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; }
18+
1919
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
2020

2121
var _componentMap = null;
@@ -30,7 +30,7 @@ var ReactJsonSchema = function () {
3030
value: function parseSchema(schema) {
3131
var element = null;
3232
var elements = null;
33-
if ((0, _lodash.isArray)(schema)) {
33+
if (Array.isArray(schema)) {
3434
elements = this.parseSubSchemas(schema);
3535
} else {
3636
element = this.createComponent(schema);
@@ -39,35 +39,63 @@ var ReactJsonSchema = function () {
3939
}
4040
}, {
4141
key: 'parseSubSchemas',
42-
value: function parseSubSchemas(subSchemas) {
43-
var _this = this;
42+
value: function parseSubSchemas() {
43+
var subSchemas = arguments.length <= 0 || arguments[0] === undefined ? [] : arguments[0];
4444

4545
var Components = [];
46-
(0, _lodash.forEach)(subSchemas, function (subSchema, index) {
47-
subSchema.key = index;
48-
Components.push(_this.parseSchema(subSchema));
49-
});
46+
var index = 0;
47+
var _iteratorNormalCompletion = true;
48+
var _didIteratorError = false;
49+
var _iteratorError = undefined;
50+
51+
try {
52+
for (var _iterator = subSchemas[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
53+
var subSchema = _step.value;
54+
55+
subSchema.key = index;
56+
Components.push(this.parseSchema(subSchema));
57+
index++;
58+
}
59+
} catch (err) {
60+
_didIteratorError = true;
61+
_iteratorError = err;
62+
} finally {
63+
try {
64+
if (!_iteratorNormalCompletion && _iterator.return) {
65+
_iterator.return();
66+
}
67+
} finally {
68+
if (_didIteratorError) {
69+
throw _iteratorError;
70+
}
71+
}
72+
}
73+
5074
return Components;
5175
}
5276
}, {
5377
key: 'createComponent',
5478
value: function createComponent(schema) {
55-
var props = (0, _lodash.clone)(schema);
56-
props = (0, _lodash.omit)(props, ['component', 'children']);
79+
var component = schema.component;
80+
var children = schema.children;
81+
var text = schema.text;
82+
83+
var rest = _objectWithoutProperties(schema, ['component', 'children', 'text']);
84+
5785
var Component = this.resolveComponent(schema);
58-
var Children = props.text || this.resolveComponentChildren(schema);
59-
return (0, _react.createElement)(Component, props, Children);
86+
var Children = typeof text !== 'undefined' ? text : this.resolveComponentChildren(schema);
87+
return (0, _react.createElement)(Component, rest, Children);
6088
}
6189
}, {
6290
key: 'resolveComponent',
6391
value: function resolveComponent(schema) {
6492
var Component = null;
65-
if ((0, _lodash.has)(schema, 'component')) {
66-
if ((0, _lodash.isObject)(schema.component)) {
93+
if (schema.hasOwnProperty('component')) {
94+
if (schema.component === Object(schema.component)) {
6795
Component = schema.component;
6896
} else if (_componentMap && _componentMap[schema.component]) {
6997
Component = _componentMap[schema.component];
70-
} else if ((0, _lodash.has)(_ReactDOMFactories2.default, schema.component)) {
98+
} else if (_ReactDOMFactories2.default.hasOwnProperty(schema.component)) {
7199
Component = schema.component;
72100
}
73101
} else {
@@ -78,7 +106,7 @@ var ReactJsonSchema = function () {
78106
}, {
79107
key: 'resolveComponentChildren',
80108
value: function resolveComponentChildren(schema) {
81-
return (0, _lodash.has)(schema, 'children') ? this.parseSchema(schema.children) : [];
109+
return schema.hasOwnProperty('children') ? this.parseSchema(schema.children) : [];
82110
}
83111
}, {
84112
key: 'getComponentMap',

dist/react-json-schema.min.js

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

lib/ReactJsonSchema.js

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { createElement } from 'react';
22
import ReactDOMFactories from 'react/lib/ReactDOMFactories';
3-
import { isArray, forEach, omit, clone, has, isObject } from 'lodash';
43

54
let _componentMap = null;
65

@@ -9,39 +8,40 @@ export default class ReactJsonSchema {
98
parseSchema(schema) {
109
let element = null;
1110
let elements = null;
12-
if (isArray(schema)) {
11+
if (Array.isArray(schema)) {
1312
elements = this.parseSubSchemas(schema);
1413
} else {
1514
element = this.createComponent(schema);
1615
}
1716
return element || elements;
1817
}
1918

20-
parseSubSchemas(subSchemas) {
19+
parseSubSchemas(subSchemas = []) {
2120
const Components = [];
22-
forEach(subSchemas, (subSchema, index) => {
21+
let index = 0;
22+
for (const subSchema of subSchemas) {
2323
subSchema.key = index;
2424
Components.push(this.parseSchema(subSchema));
25-
});
25+
index++;
26+
}
2627
return Components;
2728
}
2829

2930
createComponent(schema) {
30-
let props = clone(schema);
31-
props = omit(props, ['component', 'children']);
31+
const { component, children, text, ...rest} = schema;
3232
const Component = this.resolveComponent(schema);
33-
const Children = props.text || this.resolveComponentChildren(schema);
34-
return createElement(Component, props, Children);
33+
const Children = typeof text !== 'undefined' ? text : this.resolveComponentChildren(schema);
34+
return createElement(Component, rest, Children);
3535
}
3636

3737
resolveComponent(schema) {
3838
let Component = null;
39-
if (has(schema, 'component')) {
40-
if (isObject(schema.component)) {
39+
if (schema.hasOwnProperty('component')) {
40+
if (schema.component === Object(schema.component)) {
4141
Component = schema.component;
4242
} else if (_componentMap && _componentMap[schema.component]) {
4343
Component = _componentMap[schema.component];
44-
} else if (has(ReactDOMFactories, schema.component)) {
44+
} else if (ReactDOMFactories.hasOwnProperty(schema.component)) {
4545
Component = schema.component;
4646
}
4747
} else {
@@ -51,7 +51,7 @@ export default class ReactJsonSchema {
5151
}
5252

5353
resolveComponentChildren(schema) {
54-
return (has(schema, 'children')) ?
54+
return (schema.hasOwnProperty('children')) ?
5555
this.parseSchema(schema.children) : [];
5656
}
5757

package.json

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,37 +23,46 @@
2323
"scripts": {
2424
"prebuild": "rm -rf dist && mkdir dist",
2525
"build": "babel lib/ReactJsonSchema.js -o dist/react-json-schema.js",
26-
"postbuild": "node_modules/uglify-js/bin/uglifyjs dist/react-json-schema.js -o dist/react-json-schema.min.js",
26+
"postbuild": "node_modules/uglify-js/bin/uglifyjs dist/react-json-schema.js -o dist/react-json-schema.min.js -m --stats",
2727
"demo": "webpack-dev-server --progress --colors --inline --config webpack.config.demo.js",
28-
"test": "webpack --progress --profile --colors --config webpack.config.spec.js"
28+
"jasmine": "jasmine spec/spec.js",
29+
"jasmine-strict": "jasmine spec/spec.js",
30+
"pretest": "webpack --progress --profile --colors --config webpack.config.spec.js",
31+
"test": "npm run jasmine",
32+
"preversion": "npm run jasmine-strict",
33+
"version": "npm run build && git add -A",
34+
"postversion": "git push origin master && git push origin --tags"
2935
},
3036
"main": "dist/react-json-schema.js",
3137
"files": [
3238
"lib",
3339
"dist"
3440
],
3541
"dependencies": {
36-
"lodash": "^3.10.1",
37-
"react": "^0.14.2"
42+
"react": "^15.3.0"
3843
},
3944
"devDependencies": {
4045
"babel-cli": "^6.4.0",
4146
"babel-core": "^6.4.0",
42-
"babel-eslint": "^4.1.3",
43-
"babel-loader": "6.1.0",
44-
"babel-preset-es2015": "6.1.18",
45-
"babel-preset-react": "6.1.18",
47+
"babel-eslint": "^6.1.2",
48+
"babel-loader": "6.2.4",
49+
"babel-plugin-transform-es2015-destructuring": "^6.9.0",
50+
"babel-plugin-transform-object-rest-spread": "^6.8.0",
51+
"babel-preset-es2015": "6.9.0",
52+
"babel-preset-react": "6.11.1",
4653
"bootstrap": "^3.3.5",
47-
"eslint": "^1.5.1",
48-
"eslint-config-airbnb": "^0.1.0",
49-
"eslint-plugin-react": "^3.5.0",
54+
"eslint": "^2.9.0",
55+
"eslint-config-airbnb": "^9.0.1",
56+
"eslint-plugin-import": "^1.7.0",
57+
"eslint-plugin-jsx-a11y": "^1.2.0",
58+
"eslint-plugin-react": "^5.2.2",
5059
"file-loader": "^0.8.4",
5160
"jasmine": "^2.3.2",
5261
"jsx-loader": "^0.13.2",
5362
"path": "^0.12.7",
54-
"react-bootstrap": "^0.27.3",
55-
"react-dom": "^0.14.2",
56-
"uglify-js": "^2.4.24",
63+
"react-bootstrap": "^0.30.0",
64+
"react-dom": "^15.3.0",
65+
"uglify-js": "^2.7.0",
5766
"webpack": "^1.12.2",
5867
"webpack-dev-server": "^1.12.0"
5968
}

0 commit comments

Comments
 (0)