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

130 lines
3.8 KiB
TypeScript
Raw Normal View History

import React from "react";
2019-09-09 09:08:54 +00:00
import _ from "lodash";
import ContainerComponent, {
ContainerStyle,
2020-02-13 09:32:24 +00:00
ContainerComponentProps,
} 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,
2020-02-13 09:32:24 +00:00
MAIN_CONTAINER_WIDGET_ID,
RenderModes,
2020-01-16 11:46:21 +00:00
} 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,
2020-02-07 02:32:52 +00:00
meta: {},
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
});
}
}
2020-03-06 09:45:21 +00:00
renderChildWidget(childWidgetData: WidgetProps): React.ReactNode {
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(
// sort by row so stacking context is correct
// TODO(abhinav): This is hacky. The stacking context should increase for widgets rendered top to bottom, always.
// Figure out a way in which the stacking context is consitent.
_.sortBy(this.props.children, child => {
return this.props.renderMode === RenderModes.CANVAS
? child.topRow
: -child.topRow;
}),
this.renderChildWidget,
);
};
renderAsDropTarget() {
return (
<DropTargetComponent {...this.props} {...this.state}>
<ResizeBoundsContainerComponent {...this.props}>
{this.renderChildren()}
</ResizeBoundsContainerComponent>
</DropTargetComponent>
);
}
2020-02-13 09:32:24 +00:00
getContainerComponentProps = (): ContainerComponentProps => ({
...this.props,
isMainContainer: this.props.widgetId === MAIN_CONTAINER_WIDGET_ID,
backgroundColor: this.props.backgroundColor || "white",
});
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;