2019-12-14 13:16:01 +00:00
|
|
|
import React from "react";
|
|
|
|
import PropTypes from "prop-types";
|
|
|
|
import Selector from "./sub-components/Selector";
|
|
|
|
import { utils, Backdrop, DropDown, Aside } from "asc-web-components";
|
|
|
|
import throttle from "lodash/throttle";
|
|
|
|
const { desktop } = utils.device;
|
|
|
|
|
|
|
|
const displayTypes = ["dropdown", "aside", "auto"];
|
|
|
|
const sizes = ["compact", "full"];
|
|
|
|
|
|
|
|
class AdvancedSelector extends React.Component {
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
|
|
|
|
this.ref = React.createRef();
|
|
|
|
|
|
|
|
this.state = {
|
|
|
|
displayType: this.getTypeByWidth()
|
|
|
|
};
|
2019-12-16 08:09:15 +00:00
|
|
|
|
|
|
|
this.throttledResize = throttle(this.resize, 300);
|
2019-12-14 13:16:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
2019-12-15 18:07:45 +00:00
|
|
|
if(this.props.isOpen) {
|
2019-12-14 13:16:01 +00:00
|
|
|
window.addEventListener("resize", this.throttledResize);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
resize = () => {
|
2019-12-15 18:07:45 +00:00
|
|
|
if (this.props.displayType !== "auto")
|
|
|
|
return;
|
|
|
|
|
2019-12-14 13:16:01 +00:00
|
|
|
const type = this.getTypeByWidth();
|
2019-12-15 18:07:45 +00:00
|
|
|
|
|
|
|
if (type === this.state.displayType)
|
|
|
|
return;
|
|
|
|
|
2019-12-14 13:16:01 +00:00
|
|
|
this.setState({ displayType: type });
|
|
|
|
};
|
|
|
|
|
2019-12-16 11:23:24 +00:00
|
|
|
onClose = (e) => {
|
2019-12-16 08:09:15 +00:00
|
|
|
//console.log("onClose");
|
2019-12-14 13:16:01 +00:00
|
|
|
//this.setState({ isOpen: false });
|
2019-12-16 11:23:24 +00:00
|
|
|
this.props.onCancel && this.props.onCancel(e);
|
2019-12-14 13:16:01 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
componentDidUpdate(prevProps) {
|
2019-12-15 18:07:45 +00:00
|
|
|
if(this.props.isOpen !== prevProps.isOpen) {
|
2020-02-02 12:00:30 +00:00
|
|
|
//console.log(`ADSelector componentDidUpdate isOpen=${this.props.isOpen}`);
|
2019-12-16 08:09:15 +00:00
|
|
|
if(this.props.isOpen) {
|
2019-12-15 18:07:45 +00:00
|
|
|
this.resize();
|
|
|
|
window.addEventListener("resize", this.throttledResize);
|
|
|
|
}
|
2019-12-16 08:09:15 +00:00
|
|
|
else {
|
|
|
|
this.throttledResize.cancel();
|
|
|
|
window.removeEventListener("resize", this.throttledResize);
|
|
|
|
}
|
2019-12-14 13:16:01 +00:00
|
|
|
}
|
|
|
|
|
2019-12-15 18:07:45 +00:00
|
|
|
if (this.props.displayType !== prevProps.displayType) {
|
2020-02-02 12:00:30 +00:00
|
|
|
//console.log(`ADSelector componentDidUpdate displayType=${this.props.displayType}`);
|
2019-12-15 18:07:45 +00:00
|
|
|
this.setState({ displayType: this.getTypeByWidth() });
|
2019-12-14 13:16:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
2019-12-15 18:07:45 +00:00
|
|
|
if(this.throttledResize)
|
|
|
|
{
|
|
|
|
this.throttledResize && this.throttledResize.cancel();
|
|
|
|
window.removeEventListener("resize", this.throttledResize);
|
|
|
|
}
|
2019-12-14 13:16:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
getTypeByWidth = () => {
|
|
|
|
const displayType = this.props.displayType !== "auto"
|
|
|
|
? this.props.displayType
|
|
|
|
: window.innerWidth < desktop.match(/\d+/)[0] ? "aside" : "dropdown";
|
|
|
|
|
|
|
|
//console.log("AdvancedSelector2 displayType", displayType);
|
|
|
|
|
|
|
|
return displayType;
|
|
|
|
};
|
|
|
|
|
|
|
|
render() {
|
2019-12-15 18:07:45 +00:00
|
|
|
const { displayType } = this.state;
|
|
|
|
const { isOpen, id, className, style } = this.props;
|
|
|
|
|
2020-02-02 12:00:30 +00:00
|
|
|
//console.log(`AdvancedSelector render() isOpen=${isOpen} displayType=${displayType}`);
|
2019-12-14 13:16:01 +00:00
|
|
|
|
|
|
|
return (
|
2019-12-15 18:07:45 +00:00
|
|
|
<div ref={this.ref} id={id} className={className} style={style}>
|
2019-12-14 13:16:01 +00:00
|
|
|
{displayType === "dropdown"
|
|
|
|
?
|
2020-01-09 13:27:49 +00:00
|
|
|
<DropDown open={isOpen} className="dropdown-container" clickOutsideAction={this.onClose}>
|
2019-12-14 13:16:01 +00:00
|
|
|
<Selector {...this.props} displayType={displayType} />
|
|
|
|
</DropDown>
|
|
|
|
:
|
|
|
|
<>
|
|
|
|
<Backdrop onClick={this.onClose} visible={isOpen} zIndex={310} />
|
|
|
|
<Aside visible={isOpen} scale={false} className="aside-container">
|
|
|
|
<Selector {...this.props} displayType={displayType} />
|
|
|
|
</Aside>
|
|
|
|
</>
|
|
|
|
}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
AdvancedSelector.propTypes = {
|
2019-12-15 18:07:45 +00:00
|
|
|
id: PropTypes.string,
|
|
|
|
className: PropTypes.oneOf([PropTypes.string, PropTypes.array]),
|
|
|
|
style: PropTypes.object,
|
2019-12-14 13:16:01 +00:00
|
|
|
options: PropTypes.array,
|
|
|
|
selectedOptions: PropTypes.array,
|
|
|
|
groups: PropTypes.array,
|
|
|
|
selectedGroups: PropTypes.array,
|
|
|
|
|
|
|
|
value: PropTypes.string,
|
|
|
|
placeholder: PropTypes.string,
|
|
|
|
selectAllLabel: PropTypes.string,
|
|
|
|
buttonLabel: PropTypes.string,
|
|
|
|
|
|
|
|
size: PropTypes.oneOf(sizes),
|
|
|
|
displayType: PropTypes.oneOf(displayTypes),
|
|
|
|
|
|
|
|
maxHeight: PropTypes.number,
|
|
|
|
|
|
|
|
isMultiSelect: PropTypes.bool,
|
|
|
|
isDisabled: PropTypes.bool,
|
|
|
|
selectedAll: PropTypes.bool,
|
|
|
|
isOpen: PropTypes.bool,
|
2019-12-19 07:00:58 +00:00
|
|
|
allowGroupSelection: PropTypes.bool,
|
2019-12-14 13:16:01 +00:00
|
|
|
allowCreation: PropTypes.bool,
|
|
|
|
allowAnyClickClose: PropTypes.bool,
|
|
|
|
hasNextPage: PropTypes.bool,
|
|
|
|
isNextPageLoading: PropTypes.bool,
|
|
|
|
|
|
|
|
onSearchChanged: PropTypes.func,
|
|
|
|
onSelect: PropTypes.func,
|
|
|
|
onGroupChange: PropTypes.func,
|
|
|
|
onCancel: PropTypes.func,
|
|
|
|
onAddNewClick: PropTypes.func,
|
2020-01-09 13:27:49 +00:00
|
|
|
loadNextPage: PropTypes.func
|
2019-12-14 13:16:01 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
AdvancedSelector.defaultProps = {
|
|
|
|
isMultiSelect: false,
|
|
|
|
size: "full",
|
|
|
|
buttonLabel: "Add members",
|
|
|
|
selectAllLabel: "Select all",
|
2019-12-19 07:00:58 +00:00
|
|
|
allowGroupSelection: false,
|
2019-12-14 13:16:01 +00:00
|
|
|
allowAnyClickClose: true,
|
|
|
|
displayType: "auto",
|
|
|
|
options: []
|
|
|
|
};
|
|
|
|
|
2020-01-09 13:27:49 +00:00
|
|
|
export default AdvancedSelector;
|