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

101 lines
3.1 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 { WidgetType, WidgetTypes } from "constants/WidgetConstants";
import WidgetFactory from "utils/WidgetFactory";
2020-01-16 11:46:21 +00:00
import {
GridDefaults,
CONTAINER_GRID_PADDING,
WIDGET_PADDING,
} from "constants/WidgetConstants";
import BaseWidget, { WidgetProps, WidgetState } from "./BaseWidget";
class ContainerWidget extends BaseWidget<
2019-09-09 09:08:54 +00:00
ContainerWidgetProps<WidgetProps>,
WidgetState
> {
2019-09-09 09:08:54 +00:00
constructor(props: ContainerWidgetProps<WidgetProps>) {
super(props);
this.renderChildWidget = this.renderChildWidget.bind(this);
}
getSnapSpaces = () => {
const { componentWidth } = this.getComponentDimensions();
return {
snapRowSpace: GridDefaults.DEFAULT_GRID_ROW_HEIGHT,
snapColumnSpace: componentWidth
? (componentWidth - (CONTAINER_GRID_PADDING + WIDGET_PADDING) * 2) /
GridDefaults.DEFAULT_GRID_COLUMNS
: 0,
};
};
renderChildWidget(childWidgetData: WidgetProps): React.ReactNode {
// For now, isVisible prop defines whether to render a detached widget
if (childWidgetData.detachFromLayout && !childWidgetData.isVisible) {
return null;
}
const snapSpaces = this.getSnapSpaces();
const { componentWidth, componentHeight } = this.getComponentDimensions();
if (!childWidgetData.detachFromLayout) {
childWidgetData.parentColumnSpace = snapSpaces.snapColumnSpace;
childWidgetData.parentRowSpace = snapSpaces.snapRowSpace;
} else {
// This is for the detached child like the default CANVAS_WIDGET child
childWidgetData.rightColumn = componentWidth;
childWidgetData.bottomRow = this.props.shouldScrollContents
? childWidgetData.bottomRow
: componentHeight;
childWidgetData.minHeight = componentHeight;
childWidgetData.isVisible = this.props.isVisible;
childWidgetData.shouldScrollContents = false;
childWidgetData.canExtend = this.props.shouldScrollContents;
}
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.
2020-03-13 07:24:03 +00:00
// Figure out a way in which the stacking context is consistent.
_.sortBy(_.compact(this.props.children), child => child.topRow),
this.renderChildWidget,
);
};
renderAsContainerComponent(props: ContainerWidgetProps<WidgetProps>) {
return (
<ContainerComponent {...props}>
{this.renderChildren()}
</ContainerComponent>
2019-09-09 09:08:54 +00:00
);
}
getPageView() {
return this.renderAsContainerComponent(this.props);
}
getWidgetType(): WidgetType {
return WidgetTypes.CONTAINER_WIDGET;
}
}
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[];
containerStyle?: ContainerStyle;
shouldScrollContents?: boolean;
}
2019-09-09 09:08:54 +00:00
export default ContainerWidget;