2021-09-09 15:10:22 +00:00
|
|
|
import React from "react";
|
2022-12-01 04:55:57 +00:00
|
|
|
import BaseWidget, { WidgetState } from "widgets/BaseWidget";
|
2021-09-23 15:14:24 +00:00
|
|
|
import {
|
2022-12-01 04:55:57 +00:00
|
|
|
EventType,
|
|
|
|
|
ExecuteTriggerPayload,
|
|
|
|
|
} from "constants/AppsmithActionConstants/ActionConstants";
|
|
|
|
|
import MenuButtonComponent from "../component";
|
2021-11-30 10:38:46 +00:00
|
|
|
import { MinimumPopupRows } from "widgets/constants";
|
2022-12-01 04:55:57 +00:00
|
|
|
import { MenuButtonWidgetProps, MenuItem, MenuItemsSource } from "../constants";
|
|
|
|
|
import contentConfig from "./propertyConfig/contentConfig";
|
|
|
|
|
import styleConfig from "./propertyConfig/styleConfig";
|
|
|
|
|
import equal from "fast-deep-equal/es6";
|
|
|
|
|
import { isArray, orderBy } from "lodash";
|
|
|
|
|
import { getSourceDataKeys } from "./helper";
|
2022-11-28 04:44:31 +00:00
|
|
|
import { Stylesheet } from "entities/AppTheming";
|
2021-07-13 08:05:09 +00:00
|
|
|
|
|
|
|
|
class MenuButtonWidget extends BaseWidget<MenuButtonWidgetProps, WidgetState> {
|
2022-08-15 08:05:46 +00:00
|
|
|
static getPropertyPaneContentConfig() {
|
2022-12-01 04:55:57 +00:00
|
|
|
return contentConfig;
|
2022-08-15 08:05:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static getPropertyPaneStyleConfig() {
|
2022-12-01 04:55:57 +00:00
|
|
|
return styleConfig;
|
2022-08-15 08:05:46 +00:00
|
|
|
}
|
|
|
|
|
|
2022-11-28 04:44:31 +00:00
|
|
|
static getStylesheetConfig(): Stylesheet {
|
|
|
|
|
return {
|
|
|
|
|
menuColor: "{{appsmith.theme.colors.primaryColor}}",
|
|
|
|
|
borderRadius: "{{appsmith.theme.borderRadius.appBorderRadius}}",
|
|
|
|
|
boxShadow: "none",
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-01 04:55:57 +00:00
|
|
|
menuItemClickHandler = (onClick: string | undefined, index: number) => {
|
2021-07-13 08:05:09 +00:00
|
|
|
if (onClick) {
|
2022-12-01 04:55:57 +00:00
|
|
|
const config: ExecuteTriggerPayload = {
|
2021-07-13 08:05:09 +00:00
|
|
|
triggerPropertyName: "onClick",
|
|
|
|
|
dynamicString: onClick,
|
|
|
|
|
event: {
|
|
|
|
|
type: EventType.ON_CLICK,
|
|
|
|
|
},
|
2022-12-01 04:55:57 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if (this.props.menuItemsSource === MenuItemsSource.DYNAMIC) {
|
|
|
|
|
config.globalContext = {
|
|
|
|
|
currentItem: this.props.sourceData
|
|
|
|
|
? this.props.sourceData[index]
|
|
|
|
|
: {},
|
|
|
|
|
currentIndex: index,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
super.executeAction(config);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
getVisibleItems = () => {
|
|
|
|
|
const {
|
|
|
|
|
configureMenuItems,
|
|
|
|
|
menuItems,
|
|
|
|
|
menuItemsSource,
|
|
|
|
|
sourceData,
|
|
|
|
|
} = this.props;
|
|
|
|
|
if (menuItemsSource === MenuItemsSource.STATIC) {
|
|
|
|
|
const visibleItems = Object.keys(menuItems)
|
|
|
|
|
.map((itemKey) => menuItems[itemKey])
|
|
|
|
|
.filter((item) => item.isVisible === true);
|
|
|
|
|
|
|
|
|
|
return orderBy(visibleItems, ["index"], ["asc"]);
|
|
|
|
|
} else if (
|
|
|
|
|
menuItemsSource === MenuItemsSource.DYNAMIC &&
|
|
|
|
|
isArray(sourceData) &&
|
|
|
|
|
sourceData?.length &&
|
|
|
|
|
configureMenuItems?.config
|
|
|
|
|
) {
|
|
|
|
|
const { config } = configureMenuItems;
|
|
|
|
|
const getValue = (propertyName: keyof MenuItem, index: number) => {
|
|
|
|
|
const value = config[propertyName];
|
|
|
|
|
|
|
|
|
|
if (isArray(value)) {
|
|
|
|
|
return value[index];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return value ?? null;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const visibleItems = sourceData
|
|
|
|
|
.map((item, index) => ({
|
|
|
|
|
...item,
|
|
|
|
|
id: index.toString(),
|
|
|
|
|
isVisible: getValue("isVisible", index),
|
|
|
|
|
isDisabled: getValue("isDisabled", index),
|
|
|
|
|
index: index,
|
|
|
|
|
widgetId: "",
|
|
|
|
|
label: getValue("label", index),
|
|
|
|
|
onClick: config?.onClick,
|
|
|
|
|
textColor: getValue("textColor", index),
|
|
|
|
|
backgroundColor: getValue("backgroundColor", index),
|
|
|
|
|
iconAlign: getValue("iconAlign", index),
|
|
|
|
|
iconColor: getValue("iconColor", index),
|
|
|
|
|
iconName: getValue("iconName", index),
|
|
|
|
|
}))
|
|
|
|
|
.filter((item) => item.isVisible === true);
|
|
|
|
|
|
|
|
|
|
return visibleItems;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return [];
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
componentDidMount = () => {
|
|
|
|
|
super.updateWidgetProperty("sourceDataKeys", getSourceDataKeys(this.props));
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
componentDidUpdate = (prevProps: MenuButtonWidgetProps) => {
|
|
|
|
|
if (!equal(prevProps.sourceData, this.props.sourceData)) {
|
|
|
|
|
super.updateWidgetProperty(
|
|
|
|
|
"sourceDataKeys",
|
|
|
|
|
getSourceDataKeys(this.props),
|
|
|
|
|
);
|
2021-07-13 08:05:09 +00:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
getPageView() {
|
2021-11-30 10:38:46 +00:00
|
|
|
const { componentWidth } = this.getComponentDimensions();
|
|
|
|
|
const menuDropDownWidth = MinimumPopupRows * this.props.parentColumnSpace;
|
|
|
|
|
|
2021-07-13 08:05:09 +00:00
|
|
|
return (
|
|
|
|
|
<MenuButtonComponent
|
|
|
|
|
{...this.props}
|
2022-12-01 04:55:57 +00:00
|
|
|
getVisibleItems={this.getVisibleItems}
|
2021-11-30 10:38:46 +00:00
|
|
|
menuDropDownWidth={menuDropDownWidth}
|
2021-07-13 08:05:09 +00:00
|
|
|
onItemClicked={this.menuItemClickHandler}
|
2022-03-13 17:21:04 +00:00
|
|
|
renderMode={this.props.renderMode}
|
2021-11-30 10:38:46 +00:00
|
|
|
width={componentWidth}
|
2021-07-13 08:05:09 +00:00
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-09 15:10:22 +00:00
|
|
|
static getWidgetType() {
|
|
|
|
|
return "MENU_BUTTON_WIDGET";
|
2021-07-13 08:05:09 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default MenuButtonWidget;
|