ra-core
Version:
Core components of react-admin, a frontend Framework for building admin applications on top of REST services, using ES6, React
57 lines (49 loc) • 1.49 kB
text/typescript
import { createElement } from 'react';
import { InferredType } from './types';
class InferredElement {
type?: InferredType;
props?: any;
children?: any;
constructor(type?: InferredType, props?: any, children?: any) {
this.type = type;
this.props = props;
this.children = children;
}
getElement(props = {}) {
if (!this.type || !this.type.component) {
return;
}
return this.children
? createElement(
this.type.component,
{ ...this.props, ...props },
this.children.length > 0
? this.children.map((child, index) =>
child.getElement({ key: index })
)
: this.children.getElement()
)
: createElement(this.type.component, { ...this.props, ...props });
}
getProps() {
return this.props;
}
isDefined() {
return !!this.type;
}
getRepresentation() {
if (!this.type) {
return '';
}
if (this.type.representation) {
return this.type.representation(this.props, this.children);
}
if (this.type.component) {
return `<${
this.type.component.displayName || this.type.component.name
} source="${this.props.source}" />`;
}
return '';
}
}
export default InferredElement;