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 { DropdownOption } from "widgets/DropdownWidget";
|
|
|
|
|
import { ControlIconName, ControlIcons } from "icons/ControlIcons";
|
|
|
|
|
import { noop } from "utils/AppsmithUtils";
|
|
|
|
|
import { Intent } from "constants/DefaultTheme";
|
|
|
|
|
|
|
|
|
|
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-08-10 04:54:33 +00:00
|
|
|
background: ${props => props.theme.colors.primaryOld};
|
2019-11-21 10:52:49 +00:00
|
|
|
color: ${props => props.theme.colors.textOnDarkBG};
|
|
|
|
|
}
|
|
|
|
|
&&&.bp3-menu-item.bp3-intent-danger:hover {
|
|
|
|
|
background: ${props => props.theme.colors.error};
|
|
|
|
|
}
|
|
|
|
|
`;
|
|
|
|
|
|
|
|
|
|
type ContextDropdownProps = {
|
|
|
|
|
options: ContextDropdownOption[];
|
|
|
|
|
className: string;
|
|
|
|
|
toggle: {
|
|
|
|
|
type: "icon" | "button";
|
|
|
|
|
icon?: ControlIconName;
|
2019-11-22 14:02:55 +00:00
|
|
|
iconSize?: number;
|
2019-11-21 10:52:49 +00:00
|
|
|
text?: string;
|
|
|
|
|
placeholder?: string;
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
2020-01-24 09:54:40 +00:00
|
|
|
const DropdownItem = (option: ContextDropdownOption) => (
|
|
|
|
|
<StyledMenuItem
|
|
|
|
|
key={option.value}
|
|
|
|
|
onClick={option.onSelect}
|
|
|
|
|
shouldDismissPopover={true}
|
|
|
|
|
text={option.label || option.value}
|
|
|
|
|
intent={option.intent as BlueprintIntent}
|
|
|
|
|
popoverProps={{
|
|
|
|
|
minimal: true,
|
|
|
|
|
hoverCloseDelay: 0,
|
2020-01-27 13:53:33 +00:00
|
|
|
hoverOpenDelay: 300,
|
|
|
|
|
interactionKind: PopoverInteractionKind.CLICK,
|
|
|
|
|
position: PopoverPosition.RIGHT,
|
2020-01-24 09:54:40 +00:00
|
|
|
modifiers: {
|
|
|
|
|
arrow: {
|
|
|
|
|
enabled: false,
|
|
|
|
|
},
|
|
|
|
|
offset: {
|
|
|
|
|
enabled: true,
|
|
|
|
|
offset: "-16px, 0",
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
{option.children && option.children.map(DropdownItem)}
|
|
|
|
|
</StyledMenuItem>
|
|
|
|
|
);
|
|
|
|
|
|
2019-11-21 10:52:49 +00:00
|
|
|
export const ContextDropdown = (props: ContextDropdownProps) => {
|
|
|
|
|
let trigger: ReactNode;
|
2020-03-27 09:02:11 +00:00
|
|
|
if (props.toggle.type === "icon" && props.toggle.icon) {
|
|
|
|
|
const TriggerElement = ControlIcons[props.toggle.icon];
|
|
|
|
|
const TriggerElementProps = {
|
2020-01-24 09:54:40 +00:00
|
|
|
style: { display: "flex" },
|
2019-11-22 14:02:55 +00:00
|
|
|
width: props.toggle.iconSize,
|
|
|
|
|
height: props.toggle.iconSize,
|
2020-03-27 09:02:11 +00:00
|
|
|
};
|
|
|
|
|
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
|
|
|
|
|
items={props.options}
|
|
|
|
|
itemRenderer={renderer}
|
|
|
|
|
onItemSelect={noop}
|
|
|
|
|
filterable={false}
|
|
|
|
|
className={props.className}
|
|
|
|
|
>
|
|
|
|
|
{trigger}
|
|
|
|
|
</Dropdown>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default ContextDropdown;
|