PromucFlow_constructor/app/client/src/widgets/MenuButtonWidget/component/index.tsx
Dhruvik Neharia 1a7b4c6725
feat: Dynamic Menu Items - Menu Button Widget (#17652)
* faet: Add menu items source for menu widget

* feat: Add configuration panel for dynamic menu items

* feat: Pass down items from sourceData to menu items widget

* feat: Take menu items config from property pane for dynamic menu items

* fix: Change all onMenuItemClick to onClick for dynamic menu items

* feat: Create MenuComputeValue property control to add support for {{currentItem}} binding in menu widget

* feat: Add JS toggles for style properties for menu widget

* feat: onClick now supports currentItem for menu button widget

* feat: Add currentItem autocomplete, move property pane config to separate files for menu button widget

* feat: WIP - Add Dynamic Menu Items for Table Widget

* Revert "feat: WIP - Add Dynamic Menu Items for Table Widget"

This reverts commit 271f96211c8612bc6f073a1aab7467993b9d7e36.

* fix: remove current item label by default for dynamic menu items in menu button

* feat: Add source data max length 10 validation for dynamic menu items in menu button

* feat: Add migrations for Dynamic Menu Items for Menu Button Widget

* feat: Add cypress test for dynamic menu items for menu button

* test: Update DSLMigration test with menu button widget tests

* fix: Update MenuButtonWidget migration

* fix: DSL migrations for menu button dynmaic items

* fix: Style validations for menu widget

* feat: Add more descriptive help text for configure menu items in menu button widget

* feat: Change menu items source property type from dropdown to icon tabs

* fix: Cy test for menu button widget to select menu items source from button tabs instead of dropdown

* feat: Make ConfigureMenuItemsControl a Generic/reusable OpenNextPanelWithButtonControl

* refactor: Change MenuComputeValue to MenuButtonDynamicItemsControl

* refactor: Merge TABLE_PROPERTY and MENU_PROPERTY into one ARRAY_AND_ANY_PROPERTY

* fix: Don't polute Menu Button DSL with properties for dynamic menu items until the source is static

* style: Change color of curly braces hint in currentItem autocomplete to make it more readable

* fix: remove unused import

* refactor: Move child config panels to a different file, style: Change help text and placeholder for a few properties for Dynamic menu items - menu button

* refactor: Change event autocomplete function name, use fast equal

* refactor: Change source data validation function name and use camelCase throughout

* refactor: Validation function for source data

* refactor: Create different type for menuItems and configureMenuItems and reuse them property config

* feat: refactor: move get items to widget instead of component

* pref: Visible items to be calculated when menu button is clicked

* refactor: replace !("menuItemsSource" in child) with in migration

* refactor: Change controlType name from OPEN_NEXT_PANEL_WITH_BUTTON to OPEN_CONFIG_PANEL, use generic names inside OpenNextPanelWithButtonControl.tsx

* refactor: Minor cleanup at MenuButtonDynamicItemsControl.tsx

* refactor: Minor cleanup at MenuButtonDynamicItemsControl.tsx

* fix: Change constant used in migration to a static value

* test: Add tests for validations and helper for menu button

* test: Add more Cypress tests for dynamic-menu-items

* fix: Minor refactor at onclick handler and MenuButtonDynamicItemsControl

* refactor: Rename ARRAY_AND_ANY_PROPERTY to ARRAY_TYPE_OR_TYPE

* feat: Move initial source data and keys generation inside an update hook

* refactor: Rename ARRAY_TYPE_OR_TYPE to ARRAY_OF_TYPE_OR_TYPE

* refactor: Minor code refactor in MenuButtonWidget/widget/index.tsx

* refactor: Change OpenNextPanelWithButtonControl with OpenConfigPanelControl

* feat: Use traverseDSLAndMigrate for dynamic menu items migration

* style: Minor code hygiene changes here and there for dynamic menu items

* style: Minor code hygiene changes here and there for dynamic menu items

* style: remove any type for visible items inside dynamic menu items

* refactor: Change type MenuItems to MenuItem

* feat: Add support for dynamic menu items (menu button) inside list widget

* fix: updateMenuItemsSource hook not working when changing from DYNAMIC to STATIC menu items source

* fix: Avoid empty icon name from rendering inside button and menu item

* style: Fix a couple of code callouts

* fix: Update import from TernServer to CodemirrorTernService

* style: fix minor code callouts here and there

* fix: Add check for configureMenuItems.config

* fix: Add wait time after addOption click for DynamicHeight_Auto_Height_spec.js

* fix: Increase the wait time for DynamicHeight_Auto_Height_spec.js to 200ms

Co-authored-by: Aishwarya UR <aishwarya@appsmith.com>
2022-12-01 10:25:57 +05:30

415 lines
11 KiB
TypeScript

import * as React from "react";
import styled, { createGlobalStyle } from "styled-components";
import {
Alignment,
Button,
Icon,
Menu,
MenuItem as BlueprintMenuItem,
Classes as BlueprintClasses,
} from "@blueprintjs/core";
import { Classes, Popover2 } from "@blueprintjs/popover2";
import { IconName } from "@blueprintjs/icons";
import tinycolor from "tinycolor2";
import { darkenActive, darkenHover } from "constants/DefaultTheme";
import {
ButtonPlacement,
ButtonVariant,
ButtonVariantTypes,
} from "components/constants";
import {
getCustomBackgroundColor,
getCustomBorderColor,
getCustomHoverColor,
getComplementaryGrayscaleColor,
getCustomJustifyContent,
getAlignText,
WidgetContainerDiff,
lightenColor,
} from "widgets/WidgetUtils";
import { RenderMode } from "constants/WidgetConstants";
import { DragContainer } from "widgets/ButtonWidget/component/DragContainer";
import { THEMEING_TEXT_SIZES } from "constants/ThemeConstants";
import {
MenuButtonComponentProps,
MenuItem,
PopoverContentProps,
} from "../constants";
import { ThemeProp } from "widgets/constants";
const PopoverStyles = createGlobalStyle<{
parentWidth: number;
menuDropDownWidth: number;
id: string;
borderRadius: string;
}>`
.menu-button-popover, .${BlueprintClasses.MINIMAL}.menu-button-popover.${
Classes.POPOVER2
} {
background: none;
box-shadow: 0 6px 20px 0px rgba(0, 0, 0, 0.15) !important;
margin-top: 8px !important;
margin-bottom: 8px !important;
border-radius: ${({ borderRadius }) =>
borderRadius >= THEMEING_TEXT_SIZES.lg ? `0.375rem` : borderRadius};
overflow: hidden;
}
.menu-button-popover .${BlueprintClasses.MENU_ITEM} {
padding: 9px 12px;
border-radius: 0;
}
& > .${Classes.POPOVER2_TARGET} {
height: 100%;
}
${({ id, menuDropDownWidth, parentWidth }) => `
.menu-button-width-${id} {
max-width: ${
menuDropDownWidth > parentWidth
? `${menuDropDownWidth}px`
: `${parentWidth}px`
} !important;
min-width: ${
parentWidth > menuDropDownWidth ? parentWidth : menuDropDownWidth
}px !important;
}
`}
`;
export interface BaseStyleProps {
backgroundColor?: string;
borderRadius?: string;
boxShadow?: string;
buttonColor?: string;
buttonVariant?: ButtonVariant;
isCompact?: boolean;
textColor?: string;
placement?: ButtonPlacement;
}
const BaseButton = styled(Button)<ThemeProp & BaseStyleProps>`
height: 100%;
background-image: none !important;
font-weight: ${(props) => props.theme.fontWeights[2]};
outline: none;
padding: 0px 10px;
overflow: hidden;
border: 1.2px solid #ebebeb;
border-radius: 0;
box-shadow: none !important;
${({ buttonColor, buttonVariant, theme }) => `
background: ${
getCustomBackgroundColor(buttonVariant, buttonColor) !== "none"
? getCustomBackgroundColor(buttonVariant, buttonColor)
: buttonVariant === ButtonVariantTypes.PRIMARY
? theme.colors.button.primary.primary.bgColor
: "none"
} !important;
&:hover, &:active {
background: ${
getCustomHoverColor(theme, buttonVariant, buttonColor) !== "none"
? getCustomHoverColor(theme, buttonVariant, buttonColor)
: buttonVariant === ButtonVariantTypes.SECONDARY
? theme.colors.button.primary.secondary.hoverColor
: buttonVariant === ButtonVariantTypes.TERTIARY
? theme.colors.button.primary.tertiary.hoverColor
: theme.colors.button.primary.primary.hoverColor
} !important;
}
&:disabled {
border: ${
buttonVariant === ButtonVariantTypes.SECONDARY
? "1px solid var(--wds-color-border-disabled)"
: "none"
} !important;
background: ${
buttonVariant !== ButtonVariantTypes.TERTIARY
? "var(--wds-color-bg-disabled)"
: "transparent"
} !important;
color: var(--wds-color-text-disabled) !important;
span {
color: var(--wds-color-text-disabled) !important;
}
}
border: ${
getCustomBorderColor(buttonVariant, buttonColor) !== "none"
? `1px solid ${getCustomBorderColor(buttonVariant, buttonColor)}`
: buttonVariant === ButtonVariantTypes.SECONDARY
? `1px solid ${theme.colors.button.primary.secondary.borderColor}`
: "none"
} !important;
& > span {
text-overflow: ellipsis;
display: -webkit-box;
-webkit-line-clamp: 1;
-webkit-box-orient: vertical;
max-height: 100%;
overflow: hidden;
color: ${
buttonVariant === ButtonVariantTypes.PRIMARY
? getComplementaryGrayscaleColor(buttonColor)
: getCustomBackgroundColor(
ButtonVariantTypes.PRIMARY,
buttonColor,
) !== "none"
? getCustomBackgroundColor(ButtonVariantTypes.PRIMARY, buttonColor)
: `${theme.colors.button.primary.secondary.textColor}`
} !important;
}
`}
border-radius: ${({ borderRadius }) => borderRadius};
box-shadow: ${({ boxShadow }) => boxShadow} !important;
${({ placement }) =>
placement
? `
justify-content: ${getCustomJustifyContent(placement)};
& > span.bp3-button-text {
flex: unset !important;
}
`
: ""}
`;
const BaseMenuItem = styled(BlueprintMenuItem)<ThemeProp & BaseStyleProps>`
font-family: var(--wds-font-family);
${({ backgroundColor, theme }) =>
backgroundColor
? `
background-color: ${backgroundColor} !important;
&:hover {
background-color: ${darkenHover(backgroundColor)} !important;
}
&:active {
background-color: ${darkenActive(backgroundColor)} !important;
}
`
: `
background: none !important
&:hover {
background-color: ${tinycolor(
theme.colors.button.primary.primary.textColor,
)
.darken()
.toString()} !important;
}
&:active {
background-color: ${tinycolor(
theme.colors.button.primary.primary.textColor,
)
.darken()
.toString()} !important;
}
`}
${({ textColor }) =>
textColor &&
`
color: ${textColor} !important;
`}
${({ isCompact }) =>
isCompact &&
`
padding-top: 3px !important;
padding-bottom: 3px !important;
font-size: 12px;
`}
`;
const StyledMenu = styled(Menu)<{
backgroundColor?: string;
}>`
padding: 0;
min-width: 0px;
overflow: hidden;
${BlueprintClasses.MENU_ITEM}:hover {
background-color: ${({ backgroundColor }) => lightenColor(backgroundColor)};
}
`;
function PopoverContent(props: PopoverContentProps) {
const { backgroundColor, getVisibleItems, isCompact, onItemClicked } = props;
const visibleItems = getVisibleItems();
if (!visibleItems?.length) {
return <StyledMenu />;
} else {
const listItems = visibleItems.map((item: MenuItem, index: number) => {
const {
backgroundColor,
iconAlign,
iconColor,
iconName,
id,
isDisabled,
label,
onClick,
textColor,
} = item;
return (
<BaseMenuItem
backgroundColor={backgroundColor}
disabled={isDisabled}
icon={
iconAlign !== Alignment.RIGHT && iconName ? (
<Icon color={iconColor} icon={iconName} />
) : null
}
isCompact={isCompact}
key={id}
labelElement={
iconAlign === Alignment.RIGHT && iconName ? (
<Icon color={iconColor} icon={iconName} />
) : null
}
onClick={() => onItemClicked(onClick, index)}
text={label}
textColor={textColor}
/>
);
});
return (
<StyledMenu backgroundColor={backgroundColor}>{listItems}</StyledMenu>
);
}
}
export interface PopoverTargetButtonProps {
borderRadius?: string;
boxShadow?: string;
buttonColor?: string;
buttonVariant?: ButtonVariant;
iconName?: IconName;
iconAlign?: Alignment;
isDisabled?: boolean;
label?: string;
placement?: ButtonPlacement;
renderMode?: RenderMode;
}
function PopoverTargetButton(props: PopoverTargetButtonProps) {
const {
borderRadius,
boxShadow,
buttonColor,
buttonVariant,
iconAlign,
iconName,
isDisabled,
label,
placement,
renderMode,
} = props;
const isRightAlign = iconAlign === Alignment.RIGHT;
return (
<DragContainer
buttonColor={buttonColor}
buttonVariant={buttonVariant}
disabled={isDisabled}
renderMode={renderMode}
>
<BaseButton
alignText={getAlignText(isRightAlign, iconName)}
borderRadius={borderRadius}
boxShadow={boxShadow}
buttonColor={buttonColor}
buttonVariant={buttonVariant}
disabled={isDisabled}
fill
icon={!isRightAlign && iconName ? iconName : null}
placement={placement}
rightIcon={isRightAlign && iconName ? iconName : null}
text={label}
/>
</DragContainer>
);
}
function MenuButtonComponent(props: MenuButtonComponentProps) {
const {
borderRadius,
boxShadow,
configureMenuItems,
getVisibleItems,
iconAlign,
iconName,
isCompact,
isDisabled,
label,
menuColor,
menuDropDownWidth,
menuItems,
menuItemsSource,
menuVariant,
onItemClicked,
placement,
renderMode,
sourceData,
widgetId,
width,
} = props;
return (
<>
<PopoverStyles
borderRadius={borderRadius}
id={widgetId}
menuDropDownWidth={menuDropDownWidth}
parentWidth={width - WidgetContainerDiff}
/>
<Popover2
content={
<PopoverContent
backgroundColor={menuColor}
borderRadius={borderRadius}
configureMenuItems={configureMenuItems}
getVisibleItems={getVisibleItems}
isCompact={isCompact}
menuItems={menuItems}
menuItemsSource={menuItemsSource}
onItemClicked={onItemClicked}
sourceData={sourceData}
/>
}
disabled={isDisabled}
fill
minimal
placement="bottom-end"
popoverClassName={`menu-button-popover menu-button-width-${widgetId}`}
>
<PopoverTargetButton
borderRadius={borderRadius}
boxShadow={boxShadow}
buttonColor={menuColor}
buttonVariant={menuVariant}
iconAlign={iconAlign}
iconName={iconName}
isDisabled={isDisabled}
label={label}
placement={placement}
renderMode={renderMode}
/>
</Popover2>
</>
);
}
export default MenuButtonComponent;