2022-08-19 10:10:36 +00:00
|
|
|
import equal from "fast-deep-equal/es6";
|
|
|
|
|
import React from "react";
|
|
|
|
|
|
|
|
|
|
import BaseWidget, { WidgetProps } from "./BaseWidget";
|
|
|
|
|
import {
|
|
|
|
|
MAIN_CONTAINER_WIDGET_ID,
|
|
|
|
|
RenderModes,
|
|
|
|
|
} from "constants/WidgetConstants";
|
|
|
|
|
import {
|
|
|
|
|
getWidgetEvalValues,
|
|
|
|
|
getIsWidgetLoading,
|
|
|
|
|
} from "selectors/dataTreeSelectors";
|
|
|
|
|
import {
|
|
|
|
|
getMainCanvasProps,
|
|
|
|
|
computeMainContainerWidget,
|
|
|
|
|
getChildWidgets,
|
|
|
|
|
getRenderMode,
|
|
|
|
|
} from "selectors/editorSelectors";
|
2022-08-24 12:16:32 +00:00
|
|
|
import { AppState } from "@appsmith/reducers";
|
2022-11-23 09:48:23 +00:00
|
|
|
import { useDispatch, useSelector } from "react-redux";
|
2022-08-19 10:10:36 +00:00
|
|
|
import { getWidget } from "sagas/selectors";
|
|
|
|
|
import {
|
|
|
|
|
createCanvasWidget,
|
|
|
|
|
createLoadingWidget,
|
|
|
|
|
} from "utils/widgetRenderUtils";
|
2022-11-23 09:48:23 +00:00
|
|
|
import { ReduxActionTypes } from "ce/constants/ReduxActionConstants";
|
|
|
|
|
import { checkContainersForAutoHeightAction } from "actions/autoHeightActions";
|
2022-08-19 10:10:36 +00:00
|
|
|
|
|
|
|
|
const WIDGETS_WITH_CHILD_WIDGETS = ["LIST_WIDGET", "FORM_WIDGET"];
|
|
|
|
|
|
|
|
|
|
function withWidgetProps(WrappedWidget: typeof BaseWidget) {
|
|
|
|
|
function WrappedPropsComponent(
|
|
|
|
|
props: WidgetProps & { skipWidgetPropsHydration?: boolean },
|
|
|
|
|
) {
|
|
|
|
|
const { children, skipWidgetPropsHydration, type, widgetId } = props;
|
|
|
|
|
|
|
|
|
|
const canvasWidget = useSelector((state: AppState) =>
|
|
|
|
|
getWidget(state, widgetId),
|
|
|
|
|
);
|
|
|
|
|
const mainCanvasProps = useSelector((state: AppState) =>
|
|
|
|
|
getMainCanvasProps(state),
|
|
|
|
|
);
|
|
|
|
|
const renderMode = useSelector(getRenderMode);
|
|
|
|
|
const evaluatedWidget = useSelector((state: AppState) =>
|
|
|
|
|
getWidgetEvalValues(state, canvasWidget?.widgetName),
|
|
|
|
|
);
|
|
|
|
|
const isLoading = useSelector((state: AppState) =>
|
|
|
|
|
getIsWidgetLoading(state, canvasWidget?.widgetName),
|
|
|
|
|
);
|
|
|
|
|
|
2022-11-23 09:48:23 +00:00
|
|
|
const dispatch = useDispatch();
|
|
|
|
|
|
2022-08-19 10:10:36 +00:00
|
|
|
const childWidgets = useSelector((state: AppState) => {
|
|
|
|
|
if (!WIDGETS_WITH_CHILD_WIDGETS.includes(type)) return undefined;
|
|
|
|
|
return getChildWidgets(state, widgetId);
|
|
|
|
|
}, equal);
|
|
|
|
|
|
|
|
|
|
let widgetProps: WidgetProps = {} as WidgetProps;
|
|
|
|
|
|
|
|
|
|
if (!skipWidgetPropsHydration) {
|
|
|
|
|
const canvasWidgetProps = (() => {
|
|
|
|
|
if (widgetId === MAIN_CONTAINER_WIDGET_ID) {
|
|
|
|
|
return computeMainContainerWidget(canvasWidget, mainCanvasProps);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return evaluatedWidget
|
|
|
|
|
? createCanvasWidget(canvasWidget, evaluatedWidget)
|
|
|
|
|
: createLoadingWidget(canvasWidget);
|
|
|
|
|
})();
|
|
|
|
|
|
|
|
|
|
widgetProps = { ...canvasWidgetProps };
|
2022-11-23 09:48:23 +00:00
|
|
|
|
2022-08-19 10:10:36 +00:00
|
|
|
/**
|
|
|
|
|
* MODAL_WIDGET by default is to be hidden unless the isVisible property is found.
|
|
|
|
|
* If the isVisible property is undefined and the widget is MODAL_WIDGET then isVisible
|
|
|
|
|
* is set to false
|
|
|
|
|
* If the isVisible property is undefined and the widget is not MODAL_WIDGET then isVisible
|
|
|
|
|
* is set to true
|
|
|
|
|
*/
|
|
|
|
|
widgetProps.isVisible =
|
|
|
|
|
canvasWidgetProps.isVisible ??
|
|
|
|
|
canvasWidgetProps.type !== "MODAL_WIDGET";
|
|
|
|
|
|
|
|
|
|
if (
|
|
|
|
|
props.type === "CANVAS_WIDGET" &&
|
|
|
|
|
widgetId !== MAIN_CONTAINER_WIDGET_ID
|
|
|
|
|
) {
|
|
|
|
|
widgetProps.rightColumn = props.rightColumn;
|
2022-11-23 09:48:23 +00:00
|
|
|
if (widgetProps.bottomRow === undefined) {
|
|
|
|
|
widgetProps.bottomRow = props.bottomRow;
|
|
|
|
|
widgetProps.minHeight = props.minHeight;
|
|
|
|
|
}
|
2022-08-19 10:10:36 +00:00
|
|
|
widgetProps.shouldScrollContents = props.shouldScrollContents;
|
|
|
|
|
widgetProps.canExtend = props.canExtend;
|
|
|
|
|
widgetProps.parentId = props.parentId;
|
|
|
|
|
} else if (widgetId !== MAIN_CONTAINER_WIDGET_ID) {
|
|
|
|
|
widgetProps.parentColumnSpace = props.parentColumnSpace;
|
|
|
|
|
widgetProps.parentRowSpace = props.parentRowSpace;
|
|
|
|
|
widgetProps.parentId = props.parentId;
|
|
|
|
|
|
|
|
|
|
// Form Widget Props
|
|
|
|
|
widgetProps.onReset = props.onReset;
|
|
|
|
|
if ("isFormValid" in props) widgetProps.isFormValid = props.isFormValid;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
widgetProps.children = children;
|
|
|
|
|
|
|
|
|
|
widgetProps.isLoading = isLoading;
|
|
|
|
|
widgetProps.childWidgets = childWidgets;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//merging with original props
|
|
|
|
|
widgetProps = { ...props, ...widgetProps, renderMode };
|
|
|
|
|
|
|
|
|
|
// isVisible prop defines whether to render a detached widget
|
|
|
|
|
if (widgetProps.detachFromLayout && !widgetProps.isVisible) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-23 09:48:23 +00:00
|
|
|
const shouldCollapseWidgetInViewOrPreviewMode =
|
|
|
|
|
!widgetProps.isVisible &&
|
|
|
|
|
(renderMode === RenderModes.PAGE || renderMode === RenderModes.PREVIEW) &&
|
|
|
|
|
widgetProps.bottomRow !== widgetProps.topRow;
|
|
|
|
|
|
|
|
|
|
const shouldResetCollapsedContainerHeightInViewOrPreviewMode =
|
|
|
|
|
widgetProps.isVisible && widgetProps.topRow === widgetProps.bottomRow;
|
|
|
|
|
|
|
|
|
|
const shouldResetCollapsedContainerHeightInCanvasMode =
|
|
|
|
|
widgetProps.topRow === widgetProps.bottomRow &&
|
|
|
|
|
renderMode === RenderModes.CANVAS;
|
|
|
|
|
|
2022-08-19 10:10:36 +00:00
|
|
|
// We don't render invisible widgets in view mode
|
2022-11-23 09:48:23 +00:00
|
|
|
if (shouldCollapseWidgetInViewOrPreviewMode) {
|
|
|
|
|
dispatch({
|
|
|
|
|
type: ReduxActionTypes.UPDATE_WIDGET_AUTO_HEIGHT,
|
|
|
|
|
payload: {
|
|
|
|
|
widgetId: props.widgetId,
|
|
|
|
|
height: 0,
|
|
|
|
|
},
|
|
|
|
|
});
|
2022-08-19 10:10:36 +00:00
|
|
|
return null;
|
2022-11-23 09:48:23 +00:00
|
|
|
} else if (
|
|
|
|
|
shouldResetCollapsedContainerHeightInViewOrPreviewMode ||
|
|
|
|
|
shouldResetCollapsedContainerHeightInCanvasMode
|
|
|
|
|
) {
|
|
|
|
|
dispatch(checkContainersForAutoHeightAction());
|
2022-08-19 10:10:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return <WrappedWidget {...widgetProps} />;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return WrappedPropsComponent;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default withWidgetProps;
|