Skip to content

Commit 2940df5

Browse files
committed
Moving _componentMap property inside the ReactJsonSchema class so that its not shared between instances.
1 parent ecc0607 commit 2940df5

File tree

4 files changed

+30
-15
lines changed

4 files changed

+30
-15
lines changed

dist/react-json-schema.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in ob
1212

1313
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
1414

15-
var _componentMap = null;
16-
1715
var ReactJsonSchema = function () {
1816
function ReactJsonSchema() {
1917
_classCallCheck(this, ReactJsonSchema);
18+
19+
this._componentMap = null;
2020
}
2121

2222
_createClass(ReactJsonSchema, [{
@@ -34,7 +34,7 @@ var ReactJsonSchema = function () {
3434
}, {
3535
key: 'parseSubSchemas',
3636
value: function parseSubSchemas() {
37-
var subSchemas = arguments.length <= 0 || arguments[0] === undefined ? [] : arguments[0];
37+
var subSchemas = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
3838

3939
var Components = [];
4040
var index = 0;
@@ -70,11 +70,10 @@ var ReactJsonSchema = function () {
7070
}, {
7171
key: 'createComponent',
7272
value: function createComponent(schema) {
73-
var component = schema.component;
74-
var children = schema.children;
75-
var text = schema.text;
76-
77-
var rest = _objectWithoutProperties(schema, ['component', 'children', 'text']);
73+
var component = schema.component,
74+
children = schema.children,
75+
text = schema.text,
76+
rest = _objectWithoutProperties(schema, ['component', 'children', 'text']);
7877

7978
var Component = this.resolveComponent(schema);
8079
var Children = typeof text !== 'undefined' ? text : this.resolveComponentChildren(schema);
@@ -83,6 +82,7 @@ var ReactJsonSchema = function () {
8382
}, {
8483
key: 'resolveComponent',
8584
value: function resolveComponent(schema) {
85+
var _componentMap = this._componentMap;
8686
var Component = null;
8787
if (schema.hasOwnProperty('component')) {
8888
if (schema.component === Object(schema.component)) {
@@ -105,12 +105,12 @@ var ReactJsonSchema = function () {
105105
}, {
106106
key: 'getComponentMap',
107107
value: function getComponentMap() {
108-
return _componentMap;
108+
return this._componentMap;
109109
}
110110
}, {
111111
key: 'setComponentMap',
112112
value: function setComponentMap(componentMap) {
113-
_componentMap = componentMap;
113+
this._componentMap = componentMap;
114114
}
115115
}]);
116116

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: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import { DOM, createElement } from 'react';
22

3-
let _componentMap = null;
4-
53
export default class ReactJsonSchema {
64

5+
constructor(){
6+
this._componentMap = null;
7+
}
8+
79
parseSchema(schema) {
810
let element = null;
911
let elements = null;
@@ -34,6 +36,7 @@ export default class ReactJsonSchema {
3436
}
3537

3638
resolveComponent(schema) {
39+
const _componentMap = this._componentMap;
3740
let Component = null;
3841
if (schema.hasOwnProperty('component')) {
3942
if (schema.component === Object(schema.component)) {
@@ -55,10 +58,10 @@ export default class ReactJsonSchema {
5558
}
5659

5760
getComponentMap() {
58-
return _componentMap;
61+
return this._componentMap;
5962
}
6063

6164
setComponentMap(componentMap) {
62-
_componentMap = componentMap;
65+
this._componentMap = componentMap;
6366
}
6467
}

spec/ReactJsonSchemaSpec.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,4 +131,16 @@ export default describe('ReactJsonSchema', () => {
131131
expect(!!actual.length).toBe(true);
132132
});
133133
});
134+
describe('when multiple instances of ReactJsonSchema are created with different componentMaps', () => {
135+
it('getComponentMap() should return the appropriate value for each instance', () => {
136+
const reactJsonSchema1 = new ReactJsonSchema();
137+
const componentMap1 = { component1: Tester };
138+
reactJsonSchema1.setComponentMap(componentMap1);
139+
const reactJsonSchema2 = new ReactJsonSchema();
140+
const componentMap2 = { component2: Tester };
141+
reactJsonSchema2.setComponentMap(componentMap2);
142+
expect(reactJsonSchema1.getComponentMap()).toEqual(componentMap1);
143+
expect(reactJsonSchema2.getComponentMap()).toEqual(componentMap2);
144+
});
145+
});
134146
});

0 commit comments

Comments
 (0)