2019-08-13 11:21:38 +00:00
|
|
|
import React, { memo, useCallback } from 'react'
|
2019-06-10 12:28:54 +00:00
|
|
|
import styled, { css } from 'styled-components'
|
|
|
|
import PropTypes from 'prop-types'
|
2019-08-13 11:21:38 +00:00
|
|
|
import Scrollbar from '../scrollbar'
|
|
|
|
import DropDownItem from '../drop-down-item'
|
|
|
|
import { FixedSizeList } from "react-window"
|
2019-06-10 12:28:54 +00:00
|
|
|
|
|
|
|
const StyledDropdown = styled.div`
|
2019-08-07 09:36:28 +00:00
|
|
|
font-family: 'Open Sans',sans-serif,Arial;
|
2019-06-18 14:07:57 +00:00
|
|
|
font-style: normal;
|
|
|
|
font-weight: 600;
|
|
|
|
font-size: 13px;
|
|
|
|
|
2019-08-09 07:57:12 +00:00
|
|
|
${props => props.maxHeight && `
|
2019-08-13 11:21:38 +00:00
|
|
|
max-height: ${props.maxHeight}px;
|
2019-08-09 07:57:12 +00:00
|
|
|
overflow-y: auto;
|
|
|
|
`}
|
|
|
|
|
2019-06-10 12:28:54 +00:00
|
|
|
position: absolute;
|
2019-07-18 07:43:17 +00:00
|
|
|
${props => props.manualWidth && `width: ${props.manualWidth};`}
|
2019-07-19 09:11:37 +00:00
|
|
|
${props => (props.directionY === 'top' && css`bottom: ${props => props.manualY ? props.manualY : '100%'};`)}
|
|
|
|
${props => (props.directionY === 'bottom' && css`top: ${props => props.manualY ? props.manualY : '100%'};`)}
|
2019-07-18 14:21:43 +00:00
|
|
|
${props => (props.directionX === 'right' && css`right: 0px;`)}
|
|
|
|
${props => (props.directionX === 'left' && css`left: 0px;`)}
|
2019-06-10 12:28:54 +00:00
|
|
|
z-index: 1000;
|
2019-07-04 11:43:55 +00:00
|
|
|
margin-top: ${props => (props.isUserPreview ? '6px' : '0px')};
|
|
|
|
margin-right: ${props => (props.isUserPreview ? '6px' : '0px')};
|
2019-06-10 12:28:54 +00:00
|
|
|
display: ${props => (props.isOpen || props.opened ? 'block' : 'none')};
|
2019-07-04 11:43:55 +00:00
|
|
|
background: #FFFFFF;
|
2019-06-18 14:07:57 +00:00
|
|
|
border-radius: 6px;
|
|
|
|
-moz-border-radius: 6px;
|
|
|
|
-webkit-border-radius: 6px;
|
|
|
|
box-shadow: 0px 5px 20px rgba(0, 0, 0, 0.13);
|
|
|
|
-moz-box-shadow: 0px 5px 20px rgba(0, 0, 0, 0.13);
|
|
|
|
-webkit-box-shadow: 0px 5px 20px rgba(0, 0, 0, 0.13);
|
2019-06-10 12:28:54 +00:00
|
|
|
`;
|
|
|
|
|
2019-07-04 11:43:55 +00:00
|
|
|
const Arrow = styled.div`
|
|
|
|
position: absolute;
|
|
|
|
top: -6px;
|
2019-07-19 09:11:37 +00:00
|
|
|
${props => (props.directionX === 'right' && css`right: 16px;`)}
|
|
|
|
${props => (props.directionX === 'left' && css`left: 16px;`)}
|
2019-07-04 11:43:55 +00:00
|
|
|
width: 24px;
|
|
|
|
height: 6px;
|
|
|
|
background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg'%3E%3Cpath d='M9.27954 1.12012C10.8122 -0.295972 13.1759 -0.295971 14.7086 1.12012L18.8406 4.93793C19.5796 5.62078 20.5489 6 21.5551 6H24H0H2.43299C3.4392 6 4.40845 5.62077 5.1475 4.93793L9.27954 1.12012Z' fill='%23206FA4'/%3E%3C/svg%3E");
|
2019-07-18 14:21:43 +00:00
|
|
|
`;
|
|
|
|
|
2019-08-13 11:21:38 +00:00
|
|
|
const CustomScrollbars = ({ onScroll, forwardedRef, style, children }) => {
|
|
|
|
const refSetter = useCallback(scrollbarsRef => {
|
|
|
|
if (scrollbarsRef) {
|
|
|
|
forwardedRef(scrollbarsRef.view);
|
|
|
|
} else {
|
|
|
|
forwardedRef(null);
|
|
|
|
}
|
|
|
|
}, [forwardedRef]);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Scrollbar
|
|
|
|
ref={refSetter}
|
|
|
|
style={{ ...style, overflow: "hidden" }}
|
|
|
|
onScroll={onScroll}
|
|
|
|
stype="mediumBlack"
|
|
|
|
>
|
|
|
|
{children}
|
|
|
|
</Scrollbar>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
const CustomScrollbarsVirtualList = React.forwardRef((props, ref) => (
|
|
|
|
<CustomScrollbars {...props} forwardedRef={ref} />
|
|
|
|
));
|
|
|
|
|
|
|
|
const Row = memo(({ data, index, style }) => {
|
|
|
|
const option = data[index];
|
|
|
|
|
2019-07-31 11:45:39 +00:00
|
|
|
return (
|
2019-08-13 11:21:38 +00:00
|
|
|
<DropDownItem
|
|
|
|
{...option.props}
|
|
|
|
style={style} />
|
2019-07-31 11:45:39 +00:00
|
|
|
);
|
2019-07-24 17:43:18 +00:00
|
|
|
});
|
2019-06-24 13:18:47 +00:00
|
|
|
|
2019-08-13 11:21:38 +00:00
|
|
|
class DropDown extends React.PureComponent {
|
|
|
|
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
|
|
|
|
this.state = {
|
|
|
|
width: 100
|
|
|
|
};
|
|
|
|
|
|
|
|
this.dropDown = React.createRef();
|
|
|
|
};
|
|
|
|
|
|
|
|
setDropDownWidthState = () => {
|
|
|
|
if (this.dropDown.current) {
|
|
|
|
this.setState({
|
|
|
|
width: this.dropDown.current.offsetWidth
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount () {
|
|
|
|
this.setDropDownWidthState();
|
|
|
|
};
|
|
|
|
|
|
|
|
componentDidUpdate(prevProps) {
|
|
|
|
if (this.props.opened !== prevProps.opened || this.props.isOpen !== prevProps.isOpen) {
|
|
|
|
this.setDropDownWidthState();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
render() {
|
|
|
|
const {maxHeight, withArrow, directionX, children} = this.props;
|
|
|
|
const dropDownMaxHeightProp = maxHeight ? { height: maxHeight + 'px' } : {};
|
|
|
|
//console.log("DropDown render");
|
|
|
|
return (
|
|
|
|
<StyledDropdown
|
|
|
|
ref={this.dropDown}
|
|
|
|
{...this.props}
|
|
|
|
{...dropDownMaxHeightProp}
|
|
|
|
>
|
|
|
|
{withArrow && <Arrow directionX={directionX} />}
|
|
|
|
{maxHeight
|
|
|
|
? <FixedSizeList
|
|
|
|
height={maxHeight}
|
|
|
|
width={this.state.width}
|
|
|
|
itemSize={36}
|
|
|
|
itemCount={children.length}
|
|
|
|
itemData={children}
|
|
|
|
outerElementType={CustomScrollbarsVirtualList}
|
|
|
|
>
|
|
|
|
{Row}
|
|
|
|
</FixedSizeList>
|
|
|
|
: children}
|
|
|
|
</StyledDropdown>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-06-28 11:37:43 +00:00
|
|
|
DropDown.propTypes = {
|
2019-07-31 11:45:39 +00:00
|
|
|
directionX: PropTypes.oneOf(['left', 'right']),
|
|
|
|
directionY: PropTypes.oneOf(['bottom', 'top']),
|
|
|
|
withArrow: PropTypes.bool,
|
|
|
|
manualWidth: PropTypes.string,
|
2019-08-09 07:57:12 +00:00
|
|
|
manualY: PropTypes.string,
|
2019-08-13 11:21:38 +00:00
|
|
|
maxHeight: PropTypes.number
|
2019-06-28 11:37:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
DropDown.defaultProps = {
|
2019-07-31 11:45:39 +00:00
|
|
|
directionX: 'left',
|
|
|
|
directionY: 'bottom',
|
2019-08-13 11:21:38 +00:00
|
|
|
withArrow: false
|
2019-06-28 11:37:43 +00:00
|
|
|
};
|
|
|
|
|
2019-06-10 12:28:54 +00:00
|
|
|
export default DropDown
|