2023-10-02 19:41:05 +00:00
|
|
|
import React, { useCallback, useEffect, useMemo, useState } from "react";
|
|
|
|
|
import type { CSSProperties, MouseEvent } from "react";
|
|
|
|
|
import { Flex } from "@design-system/widgets";
|
|
|
|
|
import { useSelector } from "react-redux";
|
|
|
|
|
|
|
|
|
|
import { snipingModeSelector } from "selectors/editorSelectors";
|
|
|
|
|
import { useClickToSelectWidget } from "utils/hooks/useClickToSelectWidget";
|
|
|
|
|
import { usePositionedContainerZIndex } from "utils/hooks/usePositionedContainerZIndex";
|
|
|
|
|
import {
|
|
|
|
|
isCurrentWidgetFocused,
|
|
|
|
|
isWidgetSelected,
|
|
|
|
|
} from "selectors/widgetSelectors";
|
|
|
|
|
import { widgetTypeClassname } from "widgets/WidgetUtils";
|
|
|
|
|
import {
|
|
|
|
|
FlexVerticalAlignment,
|
|
|
|
|
ResponsiveBehavior,
|
|
|
|
|
} from "layoutSystems/common/utils/constants";
|
|
|
|
|
import type { FlexProps } from "@design-system/widgets/src/components/Flex/src/types";
|
|
|
|
|
import { checkIsDropTarget } from "WidgetProvider/factory/helpers";
|
|
|
|
|
import type { AnvilFlexComponentProps } from "../utils/types";
|
|
|
|
|
import {
|
|
|
|
|
getResponsiveMinWidth,
|
|
|
|
|
validateResponsiveProp,
|
|
|
|
|
} from "../utils/widgetUtils";
|
|
|
|
|
import WidgetFactory from "WidgetProvider/factory";
|
|
|
|
|
import type { WidgetProps } from "widgets/BaseWidget";
|
|
|
|
|
import type { WidgetConfigProps } from "WidgetProvider/constants";
|
2023-10-18 13:39:07 +00:00
|
|
|
import { usePositionObserver } from "layoutSystems/common/utils/LayoutElementPositionsObserver/usePositionObserver";
|
2023-10-19 15:14:10 +00:00
|
|
|
import { useWidgetBorderStyles } from "./hooks/useWidgetBorderStyles";
|
2023-10-19 20:27:40 +00:00
|
|
|
import { getAnvilWidgetDOMId } from "layoutSystems/common/utils/LayoutElementPositionsObserver/utils";
|
|
|
|
|
import type { AppState } from "@appsmith/reducers";
|
2023-10-02 19:41:05 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Adds following functionalities to the widget:
|
|
|
|
|
* 1. Click handler to select the widget and open property pane.
|
|
|
|
|
* 2. Widget size based on responsiveBehavior:
|
|
|
|
|
* 2a. Hug widgets will stick to the size provided to them. (flex: 0 0 auto;)
|
|
|
|
|
* 2b. Fill widgets will automatically take up all available width in the parent container. (flex: 1 1 0%;)
|
|
|
|
|
* 3. Widgets can optionally have auto width or height which is dictated by the props.
|
|
|
|
|
*
|
|
|
|
|
* Uses Flex component provided by WDS.
|
|
|
|
|
* @param props | AnvilFlexComponentProps
|
|
|
|
|
* @returns Widget
|
|
|
|
|
*/
|
|
|
|
|
|
2023-10-18 13:39:07 +00:00
|
|
|
export function AnvilFlexComponent(props: AnvilFlexComponentProps) {
|
2023-10-02 19:41:05 +00:00
|
|
|
const isDropTarget = checkIsDropTarget(props.widgetType);
|
|
|
|
|
const isFocused = useSelector(isCurrentWidgetFocused(props.widgetId));
|
|
|
|
|
const isSelected = useSelector(isWidgetSelected(props.widgetId));
|
|
|
|
|
const isSnipingMode = useSelector(snipingModeSelector);
|
2023-10-19 20:27:40 +00:00
|
|
|
const isDragging = useSelector(
|
|
|
|
|
(state: AppState) => state.ui.widgetDragResize.isDragging,
|
|
|
|
|
);
|
2023-10-02 19:41:05 +00:00
|
|
|
|
2023-10-18 13:39:07 +00:00
|
|
|
/** POSITIONS OBSERVER LOGIC */
|
|
|
|
|
// Create a ref so that this DOM node can be
|
|
|
|
|
// observed by the observer for changes in size
|
|
|
|
|
const ref = React.useRef<HTMLDivElement>(null);
|
2023-11-15 12:32:12 +00:00
|
|
|
usePositionObserver(
|
|
|
|
|
"widget",
|
|
|
|
|
{ widgetId: props.widgetId, layoutId: props.layoutId },
|
|
|
|
|
ref,
|
|
|
|
|
);
|
2023-10-18 13:39:07 +00:00
|
|
|
/** EO POSITIONS OBSERVER LOGIC */
|
|
|
|
|
|
2023-10-02 19:41:05 +00:00
|
|
|
const [isFillWidget, setIsFillWidget] = useState<boolean>(false);
|
|
|
|
|
const [verticalAlignment, setVerticalAlignment] =
|
|
|
|
|
useState<FlexVerticalAlignment>(FlexVerticalAlignment.Top);
|
|
|
|
|
|
|
|
|
|
const clickToSelectWidget = useClickToSelectWidget(props.widgetId);
|
|
|
|
|
const onClickFn = useCallback(
|
|
|
|
|
(e) => {
|
|
|
|
|
clickToSelectWidget(e);
|
|
|
|
|
},
|
|
|
|
|
[props.widgetId, clickToSelectWidget],
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const stopEventPropagation = (e: MouseEvent<HTMLElement>) => {
|
|
|
|
|
!isSnipingMode && e.stopPropagation();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
const widgetConfig:
|
|
|
|
|
| (Partial<WidgetProps> & WidgetConfigProps & { type: string })
|
|
|
|
|
| undefined = WidgetFactory.getConfig(props.widgetType);
|
|
|
|
|
if (!widgetConfig) return;
|
|
|
|
|
setIsFillWidget(
|
|
|
|
|
widgetConfig?.responsiveBehavior === ResponsiveBehavior.Fill,
|
|
|
|
|
);
|
|
|
|
|
setVerticalAlignment(
|
|
|
|
|
widgetConfig?.flexVerticalAlignment || FlexVerticalAlignment.Top,
|
|
|
|
|
);
|
|
|
|
|
}, [props.widgetType]);
|
|
|
|
|
|
|
|
|
|
const { onHoverZIndex } = usePositionedContainerZIndex(
|
|
|
|
|
isDropTarget,
|
|
|
|
|
props.widgetId,
|
|
|
|
|
isFocused,
|
|
|
|
|
isSelected,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const className = useMemo(
|
|
|
|
|
() =>
|
|
|
|
|
`anvil-layout-parent-${props.parentId} anvil-layout-child-${
|
|
|
|
|
props.widgetId
|
|
|
|
|
} ${widgetTypeClassname(
|
|
|
|
|
props.widgetType,
|
2023-10-24 11:37:06 +00:00
|
|
|
)} t--widget-${props.widgetName.toLowerCase()} drop-target-${
|
|
|
|
|
props.layoutId
|
2023-12-29 03:11:05 +00:00
|
|
|
} row-index-${props.rowIndex} anvil-widget-wrapper`,
|
2023-10-24 11:37:06 +00:00
|
|
|
[
|
|
|
|
|
props.parentId,
|
|
|
|
|
props.widgetId,
|
|
|
|
|
props.widgetType,
|
|
|
|
|
props.widgetName,
|
|
|
|
|
props.layoutId,
|
|
|
|
|
props.rowIndex,
|
|
|
|
|
],
|
2023-10-02 19:41:05 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// Memoize flex props to be passed to the WDS Flex component.
|
|
|
|
|
// If the widget is being resized => update width and height to auto.
|
|
|
|
|
const flexProps: FlexProps = useMemo(() => {
|
|
|
|
|
const data: FlexProps = {
|
|
|
|
|
alignSelf: verticalAlignment || FlexVerticalAlignment.Top,
|
2023-12-27 10:35:41 +00:00
|
|
|
flexGrow: props.flexGrow ? props.flexGrow : isFillWidget ? 1 : 0,
|
2023-10-02 19:41:05 +00:00
|
|
|
flexShrink: isFillWidget ? 1 : 0,
|
|
|
|
|
flexBasis: isFillWidget ? "0%" : "auto",
|
2023-10-19 20:27:40 +00:00
|
|
|
height: "auto",
|
2023-12-21 12:03:28 +00:00
|
|
|
padding: "spacing-1",
|
2023-10-19 20:27:40 +00:00
|
|
|
width: "auto",
|
2023-12-29 03:11:05 +00:00
|
|
|
minHeight: { base: "var(--sizing-12)" },
|
|
|
|
|
alignItems: "center",
|
2023-10-02 19:41:05 +00:00
|
|
|
};
|
|
|
|
|
if (props?.widgetSize) {
|
|
|
|
|
// adding min max limits only if they are available, as WDS Flex doesn't handle undefined values.
|
|
|
|
|
if (validateResponsiveProp(props.widgetSize?.maxHeight)) {
|
|
|
|
|
data.maxHeight = props.widgetSize.maxHeight;
|
|
|
|
|
}
|
|
|
|
|
if (validateResponsiveProp(props.widgetSize?.maxWidth)) {
|
|
|
|
|
data.maxWidth = props.widgetSize.maxWidth;
|
|
|
|
|
}
|
2023-12-29 03:11:05 +00:00
|
|
|
|
2023-10-02 19:41:05 +00:00
|
|
|
if (validateResponsiveProp(props.widgetSize?.minWidth)) {
|
|
|
|
|
// Setting a base of 100% for Fill widgets to ensure that they expand on smaller sizes.
|
|
|
|
|
data.minWidth = getResponsiveMinWidth(
|
|
|
|
|
props.widgetSize?.minWidth,
|
|
|
|
|
isFillWidget,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return data;
|
2023-12-27 10:35:41 +00:00
|
|
|
}, [isFillWidget, props.widgetSize, verticalAlignment, props.flexGrow]);
|
2023-10-02 19:41:05 +00:00
|
|
|
|
2023-10-19 15:14:10 +00:00
|
|
|
const borderStyles = useWidgetBorderStyles(props.widgetId);
|
|
|
|
|
|
2023-10-02 19:41:05 +00:00
|
|
|
const styleProps: CSSProperties = useMemo(() => {
|
|
|
|
|
return {
|
|
|
|
|
position: "relative",
|
2023-10-27 09:03:42 +00:00
|
|
|
// overflow is set to make sure widgets internal components/divs don't overflow this boundary causing scrolls
|
2023-12-29 03:11:05 +00:00
|
|
|
overflow: "visible",
|
2023-11-15 12:31:54 +00:00
|
|
|
opacity: (isDragging && isSelected) || !props.isVisible ? 0.5 : 1,
|
2023-10-02 19:41:05 +00:00
|
|
|
"&:hover": {
|
|
|
|
|
zIndex: onHoverZIndex,
|
|
|
|
|
},
|
2023-10-19 15:14:10 +00:00
|
|
|
...borderStyles,
|
2023-10-02 19:41:05 +00:00
|
|
|
};
|
2023-12-26 14:16:58 +00:00
|
|
|
}, [borderStyles, isDragging, isSelected, onHoverZIndex]);
|
2023-10-02 19:41:05 +00:00
|
|
|
|
|
|
|
|
return (
|
2023-10-19 20:27:40 +00:00
|
|
|
<Flex
|
|
|
|
|
{...flexProps}
|
|
|
|
|
className={className}
|
|
|
|
|
id={getAnvilWidgetDOMId(props.widgetId)}
|
|
|
|
|
ref={ref}
|
|
|
|
|
style={styleProps}
|
|
|
|
|
>
|
2023-10-02 19:41:05 +00:00
|
|
|
<div
|
|
|
|
|
className="w-full h-full"
|
|
|
|
|
onClick={stopEventPropagation}
|
|
|
|
|
onClickCapture={onClickFn}
|
|
|
|
|
>
|
|
|
|
|
{props.children}
|
|
|
|
|
</div>
|
|
|
|
|
</Flex>
|
|
|
|
|
);
|
2023-10-18 13:39:07 +00:00
|
|
|
}
|