Skip to content

Moving _componentMap property inside the ReactJsonSchema class #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions dist/react-json-schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in ob

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

var _componentMap = null;
var _componentMap = new WeakMap();

var ReactJsonSchema = function () {
function ReactJsonSchema() {
Expand All @@ -34,7 +34,7 @@ var ReactJsonSchema = function () {
}, {
key: 'parseSubSchemas',
value: function parseSubSchemas() {
var subSchemas = arguments.length <= 0 || arguments[0] === undefined ? [] : arguments[0];
var subSchemas = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like this is compiling different? I assume this is ok and its just the result of the compiler version...

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, compiler probably updated to && for better performance (doesn't perform right hand evaluation if left hand fails, unlike ||).


var Components = [];
var index = 0;
Expand Down Expand Up @@ -70,11 +70,10 @@ var ReactJsonSchema = function () {
}, {
key: 'createComponent',
value: function createComponent(schema) {
var component = schema.component;
var children = schema.children;
var text = schema.text;

var rest = _objectWithoutProperties(schema, ['component', 'children', 'text']);
var component = schema.component,
children = schema.children,
text = schema.text,
rest = _objectWithoutProperties(schema, ['component', 'children', 'text']);

var Component = this.resolveComponent(schema);
var Children = typeof text !== 'undefined' ? text : this.resolveComponentChildren(schema);
Expand All @@ -83,6 +82,7 @@ var ReactJsonSchema = function () {
}, {
key: 'resolveComponent',
value: function resolveComponent(schema) {
var _componentMap = this.getComponentMap();
var Component = null;
if (schema.hasOwnProperty('component')) {
if (schema.component === Object(schema.component)) {
Expand All @@ -105,12 +105,12 @@ var ReactJsonSchema = function () {
}, {
key: 'getComponentMap',
value: function getComponentMap() {
return _componentMap;
return _componentMap.get(this);
}
}, {
key: 'setComponentMap',
value: function setComponentMap(componentMap) {
_componentMap = componentMap;
_componentMap.set(this, componentMap);
}
}]);

Expand Down
2 changes: 1 addition & 1 deletion dist/react-json-schema.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 7 additions & 6 deletions lib/ReactJsonSchema.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { DOM, createElement } from 'react';

let _componentMap = null;
const _componentMap = new WeakMap();

export default class ReactJsonSchema {

Expand All @@ -27,19 +27,20 @@ export default class ReactJsonSchema {
}

createComponent(schema) {
const { component, children, text, ...rest} = schema;
const { component, children, text, ...rest } = schema;
const Component = this.resolveComponent(schema);
const Children = typeof text !== 'undefined' ? text : this.resolveComponentChildren(schema);
return createElement(Component, rest, Children);
}

resolveComponent(schema) {
const componentMap = this.getComponentMap();
let Component = null;
if (schema.hasOwnProperty('component')) {
if (schema.component === Object(schema.component)) {
Component = schema.component;
} else if (_componentMap && _componentMap[schema.component]) {
Component = _componentMap[schema.component];
} else if (componentMap && componentMap[schema.component]) {
Component = componentMap[schema.component];
} else if (DOM.hasOwnProperty(schema.component)) {
Component = schema.component;
}
Expand All @@ -55,10 +56,10 @@ export default class ReactJsonSchema {
}

getComponentMap() {
return _componentMap;
return _componentMap.get(this);
}

setComponentMap(componentMap) {
_componentMap = componentMap;
_componentMap.set(this, componentMap);
}
}
12 changes: 12 additions & 0 deletions spec/ReactJsonSchemaSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,4 +131,16 @@ export default describe('ReactJsonSchema', () => {
expect(!!actual.length).toBe(true);
});
});
describe('when multiple instances of ReactJsonSchema are created with different componentMaps', () => {
it('getComponentMap() should return the appropriate value for each instance', () => {
const reactJsonSchema1 = new ReactJsonSchema();
const componentMap1 = { component1: Tester };
reactJsonSchema1.setComponentMap(componentMap1);
const reactJsonSchema2 = new ReactJsonSchema();
const componentMap2 = { component2: Tester };
reactJsonSchema2.setComponentMap(componentMap2);
expect(reactJsonSchema1.getComponentMap()).toEqual(componentMap1);
expect(reactJsonSchema2.getComponentMap()).toEqual(componentMap2);
});
});
});