2019-11-21 10:52:49 +00:00
|
|
|
import React, { Component, ReactNode } from "react";
|
2019-10-31 08:36:04 +00:00
|
|
|
import styled from "styled-components";
|
2021-08-16 12:00:11 +00:00
|
|
|
import {
|
|
|
|
|
MenuItem,
|
|
|
|
|
Menu,
|
|
|
|
|
ControlGroup,
|
|
|
|
|
InputGroup,
|
|
|
|
|
IMenuProps,
|
|
|
|
|
} from "@blueprintjs/core";
|
2021-09-09 15:10:22 +00:00
|
|
|
import { BaseButton } from "components/designSystems/appsmith/BaseButton";
|
2019-10-31 08:36:04 +00:00
|
|
|
import {
|
|
|
|
|
ItemRenderer,
|
|
|
|
|
Select,
|
|
|
|
|
ItemListRenderer,
|
|
|
|
|
IItemListRendererProps,
|
|
|
|
|
} from "@blueprintjs/select";
|
2021-10-12 08:04:51 +00:00
|
|
|
import { ButtonVariantTypes, DropdownOption } from "components/constants";
|
2021-08-16 12:00:11 +00:00
|
|
|
import { WrappedFieldInputProps } from "redux-form";
|
|
|
|
|
|
|
|
|
|
interface ButtonWrapperProps {
|
2021-10-04 15:34:37 +00:00
|
|
|
height?: string;
|
2021-08-16 12:00:11 +00:00
|
|
|
width?: string;
|
|
|
|
|
}
|
|
|
|
|
interface MenuProps {
|
|
|
|
|
width?: string;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type MenuComponentProps = IMenuProps & MenuProps;
|
2019-10-31 08:36:04 +00:00
|
|
|
|
|
|
|
|
const Dropdown = Select.ofType<DropdownOption>();
|
|
|
|
|
const StyledDropdown = styled(Dropdown)``;
|
|
|
|
|
|
2021-08-16 12:00:11 +00:00
|
|
|
const StyledButtonWrapper = styled.div<ButtonWrapperProps>`
|
|
|
|
|
width: ${(props) => props.width || "100%"};
|
2021-10-04 15:34:37 +00:00
|
|
|
height: ${(props) => props.height || "100%"};
|
|
|
|
|
button.bp3-button {
|
|
|
|
|
border: 1px solid ${(props) => props.theme.colors.border} !important;
|
|
|
|
|
background: #fff !important;
|
|
|
|
|
& > span {
|
|
|
|
|
color: ${(props) => props.theme.colors.dropdown.header.text} !important;
|
|
|
|
|
}
|
2021-10-06 19:20:35 +00:00
|
|
|
font-weight: ${(props) => props.theme.fontWeights[1]};
|
2021-10-04 15:34:37 +00:00
|
|
|
}
|
2021-08-16 12:00:11 +00:00
|
|
|
`;
|
|
|
|
|
const StyledMenu = styled(Menu)<MenuComponentProps>`
|
|
|
|
|
min-width: ${(props) => props.width || "100%"};
|
|
|
|
|
border-radius: 0;
|
|
|
|
|
`;
|
|
|
|
|
const StyledMenuItem = styled(MenuItem)`
|
|
|
|
|
border-radius: 0;
|
2021-10-04 15:34:37 +00:00
|
|
|
color: ${(props) => props.theme.colors.dropdown.header.text};
|
|
|
|
|
&&&:hover {
|
|
|
|
|
color: ${(props) => props.theme.colors.dropdown.menu.hoverText};
|
|
|
|
|
background: ${(props) => props.theme.colors.dropdown.menu.hover};
|
|
|
|
|
}
|
2021-08-16 12:00:11 +00:00
|
|
|
&&&.bp3-active {
|
2021-10-04 15:34:37 +00:00
|
|
|
color: ${(props) => props.theme.colors.dropdown.selected.text};
|
|
|
|
|
background: ${(props) => props.theme.colors.dropdown.selected.bg};
|
2021-08-16 12:00:11 +00:00
|
|
|
}
|
|
|
|
|
`;
|
|
|
|
|
|
2021-10-06 19:20:35 +00:00
|
|
|
// function checks if dropdown is connected to a redux form (of interface 'FormDropdownComponentProps')
|
|
|
|
|
const isFormDropdown = (
|
|
|
|
|
props: DropdownComponentProps | FormDropdownComponentProps,
|
|
|
|
|
): props is FormDropdownComponentProps => {
|
|
|
|
|
return "input" in props && props.input !== undefined;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class DropdownComponent extends Component<
|
|
|
|
|
DropdownComponentProps | FormDropdownComponentProps
|
|
|
|
|
> {
|
2019-10-31 08:36:04 +00:00
|
|
|
private newItemTextInput: HTMLInputElement | null = null;
|
|
|
|
|
private setNewItemTextInput = (element: HTMLInputElement | null) => {
|
|
|
|
|
this.newItemTextInput = element;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
public state = {
|
|
|
|
|
isEditing: false,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
showTextBox = (): void => {
|
|
|
|
|
this.setState({
|
|
|
|
|
isEditing: true,
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
handleAddItem = (): void => {
|
|
|
|
|
this.props.addItem &&
|
|
|
|
|
this.newItemTextInput &&
|
|
|
|
|
this.props.addItem.addItemHandler(this.newItemTextInput.value);
|
|
|
|
|
this.setState({
|
|
|
|
|
isEditing: false,
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
renderItemList: ItemListRenderer<DropdownOption> = (
|
|
|
|
|
props: IItemListRendererProps<DropdownOption>,
|
|
|
|
|
) => {
|
2021-08-16 12:00:11 +00:00
|
|
|
const { items, renderItem } = props;
|
|
|
|
|
const { addItem, width } = this.props;
|
|
|
|
|
const renderItems = items.map(renderItem).filter(Boolean);
|
|
|
|
|
|
|
|
|
|
const displayMode = (
|
|
|
|
|
<BaseButton
|
2021-08-24 13:53:15 +00:00
|
|
|
buttonStyle="PRIMARY"
|
2021-08-16 12:00:11 +00:00
|
|
|
icon-right="plus"
|
|
|
|
|
onClick={this.showTextBox}
|
|
|
|
|
text={addItem?.displayText}
|
|
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
const editMode = (
|
|
|
|
|
<ControlGroup fill>
|
|
|
|
|
<InputGroup inputRef={this.setNewItemTextInput} />
|
2021-08-24 13:53:15 +00:00
|
|
|
<BaseButton onClick={this.handleAddItem} text={addItem?.displayText} />
|
2021-08-16 12:00:11 +00:00
|
|
|
</ControlGroup>
|
|
|
|
|
);
|
|
|
|
|
return (
|
|
|
|
|
<StyledMenu ulRef={props.itemsParentRef} width={width}>
|
|
|
|
|
{renderItems}
|
|
|
|
|
{addItem && (!this.state.isEditing ? displayMode : editMode)}
|
|
|
|
|
</StyledMenu>
|
|
|
|
|
);
|
2019-10-31 08:36:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
searchItem = (query: string, option: DropdownOption): boolean => {
|
|
|
|
|
return (
|
|
|
|
|
option.label.toLowerCase().indexOf(query.toLowerCase()) > -1 ||
|
|
|
|
|
option.value.toLowerCase().indexOf(query.toLowerCase()) > -1 ||
|
2019-11-05 05:09:50 +00:00
|
|
|
(!!option.label &&
|
|
|
|
|
option.label.toLowerCase().indexOf(query.toLowerCase()) > -1)
|
2019-10-31 08:36:04 +00:00
|
|
|
);
|
|
|
|
|
};
|
2021-10-06 19:20:35 +00:00
|
|
|
// function is called after user selects an option
|
2019-10-31 08:36:04 +00:00
|
|
|
onItemSelect = (item: DropdownOption): void => {
|
2021-10-06 19:20:35 +00:00
|
|
|
if (isFormDropdown(this.props)) {
|
|
|
|
|
this.props.input.onChange(item.value);
|
|
|
|
|
} else {
|
|
|
|
|
this.props.selectHandler(item.value);
|
|
|
|
|
}
|
2019-10-31 08:36:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
renderItem: ItemRenderer<DropdownOption> = (
|
|
|
|
|
option: DropdownOption,
|
|
|
|
|
{ handleClick, modifiers },
|
|
|
|
|
) => {
|
|
|
|
|
if (!modifiers.matchesPredicate) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
return (
|
2021-08-16 12:00:11 +00:00
|
|
|
<StyledMenuItem
|
2019-10-31 08:36:04 +00:00
|
|
|
active={modifiers.active}
|
|
|
|
|
key={option.value}
|
2021-08-16 12:00:11 +00:00
|
|
|
label={this.props.hasLabel ? option.label : ""}
|
2019-10-31 08:36:04 +00:00
|
|
|
onClick={handleClick}
|
|
|
|
|
shouldDismissPopover={false}
|
2021-08-16 12:00:11 +00:00
|
|
|
text={option.label}
|
2019-10-31 08:36:04 +00:00
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
};
|
2021-08-25 10:48:17 +00:00
|
|
|
|
2021-10-06 19:20:35 +00:00
|
|
|
// helper function that returns a dropdown option given its value
|
|
|
|
|
// returns undefined if option isn't found
|
|
|
|
|
getDropdownOption = (
|
|
|
|
|
value: string | undefined,
|
|
|
|
|
): DropdownOption | undefined => {
|
2021-08-25 10:48:17 +00:00
|
|
|
return this.props.options.find((option) => option.value === value);
|
|
|
|
|
};
|
|
|
|
|
|
2021-10-06 19:20:35 +00:00
|
|
|
// this function returns the selected item's label
|
|
|
|
|
// returns the "placeholder" in the event that no option is selected.
|
2019-10-31 08:36:04 +00:00
|
|
|
getSelectedDisplayText = () => {
|
2021-10-06 19:20:35 +00:00
|
|
|
const value = isFormDropdown(this.props)
|
|
|
|
|
? this.props.input.value
|
|
|
|
|
: this.props.selected?.value;
|
2019-10-31 08:36:04 +00:00
|
|
|
|
2021-10-06 19:20:35 +00:00
|
|
|
const item = this.getDropdownOption(value);
|
|
|
|
|
return item ? item.label : this.props.placeholder;
|
2019-10-31 08:36:04 +00:00
|
|
|
};
|
2020-08-07 13:17:15 +00:00
|
|
|
|
2021-10-06 19:20:35 +00:00
|
|
|
// this function returns the active option
|
|
|
|
|
// returns undefined if no option is selected
|
|
|
|
|
getActiveOption = (): DropdownOption | undefined => {
|
|
|
|
|
if (isFormDropdown(this.props)) {
|
|
|
|
|
return this.getDropdownOption(this.props.input.value);
|
2021-08-25 10:48:17 +00:00
|
|
|
} else {
|
2021-10-06 19:20:35 +00:00
|
|
|
return this.props.selected;
|
2021-08-25 10:48:17 +00:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2019-10-31 08:36:04 +00:00
|
|
|
render() {
|
2021-10-06 19:20:35 +00:00
|
|
|
const { autocomplete, height, options, width } = this.props;
|
2021-08-16 12:00:11 +00:00
|
|
|
|
2019-10-31 08:36:04 +00:00
|
|
|
return (
|
|
|
|
|
<StyledDropdown
|
2021-08-25 10:48:17 +00:00
|
|
|
activeItem={this.getActiveOption()}
|
2021-08-16 12:00:11 +00:00
|
|
|
filterable={!!autocomplete}
|
|
|
|
|
itemListRenderer={this.renderItemList}
|
2019-10-31 08:36:04 +00:00
|
|
|
itemPredicate={this.searchItem}
|
2021-04-28 10:28:39 +00:00
|
|
|
itemRenderer={this.renderItem}
|
2021-08-16 12:00:11 +00:00
|
|
|
items={options}
|
2019-10-31 08:36:04 +00:00
|
|
|
itemsEqual="value"
|
2021-04-28 10:28:39 +00:00
|
|
|
noResults={<MenuItem disabled text="No results." />}
|
|
|
|
|
onItemSelect={this.onItemSelect}
|
2019-10-31 08:36:04 +00:00
|
|
|
popoverProps={{ minimal: true }}
|
2021-10-06 19:20:35 +00:00
|
|
|
// Destructure the "input" prop if dropdown is form-connected
|
|
|
|
|
{...(isFormDropdown(this.props) ? this.props.input : {})}
|
2019-10-31 08:36:04 +00:00
|
|
|
>
|
2019-11-21 10:52:49 +00:00
|
|
|
{this.props.toggle || (
|
2021-10-04 15:34:37 +00:00
|
|
|
<StyledButtonWrapper height={height} width={width}>
|
2021-08-16 12:00:11 +00:00
|
|
|
<BaseButton
|
2021-10-12 08:04:51 +00:00
|
|
|
buttonStyle="PRIMARY"
|
|
|
|
|
buttonVariant={ButtonVariantTypes.SECONDARY}
|
2021-08-16 12:00:11 +00:00
|
|
|
rightIcon="chevron-down"
|
|
|
|
|
text={this.getSelectedDisplayText()}
|
|
|
|
|
/>
|
|
|
|
|
</StyledButtonWrapper>
|
2019-11-21 10:52:49 +00:00
|
|
|
)}
|
2019-10-31 08:36:04 +00:00
|
|
|
</StyledDropdown>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-06 19:20:35 +00:00
|
|
|
// Dropdown can either be connected to a redux-form
|
|
|
|
|
// or be a stand-alone component
|
|
|
|
|
|
|
|
|
|
// Props common to both classes of dropdowns
|
|
|
|
|
export interface BaseDropdownComponentProps {
|
2019-10-31 08:36:04 +00:00
|
|
|
addItem?: {
|
|
|
|
|
displayText: string;
|
|
|
|
|
addItemHandler: (name: string) => void;
|
|
|
|
|
};
|
2021-10-06 19:20:35 +00:00
|
|
|
autocomplete?: boolean;
|
|
|
|
|
checked?: boolean;
|
|
|
|
|
hasLabel?: boolean;
|
2021-10-04 15:34:37 +00:00
|
|
|
height?: string;
|
2021-10-06 19:20:35 +00:00
|
|
|
multi?: boolean;
|
|
|
|
|
multiselectDisplayType?: "TAGS" | "CHECKBOXES";
|
|
|
|
|
options: DropdownOption[];
|
|
|
|
|
placeholder: string;
|
|
|
|
|
toggle?: ReactNode;
|
2021-08-16 12:00:11 +00:00
|
|
|
width?: string;
|
2019-10-31 08:36:04 +00:00
|
|
|
}
|
|
|
|
|
|
2021-10-06 19:20:35 +00:00
|
|
|
// stand-alone dropdown interface
|
|
|
|
|
export interface DropdownComponentProps extends BaseDropdownComponentProps {
|
|
|
|
|
selectHandler: (selectedValue: string) => void;
|
|
|
|
|
selected: DropdownOption | undefined;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Form-connected dropdown interface
|
|
|
|
|
export interface FormDropdownComponentProps extends BaseDropdownComponentProps {
|
|
|
|
|
input: WrappedFieldInputProps;
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-31 08:36:04 +00:00
|
|
|
export default DropdownComponent;
|