import classNames from "classnames"; import GeminiScrollbar from "react-gemini-scrollbar"; import React from "react"; import PropTypes from "prop-types"; import { CSSTransition } from "react-transition-group"; import ReactDOM from "react-dom"; import BindMixin from "../Mixin/BindMixin"; import DOMUtil from "../Util/DOMUtil"; import Keycodes from "../constants/Keycodes"; import Portal from "../Portal/Portal.js"; import Util from "../Util/Util"; import DropdownListTrigger from "./DropdownListTrigger"; class Dropdown extends Util.mixin(BindMixin) { get methodsToBind() { return [ "closeDropdown", "handleMenuToggle", "handleExternalClick", "handleKeyDown", "handleMenuRender", "handleWrapperBlur" ]; } constructor() { super(); this.container = null; this.state = { maxDropdownHeight: null, menuDirection: "down", menuHeight: null, menuPositionStyle: null, isOpen: false, renderHidden: false, selectedID: null }; this.dropdownMenuRef = React.createRef(); this.dropdownWrapperRef = React.createRef(); } UNSAFE_componentWillMount() { super.UNSAFE_componentWillMount(...arguments); const props = this.props; if (!props.persistentID) { this.setState({ selectedID: props.initialID }); } } componentDidMount() { this.container = this.getScrollContainer(); } componentDidUpdate() { if (this.state.isOpen) { global.addEventListener("resize", this.closeDropdown); } else { global.removeEventListener("resize", this.closeDropdown); } } UNSAFE_componentWillUpdate(nextProps, nextState) { // If the open state changed, add or remove listener as needed. if (nextState.isOpen !== this.state.isOpen) { if (nextState.isOpen) { this.addKeydownListener(); this.addScrollListener(); } else { this.removeKeydownListener(); this.removeScrollListener(); } } } handleExternalClick() { this.closeDropdown(); } handleKeyDown(event) { if (event.keyCode === Keycodes.esc) { this.closeDropdown(); } } handleItemClick(item) { const props = this.props; props.onItemSelection(item); const newState = { isOpen: false }; // Only set the selectedID if persistentID is not set if (!props.persistentID) { newState.selectedID = item.id; } this.setState(newState); this.removeBlurTimeout(); } handleWrapperBlur() { this.removeBlurTimeout(); this.currentBlurTimeout = setTimeout(() => { this.closeDropdown(); }, 150); // We need to remove focus from the button to avoid this event firing again // when we open the dropdown global.focus(); } handleMenuToggle(e) { e.stopPropagation(); if (this.state.isOpen) { this.closeDropdown(); } else { this.openDropdown(); } this.removeBlurTimeout(); } handleMenuRender() { // If the menu is hidden while rendering, then we need to calculate its // optimal location and then un-hide it. if (this.state.renderHidden) { this.determineOptimalMenuLocation(); } } addKeydownListener() { global.document.body.addEventListener("keydown", this.handleKeyDown); } addScrollListener() { if (this.container && this.container.current) { this.container.current.addEventListener("scroll", this.closeDropdown); } } removeKeydownListener() { global.document.body.removeEventListener("keydown", this.handleKeyDown); } removeScrollListener() { if (this.container && this.container.current) { this.container.current.removeEventListener("scroll", this.closeDropdown); } } removeBlurTimeout() { if (this.currentBlurTimeout) { global.clearTimeout(this.currentBlurTimeout); } } determineOptimalMenuLocation() { let height = null; let menuDirection = this.state.menuDirection; const menuPositionStyle = {}; const spaceAroundDropdownButton = DOMUtil.getNodeClearance( this.dropdownWrapperRef.current ); const dropdownChildHeight = this.dropdownMenuRef && this.dropdownMenuRef.current ? this.dropdownMenuRef.current.firstChild.clientHeight : 0; const menuHeight = this.state.menuHeight || dropdownChildHeight; const isMenuTallerThanBottom = menuHeight > spaceAroundDropdownButton.bottom; const isMenuTallerThanTop = menuHeight > spaceAroundDropdownButton.top; const isMenuShorterThanTop = !isMenuTallerThanTop; const isTopTallerThanBottom = spaceAroundDropdownButton.top > spaceAroundDropdownButton.bottom; // If the menu height is larger than the space available on the bottom and // less than the space available on top, then render it up. If the height // of the menu exceeds the space below and above, but there is more space // above than below, render it up. Otherwise, render down. if ( (isMenuTallerThanBottom && isMenuShorterThanTop) || (isMenuTallerThanBottom && isMenuTallerThanTop && isTopTallerThanBottom) ) { menuDirection = "up"; menuPositionStyle.bottom = spaceAroundDropdownButton.bottom + spaceAroundDropdownButton.boundingRect.height; menuPositionStyle.top = "auto"; height = spaceAroundDropdownButton.top; } else { menuDirection = "down"; menuPositionStyle.bottom = "auto"; menuPositionStyle.top = spaceAroundDropdownButton.top + spaceAroundDropdownButton.boundingRect.height; height = spaceAroundDropdownButton.bottom; } if (this.props.matchButtonWidth) { menuPositionStyle.left = spaceAroundDropdownButton.left; menuPositionStyle.right = spaceAroundDropdownButton.right; } else if (this.props.anchorRight) { menuPositionStyle.left = "auto"; menuPositionStyle.right = spaceAroundDropdownButton.right; } else { menuPositionStyle.left = spaceAroundDropdownButton.left; menuPositionStyle.right = "auto"; } // We assume that 125 pixels is the smallest height we should render. if (height < 125) { height = 125; } this.setState({ menuDirection, maxDropdownHeight: height, menuHeight, menuPositionStyle, renderHidden: false }); } openDropdown() { const state = Object.assign({}, this.state); state.isOpen = true; state.renderHidden = true; // If we don't already know the menu height, we need to set the menu // position to a default state to trigger its recalculation on the next // render. if ( this.state.menuHeight == null && this.dropdownWrapperRef && this.dropdownWrapperRef.current ) { const buttonPosition = this.dropdownWrapperRef.current.getBoundingClientRect(); state.menuDirection = "down"; state.menuPositionStyle = { top: buttonPosition.top + buttonPosition.height, left: buttonPosition.left }; } this.setState(state); } closeDropdown() { if (this.state.isOpen) { this.setState({ isOpen: false, renderHidden: false }); } } getMenuItems(items) { const selectedID = this.getSelectedID(); return items.map(item => { const classSet = classNames( { "is-selectable": item.selectable !== false, "is-selected": item.id === selectedID }, item.className, this.props.dropdownMenuListItemClassName ); let handleUserClick = null; if (item.selectable !== false) { handleUserClick = this.handleItemClick.bind(this, item); } return (