-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathReactJsonSchema.js
68 lines (60 loc) · 2.11 KB
/
ReactJsonSchema.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import { createElement } from 'react';
import DOM from 'react-dom-factories';
const componentMapCollection = new WeakMap();
export default class ReactJsonSchema {
parseSchema(schema) {
let element = null;
let elements = null;
if (Array.isArray(schema)) {
elements = this.parseSubSchemas(schema);
} else if (schema) {
element = this.createComponent(schema);
}
return element || elements;
}
parseSubSchemas(subSchemas = []) {
const Components = [];
let index = 0;
Object.keys(subSchemas).forEach((key) => {
const subSchema = subSchemas[key];
subSchema.key = typeof subSchema.key !== 'undefined' ? subSchema.key : index;
Components.push(this.parseSchema(subSchema));
index += 1;
});
return Components;
}
createComponent(schema) {
// eslint-disable-next-line no-unused-vars
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 (Object.prototype.hasOwnProperty.call(schema, 'component')) {
if (schema.component === Object(schema.component)) {
Component = schema.component;
} else if (componentMap && componentMap[schema.component]) {
Component = componentMap[schema.component];
} else if (Object.prototype.hasOwnProperty.call(DOM, schema.component)) {
Component = schema.component;
}
} else {
throw new Error(`ReactJsonSchema could not resolve a component due to a missing component
attribute in the schema.`);
}
return Component;
}
resolveComponentChildren(schema) {
return (Object.prototype.hasOwnProperty.call(schema, 'children')) ?
this.parseSchema(schema.children) : undefined;
}
getComponentMap() {
return componentMapCollection.get(this);
}
setComponentMap(componentMap) {
componentMapCollection.set(this, componentMap);
}
}