attributes-kit
Version:
React component for MSON rendering
70 lines (54 loc) • 1.6 kB
JavaScript
import React from 'react';
import ReactDom from 'react-dom';
import { EventEmitter } from 'events';
import eidolon from 'eidolon';
import AttributesComponent from './Components/Attributes/Attributes';
import AttributeComponent from './Components/Attribute/Attribute';
class AttributesKit extends EventEmitter {
static Attributes = AttributesComponent;
static Attribute = AttributeComponent;
static generateExample = eidolon.example;
static generateJsonSchema = eidolon.schema;
static render(refractElement, element, options) {
let elementSelector = null;
if (typeof element === 'string') {
elementSelector = element;
}
const attributesKit = new AttributesKit({
element,
elementSelector,
options,
});
attributesKit.render(refractElement);
return attributesKit;
}
constructor(options) {
super();
this.options = options || {};
this.element = options.element;
this.elementSelector = options.elementSelector;
this.options = options.options;
this.getElement();
this.validate();
}
validate() {
if (!this.element) {
throw Error('Unable to find element where to render attributes.');
}
}
getElement() {
if (this.element) {
return this.element;
}
this.element = document.querySelector(this.elementSelector);
return this.element;
}
render(refractElement) {
ReactDom.render(
<AttributesComponent element={refractElement} dataStructures={this.options.dataStructures} />,
this.element
);
}
}
// Exports
export default AttributesKit;