DocSpace-client/packages/asc-web-components/combobox/index.js

227 lines
6.3 KiB
JavaScript
Raw Normal View History

import PropTypes from "prop-types";
import React from "react";
import equal from "fast-deep-equal/react";
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
class ComboBox extends React.Component {
constructor(props) {
super(props);
2019-07-18 08:00:54 +00:00
this.ref = React.createRef();
this.state = {
isOpen: props.opened,
selectedOption: props.selectedOption,
};
}
2019-07-18 08:00:54 +00:00
2020-01-09 13:27:49 +00:00
shouldComponentUpdate(nextProps, nextState) {
const needUpdate =
!equal(this.props, nextProps) || !equal(this.state, nextState);
2020-01-09 13:27:49 +00:00
return needUpdate;
}
stopAction = (e) => e.preventDefault();
2020-01-09 13:27:49 +00:00
setIsOpen = (isOpen) => this.setState({ isOpen: isOpen });
handleClickOutside = (e) => {
if (this.ref.current.contains(e.target)) return;
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-01-09 13:27:49 +00:00
};
comboBoxClick = (e) => {
const { disableIconClick, isDisabled, toggleAction } = this.props;
if (
isDisabled ||
(disableIconClick && e && e.target.closest(".optionalBlock"))
)
return;
2020-01-10 10:47:06 +00:00
this.setState({ isOpen: !this.state.isOpen }, () => {
toggleAction && toggleAction(e, this.state.isOpen);
});
};
optionClick = (option) => {
this.setState({
isOpen: !this.state.isOpen,
selectedOption: option,
2019-07-31 18:42:23 +00:00
});
this.props.onSelect && this.props.onSelect(option);
};
2019-07-18 08:00:54 +00:00
componentDidUpdate(prevProps) {
if (this.props.opened !== prevProps.opened) {
this.setIsOpen(this.props.opened);
}
if (this.props.selectedOption !== prevProps.selectedOption) {
this.setState({ selectedOption: this.props.selectedOption });
}
}
2019-07-18 08:00:54 +00:00
render() {
//console.log("ComboBox render");
const {
dropDownMaxHeight,
directionX,
directionY,
scaled,
size,
options,
advancedOptions,
isDisabled,
children,
noBorder,
scaledOptions,
displayType,
toggleAction,
textOverflow,
showDisabledItems,
} = this.props;
const { isOpen, selectedOption } = this.state;
const dropDownMaxHeightProp = dropDownMaxHeight
? { maxHeight: dropDownMaxHeight }
: {};
const dropDownManualWidthProp = scaledOptions
? { manualWidth: "100%" }
: {};
const optionsLength = options.length
? options.length
: displayType !== "toggle"
? 0
: 1;
const advancedOptionsLength = advancedOptions
? advancedOptions.props.children.length
: 0;
return (
<StyledComboBox
ref={this.ref}
isDisabled={isDisabled}
scaled={scaled}
size={size}
data={selectedOption}
onClick={this.comboBoxClick}
toggleAction={toggleAction}
{...this.props}
>
<ComboButton
noBorder={noBorder}
isDisabled={isDisabled}
selectedOption={selectedOption}
withOptions={optionsLength > 0}
optionsLength={optionsLength}
withAdvancedOptions={advancedOptionsLength > 0}
innerContainer={children}
innerContainerClassName="optionalBlock"
isOpen={isOpen}
size={size}
scaled={scaled}
/>
{displayType !== "toggle" && (
<DropDown
className="dropdown-container"
directionX={directionX}
directionY={directionY}
manualY="102%"
open={isOpen}
2020-01-09 13:27:49 +00:00
clickOutsideAction={this.handleClickOutside}
{...dropDownMaxHeightProp}
{...dropDownManualWidthProp}
showDisabledItems={showDisabledItems}
>
{advancedOptions
? advancedOptions
: options.map((option) => (
<DropDownItem
{...option}
textOverflow={textOverflow}
key={option.key}
disabled={
option.disabled || option.label === selectedOption.label
}
onClick={this.optionClick.bind(this, option)}
/>
))}
</DropDown>
)}
</StyledComboBox>
);
}
}
2019-07-18 08:00:54 +00:00
ComboBox.propTypes = {
/** If you need display options not basic options */
advancedOptions: PropTypes.element,
/** Children elements */
children: PropTypes.any,
/** Accepts class */
className: PropTypes.string,
/** X direction selection */
directionX: PropTypes.oneOf(["left", "right"]),
/** Y direction selection */
directionY: PropTypes.oneOf(["bottom", "top"]),
/** Component Display Type */
displayType: PropTypes.oneOf(["default", "toggle"]),
/** Height of Dropdown */
dropDownMaxHeight: PropTypes.number,
/** Display disabled items or not when displayType !== toggle */
showDisabledItems: PropTypes.bool,
/** Accepts id */
id: PropTypes.string,
/** Indicates that component is disabled */
isDisabled: PropTypes.bool,
/** Indicates that component is displayed without borders */
noBorder: PropTypes.bool,
/** Will be triggered whenever an ComboBox is selected option */
onSelect: PropTypes.func,
/** Tells when a component is open */
opened: PropTypes.bool,
/** Combo box options */
options: PropTypes.array.isRequired,
/** Indicates that component is scaled by parent */
scaled: PropTypes.bool,
/** Indicates that component`s options is scaled by ComboButton */
scaledOptions: PropTypes.bool,
/** Selected option */
selectedOption: PropTypes.object.isRequired,
/** Select component width, one of default */
size: PropTypes.oneOf(["base", "middle", "big", "huge", "content"]),
/** Accepts css style */
style: PropTypes.oneOfType([PropTypes.object, PropTypes.array]),
/** The event will be raised when using `displayType: toggle` when clicking on a component */
toggleAction: PropTypes.func,
/** Accepts css text-overflow */
textOverflow: PropTypes.bool,
/** Disables clicking on the icon */
disableIconClick: PropTypes.bool,
};
2019-07-18 08:00:54 +00:00
ComboBox.defaultProps = {
displayType: "default",
isDisabled: false,
noBorder: false,
scaled: true,
scaledOptions: false,
size: "base",
disableIconClick: true,
showDisabledItems: false,
};
export default ComboBox;