2023-11-24 10:21:46 +00:00
|
|
|
import React, { Component } from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
|
|
|
|
import '../css/single-selector.css';
|
|
|
|
|
|
|
|
const propTypes = {
|
2024-06-17 01:32:05 +00:00
|
|
|
customSelectorToggle: PropTypes.object,
|
|
|
|
menuCustomClass: PropTypes.string,
|
|
|
|
isDropdownToggleShown: PropTypes.bool,
|
|
|
|
currentSelectedOption: PropTypes.object,
|
2023-11-24 10:21:46 +00:00
|
|
|
options: PropTypes.array.isRequired,
|
|
|
|
selectOption: PropTypes.func.isRequired,
|
2024-04-23 08:26:31 +00:00
|
|
|
operationBeforeSelect: PropTypes.func,
|
2023-11-24 10:21:46 +00:00
|
|
|
toggleItemFreezed: PropTypes.func
|
|
|
|
};
|
|
|
|
|
|
|
|
class Selector extends Component {
|
|
|
|
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
this.state = {
|
|
|
|
isPopoverOpen: false
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
document.addEventListener('click', this.handleOutsideClick);
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
document.removeEventListener('click', this.handleOutsideClick);
|
|
|
|
}
|
|
|
|
|
|
|
|
handleOutsideClick = (e) => {
|
|
|
|
const { isPopoverOpen } = this.state;
|
|
|
|
if (isPopoverOpen && !this.selector.contains(e.target)) {
|
|
|
|
this.togglePopover();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
togglePopover = () => {
|
|
|
|
this.setState({
|
|
|
|
isPopoverOpen: !this.state.isPopoverOpen
|
|
|
|
}, () => {
|
|
|
|
if (this.props.toggleItemFreezed) {
|
|
|
|
this.props.toggleItemFreezed(this.state.isPopoverOpen);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
onToggleClick = (e) => {
|
|
|
|
e.stopPropagation();
|
|
|
|
this.togglePopover();
|
|
|
|
};
|
|
|
|
|
|
|
|
selectItem = (e, targetItem) => {
|
|
|
|
e.stopPropagation();
|
2024-04-23 08:26:31 +00:00
|
|
|
if (this.props.operationBeforeSelect) {
|
|
|
|
this.props.operationBeforeSelect();
|
|
|
|
} else {
|
|
|
|
this.props.selectOption(targetItem);
|
|
|
|
}
|
2023-11-24 10:21:46 +00:00
|
|
|
this.togglePopover();
|
|
|
|
};
|
|
|
|
|
|
|
|
render() {
|
|
|
|
const { isPopoverOpen } = this.state;
|
2024-06-17 01:32:05 +00:00
|
|
|
const { currentSelectedOption, options, isDropdownToggleShown, menuCustomClass = '',
|
|
|
|
customSelectorToggle = null
|
|
|
|
} = this.props;
|
2023-11-24 10:21:46 +00:00
|
|
|
return (
|
|
|
|
<div className="sf-single-selector position-relative">
|
2024-06-17 01:32:05 +00:00
|
|
|
<div onClick={this.onToggleClick}>
|
|
|
|
{customSelectorToggle ? customSelectorToggle : (
|
|
|
|
<span className="cur-option">
|
|
|
|
{currentSelectedOption.text}
|
2024-06-21 04:07:58 +00:00
|
|
|
{isDropdownToggleShown && <i className="sf3-font sf3-font-down ml-2 toggle-icon"></i>}
|
2024-06-17 01:32:05 +00:00
|
|
|
</span>
|
|
|
|
)}
|
|
|
|
</div>
|
2023-11-24 10:21:46 +00:00
|
|
|
{isPopoverOpen && (
|
2024-06-17 01:32:05 +00:00
|
|
|
<div className={`options-container position-absolute rounded shadow mt-1 ${menuCustomClass}`} ref={ref => this.selector = ref}>
|
2023-12-08 08:52:37 +00:00
|
|
|
<ul className="option-list list-unstyled py-3 o-auto">
|
2023-11-24 10:21:46 +00:00
|
|
|
{options.map((item, index) => {
|
|
|
|
return (
|
2023-12-08 08:52:37 +00:00
|
|
|
<li key={index} className="option-item h-6 py-1 px-3 d-flex justify-content-between align-items-center" onClick={(e) => {this.selectItem(e, item);}}>
|
2023-11-24 10:21:46 +00:00
|
|
|
<span className="option-item-text flex-shrink-0 mr-3">{item.text}</span>
|
2024-06-19 07:16:54 +00:00
|
|
|
<i className={`sf2-icon-tick ${item.isSelected ? '' : 'invisible'}`}></i>
|
2023-11-24 10:21:46 +00:00
|
|
|
</li>
|
|
|
|
);
|
|
|
|
})}
|
|
|
|
</ul>
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Selector.propTypes = propTypes;
|
|
|
|
|
|
|
|
export default Selector;
|