|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +Object.defineProperty(exports, '__esModule', { |
| 4 | + value: true |
| 5 | +}); |
| 6 | + |
| 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 | + |
| 9 | +function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; } |
| 10 | + |
| 11 | +function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } } |
| 12 | + |
| 13 | +var _react = require('react'); |
| 14 | + |
| 15 | +var _react2 = _interopRequireDefault(_react); |
| 16 | + |
| 17 | +var _lodash = require('lodash'); |
| 18 | + |
| 19 | +var _lodash2 = _interopRequireDefault(_lodash); |
| 20 | + |
| 21 | +var _componentMap = null; |
| 22 | + |
| 23 | +var ReactJsonSchema = (function () { |
| 24 | + function ReactJsonSchema() { |
| 25 | + _classCallCheck(this, ReactJsonSchema); |
| 26 | + } |
| 27 | + |
| 28 | + _createClass(ReactJsonSchema, [{ |
| 29 | + key: 'parseSchema', |
| 30 | + value: function parseSchema(schema) { |
| 31 | + var element = null; |
| 32 | + var elements = null; |
| 33 | + if (_lodash2['default'].isArray(schema)) { |
| 34 | + elements = this.parseSubSchemas(schema); |
| 35 | + } else { |
| 36 | + element = this.createComponent(schema); |
| 37 | + } |
| 38 | + return element || elements; |
| 39 | + } |
| 40 | + }, { |
| 41 | + key: 'parseSubSchemas', |
| 42 | + value: function parseSubSchemas(subSchemas) { |
| 43 | + var _this = this; |
| 44 | + |
| 45 | + var Components = []; |
| 46 | + _lodash2['default'].forEach(subSchemas, function (subSchema, index) { |
| 47 | + subSchema.key = index; |
| 48 | + Components.push(_this.parseSchema(subSchema)); |
| 49 | + }); |
| 50 | + return Components; |
| 51 | + } |
| 52 | + }, { |
| 53 | + key: 'createComponent', |
| 54 | + value: function createComponent(schema) { |
| 55 | + var props = _lodash2['default'].clone(schema); |
| 56 | + props = _lodash2['default'].omit(props, ['component', 'children']); |
| 57 | + var Component = this.resolveComponent(schema); |
| 58 | + var Children = this.resolveComponentChildren(schema); |
| 59 | + return _react2['default'].createElement(Component, props, Children); |
| 60 | + } |
| 61 | + }, { |
| 62 | + key: 'resolveComponent', |
| 63 | + value: function resolveComponent(schema) { |
| 64 | + var Component = null; |
| 65 | + if (_lodash2['default'].has(schema, 'component')) { |
| 66 | + if (_lodash2['default'].isObject(schema.component)) { |
| 67 | + Component = schema.component; |
| 68 | + } else if (_lodash2['default'].isString(schema.component)) { |
| 69 | + Component = _componentMap[schema.component]; |
| 70 | + } |
| 71 | + } else { |
| 72 | + throw new Error('ReactJsonSchema could not resolve a component due to a missing component attribute in the schema.'); |
| 73 | + } |
| 74 | + return Component; |
| 75 | + } |
| 76 | + }, { |
| 77 | + key: 'resolveComponentChildren', |
| 78 | + value: function resolveComponentChildren(schema) { |
| 79 | + return _lodash2['default'].has(schema, 'children') ? this.parseSchema(schema.children) : []; |
| 80 | + } |
| 81 | + }, { |
| 82 | + key: 'getComponentMap', |
| 83 | + value: function getComponentMap() { |
| 84 | + return _componentMap; |
| 85 | + } |
| 86 | + }, { |
| 87 | + key: 'setComponentMap', |
| 88 | + value: function setComponentMap(componentMap) { |
| 89 | + _componentMap = componentMap; |
| 90 | + } |
| 91 | + }]); |
| 92 | + |
| 93 | + return ReactJsonSchema; |
| 94 | +})(); |
| 95 | + |
| 96 | +exports['default'] = ReactJsonSchema; |
| 97 | +module.exports = exports['default']; |
0 commit comments