PromucFlow_constructor/app/client/src/widgets/ContainerWidget.tsx

111 lines
3.0 KiB
TypeScript
Raw Normal View History

2019-09-09 09:08:54 +00:00
import React from "react";
import BaseWidget, {
WidgetProps,
WidgetState,
WidgetFunctions,
} from "./BaseWidget";
2019-09-09 09:08:54 +00:00
import ContainerComponent from "../editorComponents/ContainerComponent";
import { ContainerOrientation, WidgetType } from "../constants/WidgetConstants";
import WidgetFactory from "../utils/WidgetFactory";
import _ from "lodash";
import { Color } from "../constants/Colors";
import DropTargetComponent from "../editorComponents/DropTargetComponent";
import { GridDefaults } from "../constants/WidgetConstants";
const { DEFAULT_GRID_COLUMNS, DEFAULT_GRID_ROWS } = GridDefaults;
class ContainerWidget extends BaseWidget<
2019-09-09 09:08:54 +00:00
ContainerWidgetProps<WidgetProps>,
2019-08-29 11:22:09 +00:00
ContainerWidgetState
> {
2019-09-09 09:08:54 +00:00
constructor(props: ContainerWidgetProps<WidgetProps>) {
super(props);
this.renderChildWidget = this.renderChildWidget.bind(this);
this.state = {
width: 0,
height: 0,
snapColumnSpace: DEFAULT_GRID_COLUMNS,
snapRowSpace: DEFAULT_GRID_ROWS,
2019-09-09 09:08:54 +00:00
};
}
2019-09-09 09:08:54 +00:00
componentDidUpdate(previousProps: ContainerWidgetProps<WidgetProps>) {
super.componentDidUpdate(previousProps);
let snapColumnSpace = this.state.snapColumnSpace;
let snapRowSpace = this.state.snapRowSpace;
2019-03-19 15:21:27 +00:00
if (this.state.width)
snapColumnSpace =
this.state.width / (this.props.snapColumns || DEFAULT_GRID_COLUMNS);
2019-03-19 15:21:27 +00:00
if (this.state.height)
snapRowSpace =
this.state.height / (this.props.snapRows || DEFAULT_GRID_ROWS);
if (
this.state.snapColumnSpace !== snapColumnSpace ||
this.state.snapRowSpace !== snapRowSpace
) {
this.setState({
snapColumnSpace: snapColumnSpace,
2019-09-09 09:08:54 +00:00
snapRowSpace: snapRowSpace,
});
}
}
2019-09-09 09:08:54 +00:00
renderChildWidget(childWidgetData: WidgetProps) {
childWidgetData.parentColumnSpace = this.state.snapColumnSpace;
childWidgetData.parentRowSpace = this.state.snapRowSpace;
const widgetFunctions: WidgetFunctions = this.props as WidgetFunctions;
return WidgetFactory.createWidget(
childWidgetData,
widgetFunctions,
this.props.renderMode,
);
}
getPageView() {
return (
<ContainerComponent
widgetId={this.props.widgetId}
style={{
2019-09-09 09:08:54 +00:00
...this.getPositionStyle(),
}}
orientation={this.props.orientation || "VERTICAL"}
>
{_.map(this.props.children, this.renderChildWidget)}
</ContainerComponent>
2019-09-09 09:08:54 +00:00
);
}
getCanvasView() {
return (
<DropTargetComponent
{...this.props}
style={{
2019-09-09 09:08:54 +00:00
...this.getPositionStyle(),
}}
>
{super.getCanvasView()}
</DropTargetComponent>
2019-09-09 09:08:54 +00:00
);
}
getWidgetType(): WidgetType {
2019-09-09 09:08:54 +00:00
return "CONTAINER_WIDGET";
}
}
2019-09-09 09:08:54 +00:00
export interface ContainerWidgetState extends WidgetState {
2019-08-29 11:22:09 +00:00
snapColumnSpace: number;
snapRowSpace: number;
}
2019-09-09 09:08:54 +00:00
export interface ContainerWidgetProps<T extends WidgetProps>
extends WidgetProps {
2019-08-29 11:22:09 +00:00
children?: T[];
snapColumns?: number;
snapRows?: number;
orientation?: ContainerOrientation;
backgroundColor?: Color;
}
2019-09-09 09:08:54 +00:00
export default ContainerWidget;