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

117 lines
3.3 KiB
TypeScript
Raw Normal View History

import React from "react";
2019-09-09 09:08:54 +00:00
import _ from "lodash";
import ContainerComponent, {
ContainerStyle,
} from "components/designSystems/appsmith/ContainerComponent";
import { ContainerOrientation, WidgetType } from "constants/WidgetConstants";
import WidgetFactory from "utils/WidgetFactory";
import { Color } from "constants/Colors";
import DropTargetComponent from "components/editorComponents/DropTargetComponent";
2020-01-16 11:46:21 +00:00
import {
GridDefaults,
CONTAINER_GRID_PADDING,
WIDGET_PADDING,
} from "constants/WidgetConstants";
import ResizeBoundsContainerComponent from "components/editorComponents/ResizeBoundsContainerComponent";
import BaseWidget, { WidgetProps, WidgetState } from "./BaseWidget";
const { DEFAULT_GRID_COLUMNS, DEFAULT_GRID_ROW_HEIGHT } = 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 = {
componentWidth: 0,
componentHeight: 0,
snapColumnSpace: 0,
snapRowSpace: 0,
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;
if (this.state.componentWidth)
2020-01-16 11:46:21 +00:00
snapColumnSpace =
(this.state.componentWidth -
(CONTAINER_GRID_PADDING + WIDGET_PADDING) * 2) /
(this.props.snapColumns || DEFAULT_GRID_COLUMNS);
if (this.state.snapColumnSpace !== snapColumnSpace) {
this.setState({
snapColumnSpace,
snapRowSpace: DEFAULT_GRID_ROW_HEIGHT,
2019-09-09 09:08:54 +00:00
});
}
}
renderChildWidget(childWidgetData: WidgetProps): JSX.Element {
2019-09-09 09:08:54 +00:00
childWidgetData.parentColumnSpace = this.state.snapColumnSpace;
childWidgetData.parentRowSpace = this.state.snapRowSpace;
childWidgetData.parentId = this.props.widgetId;
return WidgetFactory.createWidget(childWidgetData, this.props.renderMode);
}
renderChildren = () => {
return _.map(this.props.children, this.renderChildWidget);
};
renderAsDropTarget() {
return (
<DropTargetComponent {...this.props} {...this.state}>
<ResizeBoundsContainerComponent {...this.props}>
{this.renderChildren()}
</ResizeBoundsContainerComponent>
</DropTargetComponent>
);
}
getContainerComponentProps = () => {
const containerProps: ContainerWidgetProps<WidgetProps> = { ...this.props };
containerProps.backgroundColor = this.props.backgroundColor || "white";
2020-01-16 11:46:21 +00:00
return containerProps;
};
renderAsContainerComponent() {
return (
<ContainerComponent {...this.getContainerComponentProps()}>
{this.renderChildren()}
</ContainerComponent>
2019-09-09 09:08:54 +00:00
);
}
getPageView() {
return this.renderAsContainerComponent();
}
2019-10-08 06:19:10 +00:00
getCanvasView() {
return this.renderAsDropTarget();
}
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;
containerStyle?: ContainerStyle;
}
2019-09-09 09:08:54 +00:00
export default ContainerWidget;