PromucFlow_constructor/app/client/src/components/editorComponents/ContextDropdown.tsx

110 lines
3.0 KiB
TypeScript
Raw Normal View History

2019-11-21 10:52:49 +00:00
import React, { ReactNode } from "react";
import styled from "styled-components";
import { ItemRenderer, Select } from "@blueprintjs/select";
2020-01-24 09:54:40 +00:00
import {
Button,
MenuItem,
Intent as BlueprintIntent,
PopoverPosition,
2020-01-27 13:53:33 +00:00
PopoverInteractionKind,
2020-01-24 09:54:40 +00:00
} from "@blueprintjs/core";
2019-11-21 10:52:49 +00:00
import { ControlIconName, ControlIcons } from "icons/ControlIcons";
import { noop } from "utils/AppsmithUtils";
import { Intent } from "constants/DefaultTheme";
import { IconProps } from "constants/IconConstants";
import { Colors } from "constants/Colors";
import { DropdownOption } from "components/constants";
2019-11-21 10:52:49 +00:00
export type ContextDropdownOption = DropdownOption & {
onSelect: (event: React.MouseEvent<HTMLElement, MouseEvent>) => void;
intent?: Intent;
2020-01-24 09:54:40 +00:00
children?: ContextDropdownOption[];
2019-11-21 10:52:49 +00:00
};
const Dropdown = Select.ofType<ContextDropdownOption>();
const StyledMenuItem = styled(MenuItem)`
&&&&.bp3-menu-item:hover {
2020-12-24 04:32:25 +00:00
background: ${(props) => props.theme.colors.primaryOld};
color: ${(props) => props.theme.colors.textOnDarkBG};
2019-11-21 10:52:49 +00:00
}
&&&.bp3-menu-item.bp3-intent-danger:hover {
2020-12-24 04:32:25 +00:00
background: ${(props) => props.theme.colors.error};
2019-11-21 10:52:49 +00:00
}
`;
type ContextDropdownProps = {
options: ContextDropdownOption[];
className: string;
toggle: {
type: "icon" | "button";
icon?: ControlIconName;
iconSize?: number;
2019-11-21 10:52:49 +00:00
text?: string;
placeholder?: string;
color?: string;
2019-11-21 10:52:49 +00:00
};
};
function DropdownItem(option: ContextDropdownOption) {
return (
<StyledMenuItem
intent={option.intent as BlueprintIntent}
key={option.value}
onClick={option.onSelect}
popoverProps={{
minimal: true,
hoverCloseDelay: 0,
hoverOpenDelay: 300,
interactionKind: PopoverInteractionKind.CLICK,
position: PopoverPosition.RIGHT,
modifiers: {
arrow: {
enabled: false,
},
offset: {
enabled: true,
offset: "-16px, 0",
},
2020-01-24 09:54:40 +00:00
},
}}
shouldDismissPopover
text={option.label || option.value}
>
{option.children && option.children.map(DropdownItem)}
</StyledMenuItem>
);
}
2020-01-24 09:54:40 +00:00
export function ContextDropdown(props: ContextDropdownProps) {
2019-11-21 10:52:49 +00:00
let trigger: ReactNode;
if (props.toggle.type === "icon" && props.toggle.icon) {
const TriggerElement = ControlIcons[props.toggle.icon];
const TriggerElementProps: IconProps = {
width: props.toggle.iconSize,
height: props.toggle.iconSize,
color: props.toggle.color || Colors.SLATE_GRAY,
};
trigger = <TriggerElement {...TriggerElementProps} />;
}
2019-11-21 10:52:49 +00:00
if (props.toggle.type === "button" && props.toggle.text)
trigger = <Button text={props.toggle.text} />;
const renderer: ItemRenderer<ContextDropdownOption> = (
option: ContextDropdownOption,
2020-02-14 07:48:33 +00:00
) => <DropdownItem key={option.value} {...option} />;
2020-01-27 08:24:58 +00:00
2019-11-21 10:52:49 +00:00
return (
<Dropdown
className={props.className}
filterable={false}
2019-11-21 10:52:49 +00:00
itemRenderer={renderer}
items={props.options}
2019-11-21 10:52:49 +00:00
onItemSelect={noop}
>
{trigger}
</Dropdown>
);
}
2019-11-21 10:52:49 +00:00
export default ContextDropdown;