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

106 lines
2.8 KiB
TypeScript
Raw Normal View History

2019-08-29 11:22:09 +00:00
import React from "react"
2019-03-19 15:21:27 +00:00
import BaseWidget, { IWidgetProps, IWidgetState } from "./BaseWidget"
2019-08-29 11:22:09 +00:00
import ContainerComponent from "../editorComponents/ContainerComponent"
2019-03-13 15:05:24 +00:00
import {
ContainerOrientation,
WidgetType,
2019-03-19 15:21:27 +00:00
} from "../constants/WidgetConstants"
import WidgetFactory from "../utils/WidgetFactory"
import _ from "lodash"
import { Color } from "../constants/StyleConstants"
import DroppableComponent from "../editorComponents/DroppableComponent"
2019-08-29 11:22:09 +00:00
const DEFAULT_NUM_COLS = 16
const DEFAULT_NUM_ROWS = 16
class ContainerWidget extends BaseWidget<
2019-08-29 11:22:09 +00:00
ContainerWidgetProps<IWidgetProps>,
ContainerWidgetState
> {
2019-08-29 11:22:09 +00:00
constructor(props: ContainerWidgetProps<IWidgetProps>) {
2019-03-19 15:21:27 +00:00
super(props)
this.renderChildWidget = this.renderChildWidget.bind(this)
this.state = {
width: 0,
height: 0,
2019-08-29 11:22:09 +00:00
snapColumnSpace: DEFAULT_NUM_COLS,
snapRowSpace: DEFAULT_NUM_ROWS
}
}
2019-08-29 11:22:09 +00:00
componentDidUpdate(previousProps: ContainerWidgetProps<IWidgetProps>) {
2019-03-19 15:21:27 +00:00
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 =
2019-03-19 15:21:27 +00:00
this.state.width / (this.props.snapColumns || DEFAULT_NUM_COLS)
if (this.state.height)
snapRowSpace =
2019-03-19 15:21:27 +00:00
this.state.height / (this.props.snapRows || DEFAULT_NUM_ROWS)
if (
this.state.snapColumnSpace !== snapColumnSpace ||
this.state.snapRowSpace !== snapRowSpace
) {
this.setState({
snapColumnSpace: snapColumnSpace,
snapRowSpace: snapRowSpace
})
}
}
renderChildWidget(childWidgetData: IWidgetProps) {
childWidgetData.parentColumnSpace = this.state.snapColumnSpace
childWidgetData.parentRowSpace = this.state.snapRowSpace
2019-03-19 15:21:27 +00:00
return WidgetFactory.createWidget(childWidgetData)
}
getPageView() {
return (
<ContainerComponent
widgetId={this.props.widgetId}
style={{
2019-03-19 14:05:48 +00:00
...this.getPositionStyle(),
2019-03-19 15:21:27 +00:00
backgroundColor: this.props.backgroundColor
}}
orientation={this.props.orientation || "VERTICAL"}
>
{_.map(this.props.children, this.renderChildWidget)}
</ContainerComponent>
2019-03-19 15:21:27 +00:00
)
}
getCanvasView() {
return (
<DroppableComponent
{...this.props}
style={{
...this.getPositionStyle()
}}
>
{super.getCanvasView()}
</DroppableComponent>
)
}
getWidgetType(): WidgetType {
2019-03-19 15:21:27 +00:00
return "CONTAINER_WIDGET"
}
}
2019-08-29 11:22:09 +00:00
export interface ContainerWidgetState extends IWidgetState {
snapColumnSpace: number;
snapRowSpace: number;
}
2019-08-29 11:22:09 +00:00
export interface ContainerWidgetProps<T extends IWidgetProps>
extends IWidgetProps {
2019-08-29 11:22:09 +00:00
children?: T[];
snapColumns?: number;
snapRows?: number;
orientation?: ContainerOrientation;
backgroundColor?: Color;
}
2019-03-19 15:21:27 +00:00
export default ContainerWidget