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

109 lines
2.8 KiB
TypeScript
Raw Normal View History

2019-03-19 15:21:27 +00:00
import BaseWidget, { IWidgetProps, IWidgetState } from "./BaseWidget"
import ContainerComponent, {
IContainerProps
2019-03-19 15:21:27 +00:00
} from "../editorComponents/ContainerComponent"
2019-03-13 15:05:24 +00:00
import {
ContainerOrientation,
WidgetType,
CSSUnits
2019-03-19 15:21:27 +00:00
} from "../constants/WidgetConstants"
import WidgetFactory from "../utils/WidgetFactory"
import React from "react"
import _ from "lodash"
import { Color } from "../constants/StyleConstants"
import DroppableComponent from "../editorComponents/DroppableComponent"
2019-03-19 15:21:27 +00:00
const DEFAULT_NUM_COLS = 13
const DEFAULT_NUM_ROWS = 13
class ContainerWidget extends BaseWidget<
IContainerWidgetProps<IWidgetProps>,
IContainerWidgetState
> {
constructor(props: IContainerWidgetProps<IWidgetProps>) {
2019-03-19 15:21:27 +00:00
super(props)
this.renderChildWidget = this.renderChildWidget.bind(this)
this.state = {
width: 0,
height: 0,
snapColumnSpace: 1,
snapRowSpace: 1
}
}
2019-03-19 15:21:27 +00:00
componentDidUpdate(previousProps: IContainerWidgetProps<IWidgetProps>) {
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"
}
}
export interface IContainerWidgetState extends IWidgetState {
snapColumnSpace: number
snapRowSpace: number
}
export interface IContainerWidgetProps<T extends IWidgetProps>
extends IWidgetProps {
2019-03-19 15:21:27 +00:00
children?: T[]
snapColumns?: number
snapRows?: number
orientation?: ContainerOrientation
backgroundColor?: Color
}
2019-03-19 15:21:27 +00:00
export default ContainerWidget