Skip to content

Commit 5296285

Browse files
author
Nick Laffey
authored
Merge pull request #12 from TechniqueSoftware/allow_multiple_instances_with_different_component_maps
Moving _componentMap property inside the ReactJsonSchema class
2 parents ecc0607 + 3d56c45 commit 5296285

File tree

4 files changed

+29
-16
lines changed

4 files changed

+29
-16
lines changed

dist/react-json-schema.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ 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;
15+
var _componentMap = new WeakMap();
1616

1717
var ReactJsonSchema = function () {
1818
function 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.getComponentMap();
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 _componentMap.get(this);
109109
}
110110
}, {
111111
key: 'setComponentMap',
112112
value: function setComponentMap(componentMap) {
113-
_componentMap = componentMap;
113+
_componentMap.set(this, 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 & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { DOM, createElement } from 'react';
22

3-
let _componentMap = null;
3+
const _componentMap = new WeakMap();
44

55
export default class ReactJsonSchema {
66

@@ -27,19 +27,20 @@ export default class ReactJsonSchema {
2727
}
2828

2929
createComponent(schema) {
30-
const { component, children, text, ...rest} = schema;
30+
const { component, children, text, ...rest } = schema;
3131
const Component = this.resolveComponent(schema);
3232
const Children = typeof text !== 'undefined' ? text : this.resolveComponentChildren(schema);
3333
return createElement(Component, rest, Children);
3434
}
3535

3636
resolveComponent(schema) {
37+
const componentMap = this.getComponentMap();
3738
let Component = null;
3839
if (schema.hasOwnProperty('component')) {
3940
if (schema.component === Object(schema.component)) {
4041
Component = schema.component;
41-
} else if (_componentMap && _componentMap[schema.component]) {
42-
Component = _componentMap[schema.component];
42+
} else if (componentMap && componentMap[schema.component]) {
43+
Component = componentMap[schema.component];
4344
} else if (DOM.hasOwnProperty(schema.component)) {
4445
Component = schema.component;
4546
}
@@ -55,10 +56,10 @@ export default class ReactJsonSchema {
5556
}
5657

5758
getComponentMap() {
58-
return _componentMap;
59+
return _componentMap.get(this);
5960
}
6061

6162
setComponentMap(componentMap) {
62-
_componentMap = componentMap;
63+
_componentMap.set(this, componentMap);
6364
}
6465
}

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)