PromucFlow_constructor/app/client/src/components/propertyControls/DropDownControl.tsx

79 lines
2.2 KiB
TypeScript
Raw Normal View History

2019-11-05 05:09:50 +00:00
import React from "react";
import BaseControl, { ControlProps } from "./BaseControl";
import { Button, MenuItem } from "@blueprintjs/core";
import { IItemRendererProps } from "@blueprintjs/select";
2020-02-18 10:41:52 +00:00
import {
ControlWrapper,
StyledDropDown,
StyledDropDownContainer,
} from "./StyledControls";
2019-11-25 05:07:27 +00:00
import { DropdownOption } from "widgets/DropdownWidget";
import { ControlType } from "constants/PropertyControlConstants";
class DropDownControl extends BaseControl<DropDownControlProps> {
render() {
const selected: DropdownOption | undefined = this.props.options.find(
option => option.value === this.props.propertyValue,
);
return (
<ControlWrapper>
<label>{this.props.label}</label>
2020-02-18 10:41:52 +00:00
<StyledDropDownContainer>
<StyledDropDown
items={this.props.options}
filterable={false}
itemRenderer={this.renderItem}
onItemSelect={this.onItemSelect}
noResults={<MenuItem disabled={true} text="No results." />}
popoverProps={{
minimal: true,
usePortal: false,
}}
>
<Button
text={selected ? selected.label : ""}
rightIcon="chevron-down"
/>
</StyledDropDown>
</StyledDropDownContainer>
</ControlWrapper>
);
}
2019-11-05 05:09:50 +00:00
onItemSelect = (item: DropdownOption): void => {
this.updateProperty(this.props.propertyName, item.value);
2019-10-31 05:28:11 +00:00
};
2019-10-31 05:28:11 +00:00
renderItem = (option: DropdownOption, itemProps: IItemRendererProps) => {
if (!itemProps.modifiers.matchesPredicate) {
return null;
}
2019-11-05 05:09:50 +00:00
const isSelected: boolean = this.isOptionSelected(option);
return (
<MenuItem
2020-02-18 10:41:52 +00:00
className="single-select"
active={isSelected}
key={option.value}
onClick={itemProps.handleClick}
text={option.label}
/>
);
2019-10-31 05:28:11 +00:00
};
2019-11-05 05:09:50 +00:00
isOptionSelected = (selectedOption: DropdownOption) => {
return selectedOption.value === this.props.propertyValue;
};
getControlType(): ControlType {
return "DROP_DOWN";
}
}
export interface DropDownControlProps extends ControlProps {
options: DropdownOption[];
2019-11-05 05:09:50 +00:00
placeholderText: string;
propertyValue: string;
}
export default DropDownControl;