2019-10-19 17:15:53 +02:00
|
|
|
import * as React from "react";
|
|
|
|
|
import binder from "./binder";
|
2019-10-11 13:24:02 +02:00
|
|
|
|
|
|
|
|
type Props = {
|
2019-10-19 16:38:07 +02:00
|
|
|
name: string;
|
|
|
|
|
renderAll?: boolean;
|
|
|
|
|
props?: object;
|
2019-10-19 17:15:53 +02:00
|
|
|
children?: React.ReactNode;
|
2019-10-11 13:24:02 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* ExtensionPoint renders components which are bound to an extension point.
|
|
|
|
|
*/
|
|
|
|
|
class ExtensionPoint extends React.Component<Props> {
|
2019-10-19 16:38:07 +02:00
|
|
|
renderAll(name: string, props?: object) {
|
2019-10-11 13:24:02 +02:00
|
|
|
const extensions = binder.getExtensions(name, props);
|
|
|
|
|
return (
|
|
|
|
|
<>
|
|
|
|
|
{extensions.map((Component, index) => {
|
|
|
|
|
return <Component key={index} {...props} />;
|
|
|
|
|
})}
|
|
|
|
|
</>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-19 16:38:07 +02:00
|
|
|
renderSingle(name: string, props?: object) {
|
2019-10-11 13:24:02 +02:00
|
|
|
const Component = binder.getExtension(name, props);
|
|
|
|
|
if (!Component) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
return <Component {...props} />;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
renderDefault() {
|
|
|
|
|
const { children } = this.props;
|
|
|
|
|
if (children) {
|
|
|
|
|
return <>{children}</>;
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
render() {
|
|
|
|
|
const { name, renderAll, props } = this.props;
|
|
|
|
|
if (!binder.hasExtension(name, props)) {
|
|
|
|
|
return this.renderDefault();
|
|
|
|
|
} else if (renderAll) {
|
|
|
|
|
return this.renderAll(name, props);
|
|
|
|
|
}
|
|
|
|
|
return this.renderSingle(name, props);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default ExtensionPoint;
|