2020-10-16 13:16:01 +00:00
|
|
|
import PropTypes from "prop-types";
|
|
|
|
import React from "react";
|
2020-12-08 11:09:46 +00:00
|
|
|
import equal from "fast-deep-equal/react";
|
2019-08-08 11:14:25 +00:00
|
|
|
|
2021-02-04 10:28:48 +00:00
|
|
|
import ComboButton from "./sub-components/combo-button";
|
|
|
|
import DropDown from "../drop-down";
|
|
|
|
import DropDownItem from "../drop-down-item";
|
2021-02-25 21:19:45 +00:00
|
|
|
import StyledComboBox from "./styled-combobox";
|
2019-07-18 08:00:54 +00:00
|
|
|
|
2019-09-12 12:21:58 +00:00
|
|
|
class ComboBox extends React.Component {
|
2019-07-30 13:49:33 +00:00
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
2019-07-18 08:00:54 +00:00
|
|
|
|
2019-07-30 13:49:33 +00:00
|
|
|
this.ref = React.createRef();
|
2019-07-26 09:02:08 +00:00
|
|
|
|
2019-07-30 13:49:33 +00:00
|
|
|
this.state = {
|
|
|
|
isOpen: props.opened,
|
2020-10-16 13:16:01 +00:00
|
|
|
selectedOption: props.selectedOption,
|
2019-07-30 13:49:33 +00:00
|
|
|
};
|
|
|
|
}
|
2019-07-18 08:00:54 +00:00
|
|
|
|
2020-01-09 13:27:49 +00:00
|
|
|
shouldComponentUpdate(nextProps, nextState) {
|
2020-10-16 13:16:01 +00:00
|
|
|
const needUpdate =
|
2020-12-08 11:09:46 +00:00
|
|
|
!equal(this.props, nextProps) || !equal(this.state, nextState);
|
2020-01-09 13:27:49 +00:00
|
|
|
|
|
|
|
return needUpdate;
|
2019-09-19 11:59:31 +00:00
|
|
|
}
|
2019-08-05 12:45:12 +00:00
|
|
|
|
2019-07-30 13:49:33 +00:00
|
|
|
stopAction = (e) => e.preventDefault();
|
2019-08-05 12:45:12 +00:00
|
|
|
|
2020-01-09 13:27:49 +00:00
|
|
|
setIsOpen = (isOpen) => this.setState({ isOpen: isOpen });
|
|
|
|
|
2020-10-16 13:16:01 +00:00
|
|
|
handleClickOutside = (e) => {
|
2020-12-10 17:06:04 +00:00
|
|
|
if (this.ref.current.contains(e.target)) return;
|
2020-01-20 13:20:01 +00:00
|
|
|
|
2020-01-10 10:47:06 +00:00
|
|
|
this.setState({ isOpen: !this.state.isOpen }, () => {
|
|
|
|
this.props.toggleAction && this.props.toggleAction(e, this.state.isOpen);
|
2020-10-16 13:16:01 +00:00
|
|
|
});
|
2020-01-09 13:27:49 +00:00
|
|
|
};
|
2019-08-05 12:45:12 +00:00
|
|
|
|
|
|
|
comboBoxClick = (e) => {
|
2022-02-18 13:52:15 +00:00
|
|
|
const {
|
|
|
|
disableIconClick,
|
|
|
|
disableItemClick,
|
|
|
|
isDisabled,
|
|
|
|
toggleAction,
|
|
|
|
} = this.props;
|
2020-11-24 14:52:23 +00:00
|
|
|
|
|
|
|
if (
|
|
|
|
isDisabled ||
|
2022-02-18 13:52:15 +00:00
|
|
|
disableItemClick ||
|
2020-11-24 14:52:23 +00:00
|
|
|
(disableIconClick && e && e.target.closest(".optionalBlock"))
|
|
|
|
)
|
2020-10-16 13:16:01 +00:00
|
|
|
return;
|
2020-01-20 13:20:01 +00:00
|
|
|
|
2020-01-10 10:47:06 +00:00
|
|
|
this.setState({ isOpen: !this.state.isOpen }, () => {
|
2020-11-24 14:52:23 +00:00
|
|
|
toggleAction && toggleAction(e, this.state.isOpen);
|
2020-10-16 13:16:01 +00:00
|
|
|
});
|
2019-07-30 13:49:33 +00:00
|
|
|
};
|
2019-08-05 12:45:12 +00:00
|
|
|
|
2019-07-30 13:49:33 +00:00
|
|
|
optionClick = (option) => {
|
2019-08-16 06:54:23 +00:00
|
|
|
this.setState({
|
2019-08-14 12:53:05 +00:00
|
|
|
isOpen: !this.state.isOpen,
|
2020-10-16 13:16:01 +00:00
|
|
|
selectedOption: option,
|
2019-07-31 18:42:23 +00:00
|
|
|
});
|
2019-09-18 11:45:29 +00:00
|
|
|
|
2019-07-30 13:49:33 +00:00
|
|
|
this.props.onSelect && this.props.onSelect(option);
|
|
|
|
};
|
2019-07-18 08:00:54 +00:00
|
|
|
|
2020-01-23 06:29:23 +00:00
|
|
|
componentDidUpdate(prevProps) {
|
|
|
|
if (this.props.opened !== prevProps.opened) {
|
|
|
|
this.setIsOpen(this.props.opened);
|
2019-08-02 07:35:54 +00:00
|
|
|
}
|
2019-08-05 15:12:25 +00:00
|
|
|
|
2019-08-06 07:39:33 +00:00
|
|
|
if (this.props.selectedOption !== prevProps.selectedOption) {
|
2019-08-16 06:54:23 +00:00
|
|
|
this.setState({ selectedOption: this.props.selectedOption });
|
2019-08-05 15:12:25 +00:00
|
|
|
}
|
2019-09-09 13:32:04 +00:00
|
|
|
}
|
2019-07-18 08:00:54 +00:00
|
|
|
|
2019-07-30 13:49:33 +00:00
|
|
|
render() {
|
2019-08-28 12:53:38 +00:00
|
|
|
//console.log("ComboBox render");
|
2019-09-02 11:15:24 +00:00
|
|
|
const {
|
2019-09-18 11:45:29 +00:00
|
|
|
dropDownMaxHeight,
|
|
|
|
directionX,
|
|
|
|
directionY,
|
|
|
|
scaled,
|
|
|
|
size,
|
|
|
|
options,
|
|
|
|
advancedOptions,
|
|
|
|
isDisabled,
|
|
|
|
children,
|
2019-09-18 13:24:14 +00:00
|
|
|
noBorder,
|
2019-12-11 12:22:07 +00:00
|
|
|
scaledOptions,
|
|
|
|
displayType,
|
2020-09-15 06:02:24 +00:00
|
|
|
toggleAction,
|
2020-10-16 13:16:01 +00:00
|
|
|
textOverflow,
|
2020-12-09 10:06:32 +00:00
|
|
|
showDisabledItems,
|
2021-10-13 14:44:40 +00:00
|
|
|
comboIcon,
|
2021-10-20 11:27:21 +00:00
|
|
|
manualY,
|
|
|
|
manualX,
|
2021-10-07 14:05:41 +00:00
|
|
|
isDefaultMode,
|
2022-02-17 11:37:32 +00:00
|
|
|
manualWidth,
|
2022-02-25 09:18:10 +00:00
|
|
|
displaySelectedOption,
|
2022-02-25 13:07:39 +00:00
|
|
|
fixedDirection,
|
2022-05-20 12:21:13 +00:00
|
|
|
withBlur,
|
2020-10-16 13:16:01 +00:00
|
|
|
} = this.props;
|
2019-08-14 12:53:05 +00:00
|
|
|
const { isOpen, selectedOption } = this.state;
|
2019-08-12 08:19:52 +00:00
|
|
|
|
2019-09-18 11:45:29 +00:00
|
|
|
const dropDownMaxHeightProp = dropDownMaxHeight
|
|
|
|
? { maxHeight: dropDownMaxHeight }
|
|
|
|
: {};
|
2021-09-13 09:56:02 +00:00
|
|
|
|
2021-10-12 15:22:31 +00:00
|
|
|
const dropDownManualWidthProp =
|
|
|
|
scaledOptions && !isDefaultMode
|
|
|
|
? { manualWidth: "100%" }
|
|
|
|
: scaledOptions && this.ref.current
|
|
|
|
? { manualWidth: this.ref.current.clientWidth + "px" }
|
2022-02-17 11:37:32 +00:00
|
|
|
: { manualWidth: manualWidth };
|
2019-09-18 11:45:29 +00:00
|
|
|
|
|
|
|
const optionsLength = options.length
|
|
|
|
? options.length
|
2020-10-16 13:16:01 +00:00
|
|
|
: displayType !== "toggle"
|
|
|
|
? 0
|
|
|
|
: 1;
|
2019-09-18 11:45:29 +00:00
|
|
|
|
2022-05-16 12:37:08 +00:00
|
|
|
const withAdvancedOptions = !!advancedOptions?.props.children;
|
2019-09-18 11:45:29 +00:00
|
|
|
|
2019-07-30 13:49:33 +00:00
|
|
|
return (
|
2019-09-18 11:45:29 +00:00
|
|
|
<StyledComboBox
|
|
|
|
ref={this.ref}
|
|
|
|
isDisabled={isDisabled}
|
|
|
|
scaled={scaled}
|
|
|
|
size={size}
|
2019-08-14 12:53:05 +00:00
|
|
|
data={selectedOption}
|
2019-08-08 11:14:25 +00:00
|
|
|
onClick={this.comboBoxClick}
|
2019-12-11 12:22:07 +00:00
|
|
|
toggleAction={toggleAction}
|
2019-09-18 11:45:29 +00:00
|
|
|
{...this.props}
|
2019-08-08 11:14:25 +00:00
|
|
|
>
|
2019-09-18 11:45:29 +00:00
|
|
|
<ComboButton
|
|
|
|
noBorder={noBorder}
|
|
|
|
isDisabled={isDisabled}
|
|
|
|
selectedOption={selectedOption}
|
|
|
|
withOptions={optionsLength > 0}
|
|
|
|
optionsLength={optionsLength}
|
2022-05-16 12:37:08 +00:00
|
|
|
withAdvancedOptions={withAdvancedOptions}
|
2019-09-18 11:45:29 +00:00
|
|
|
innerContainer={children}
|
2020-10-16 13:16:01 +00:00
|
|
|
innerContainerClassName="optionalBlock"
|
2019-09-18 11:45:29 +00:00
|
|
|
isOpen={isOpen}
|
|
|
|
size={size}
|
|
|
|
scaled={scaled}
|
2021-10-13 14:44:40 +00:00
|
|
|
comboIcon={comboIcon}
|
2019-09-18 11:45:29 +00:00
|
|
|
/>
|
2020-10-16 13:16:01 +00:00
|
|
|
{displayType !== "toggle" && (
|
2019-12-11 12:22:07 +00:00
|
|
|
<DropDown
|
2021-05-27 11:50:23 +00:00
|
|
|
className="dropdown-container not-selectable"
|
2019-12-11 12:22:07 +00:00
|
|
|
directionX={directionX}
|
|
|
|
directionY={directionY}
|
2021-10-20 11:27:21 +00:00
|
|
|
manualY={manualY}
|
|
|
|
manualX={manualX}
|
2019-12-17 14:27:06 +00:00
|
|
|
open={isOpen}
|
2021-09-13 09:56:02 +00:00
|
|
|
forwardedRef={this.ref}
|
2020-01-09 13:27:49 +00:00
|
|
|
clickOutsideAction={this.handleClickOutside}
|
2021-10-14 10:57:51 +00:00
|
|
|
style={advancedOptions && { padding: "6px 0px" }}
|
2019-12-11 12:22:07 +00:00
|
|
|
{...dropDownMaxHeightProp}
|
|
|
|
{...dropDownManualWidthProp}
|
2020-12-09 10:06:32 +00:00
|
|
|
showDisabledItems={showDisabledItems}
|
2021-10-07 14:05:41 +00:00
|
|
|
isDefaultMode={isDefaultMode}
|
2022-02-25 13:07:39 +00:00
|
|
|
fixedDirection={fixedDirection}
|
2022-05-20 12:21:13 +00:00
|
|
|
withBlur={withBlur}
|
2019-12-11 12:22:07 +00:00
|
|
|
>
|
|
|
|
{advancedOptions
|
|
|
|
? advancedOptions
|
2022-02-25 09:18:10 +00:00
|
|
|
: options.map((option) => {
|
|
|
|
const disabled =
|
|
|
|
option.disabled ||
|
|
|
|
(!displaySelectedOption &&
|
|
|
|
option.label === selectedOption.label);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<DropDownItem
|
|
|
|
{...option}
|
|
|
|
textOverflow={textOverflow}
|
|
|
|
key={option.key}
|
|
|
|
disabled={disabled}
|
|
|
|
onClick={this.optionClick.bind(this, option)}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
})}
|
2019-12-11 12:22:07 +00:00
|
|
|
</DropDown>
|
2020-10-16 13:16:01 +00:00
|
|
|
)}
|
2019-07-30 13:49:33 +00:00
|
|
|
</StyledComboBox>
|
|
|
|
);
|
|
|
|
}
|
2019-09-09 13:32:04 +00:00
|
|
|
}
|
2019-07-18 08:00:54 +00:00
|
|
|
|
|
|
|
ComboBox.propTypes = {
|
2021-03-07 11:57:26 +00:00
|
|
|
/** If you need display options not basic options */
|
2019-08-30 13:44:04 +00:00
|
|
|
advancedOptions: PropTypes.element,
|
2021-03-07 11:57:26 +00:00
|
|
|
/** Children elements */
|
2019-09-09 13:32:04 +00:00
|
|
|
children: PropTypes.any,
|
2021-03-07 11:57:26 +00:00
|
|
|
/** Accepts class */
|
2019-12-11 12:22:07 +00:00
|
|
|
className: PropTypes.string,
|
2021-03-07 11:57:26 +00:00
|
|
|
/** X direction selection */
|
2020-10-16 13:16:01 +00:00
|
|
|
directionX: PropTypes.oneOf(["left", "right"]),
|
2021-03-07 11:57:26 +00:00
|
|
|
/** Y direction selection */
|
2021-09-13 09:56:02 +00:00
|
|
|
directionY: PropTypes.oneOf(["bottom", "top", "both"]),
|
2021-03-07 11:57:26 +00:00
|
|
|
/** Component Display Type */
|
2020-10-16 13:16:01 +00:00
|
|
|
displayType: PropTypes.oneOf(["default", "toggle"]),
|
2021-03-07 11:57:26 +00:00
|
|
|
/** Height of Dropdown */
|
2019-12-11 12:22:07 +00:00
|
|
|
dropDownMaxHeight: PropTypes.number,
|
2021-03-07 11:57:26 +00:00
|
|
|
/** Display disabled items or not when displayType !== toggle */
|
|
|
|
showDisabledItems: PropTypes.bool,
|
|
|
|
/** Accepts id */
|
2019-12-11 12:22:07 +00:00
|
|
|
id: PropTypes.string,
|
2021-03-07 11:57:26 +00:00
|
|
|
/** Indicates that component is disabled */
|
2019-12-11 12:22:07 +00:00
|
|
|
isDisabled: PropTypes.bool,
|
2021-03-07 11:57:26 +00:00
|
|
|
/** Indicates that component is displayed without borders */
|
2019-12-11 12:22:07 +00:00
|
|
|
noBorder: PropTypes.bool,
|
2021-03-07 11:57:26 +00:00
|
|
|
/** Will be triggered whenever an ComboBox is selected option */
|
2019-12-11 12:22:07 +00:00
|
|
|
onSelect: PropTypes.func,
|
2021-03-07 11:57:26 +00:00
|
|
|
/** Tells when a component is open */
|
2019-12-11 12:22:07 +00:00
|
|
|
opened: PropTypes.bool,
|
2021-03-07 11:57:26 +00:00
|
|
|
/** Combo box options */
|
2019-12-11 12:22:07 +00:00
|
|
|
options: PropTypes.array.isRequired,
|
2021-03-07 11:57:26 +00:00
|
|
|
/** Indicates that component is scaled by parent */
|
2019-09-18 13:24:14 +00:00
|
|
|
scaled: PropTypes.bool,
|
2021-03-07 11:57:26 +00:00
|
|
|
/** Indicates that component`s options is scaled by ComboButton */
|
2019-11-27 11:47:21 +00:00
|
|
|
scaledOptions: PropTypes.bool,
|
2021-03-07 11:57:26 +00:00
|
|
|
/** Selected option */
|
2019-12-11 12:22:07 +00:00
|
|
|
selectedOption: PropTypes.object.isRequired,
|
2021-03-07 11:57:26 +00:00
|
|
|
/** Select component width, one of default */
|
2020-10-16 13:16:01 +00:00
|
|
|
size: PropTypes.oneOf(["base", "middle", "big", "huge", "content"]),
|
2021-03-07 11:57:26 +00:00
|
|
|
/** Accepts css style */
|
2019-12-11 12:22:07 +00:00
|
|
|
style: PropTypes.oneOfType([PropTypes.object, PropTypes.array]),
|
2021-03-07 11:57:26 +00:00
|
|
|
/** The event will be raised when using `displayType: toggle` when clicking on a component */
|
2020-09-15 06:02:24 +00:00
|
|
|
toggleAction: PropTypes.func,
|
2021-03-07 11:57:26 +00:00
|
|
|
/** Accepts css text-overflow */
|
2020-10-16 13:16:01 +00:00
|
|
|
textOverflow: PropTypes.bool,
|
2021-03-07 11:57:26 +00:00
|
|
|
/** Disables clicking on the icon */
|
2020-11-24 14:52:23 +00:00
|
|
|
disableIconClick: PropTypes.bool,
|
2021-10-07 14:05:41 +00:00
|
|
|
/** Defines the operation mode of the component, by default with the portal */
|
|
|
|
isDefaultMode: PropTypes.bool,
|
2021-10-26 13:04:38 +00:00
|
|
|
/** Y offset */
|
|
|
|
offsetDropDownY: PropTypes.string,
|
2021-10-13 14:44:40 +00:00
|
|
|
comboIcon: PropTypes.string,
|
2021-10-20 11:27:21 +00:00
|
|
|
manualY: PropTypes.string,
|
|
|
|
manualX: PropTypes.string,
|
2022-02-17 11:37:32 +00:00
|
|
|
//** Dropdown manual width */
|
|
|
|
manualWidth: PropTypes.string,
|
2022-02-25 09:18:10 +00:00
|
|
|
displaySelectedOption: PropTypes.bool,
|
2022-02-25 13:07:39 +00:00
|
|
|
fixedDirection: PropTypes.bool,
|
2022-02-18 13:52:15 +00:00
|
|
|
/** Disable clicking on the item */
|
|
|
|
disableItemClick: PropTypes.bool,
|
2020-10-16 13:16:01 +00:00
|
|
|
};
|
2019-07-18 08:00:54 +00:00
|
|
|
|
|
|
|
ComboBox.defaultProps = {
|
2020-10-16 13:16:01 +00:00
|
|
|
displayType: "default",
|
2019-07-30 13:49:33 +00:00
|
|
|
isDisabled: false,
|
2019-12-11 12:22:07 +00:00
|
|
|
noBorder: false,
|
2019-09-18 13:24:14 +00:00
|
|
|
scaled: true,
|
2019-12-11 12:22:07 +00:00
|
|
|
scaledOptions: false,
|
2020-10-16 13:16:01 +00:00
|
|
|
size: "base",
|
2020-11-24 14:52:23 +00:00
|
|
|
disableIconClick: true,
|
2020-12-09 10:06:32 +00:00
|
|
|
showDisabledItems: false,
|
2021-10-20 11:27:21 +00:00
|
|
|
manualY: "102%",
|
2021-10-07 14:05:41 +00:00
|
|
|
isDefaultMode: true,
|
2022-02-17 11:37:32 +00:00
|
|
|
manualWidth: "200px",
|
2022-02-25 09:18:10 +00:00
|
|
|
displaySelectedOption: false,
|
2022-02-25 13:07:39 +00:00
|
|
|
fixedDirection: false,
|
2022-02-18 13:52:15 +00:00
|
|
|
disableItemClick: false,
|
2020-10-16 13:16:01 +00:00
|
|
|
};
|
2019-07-26 09:02:08 +00:00
|
|
|
|
2020-10-16 13:16:01 +00:00
|
|
|
export default ComboBox;
|