/** * Copyright Schrodinger, LLC * * @fileoverview Dropdown picker */ import { debounce, first, get, intersection, isEmpty, isNil, isObject, noop, some, sumBy } from 'lodash' import _AutoSizer, { AutoSizerProps, Size } from "react-virtualized-auto-sizer" import { FixedSizeList as _FixedSizeList, FixedSizeListProps, ListChildComponentProps } from "react-window" import { FontAwesomeIcon } from '@fortawesome/react-fontawesome' import { IconDefinition } from '@fortawesome/fontawesome-svg-core' import { solid } from '@fortawesome/fontawesome-svg-core/import.macro' import React, { PureComponent, SyntheticEvent, KeyboardEvent } from 'react' import { KeyCodes, EventType, BORDER_WIDTH } from './enums' import { SelectOption } from './types' import { Option } from './Option' import { Portal } from 'react-portal' import { PICKLIST_BORDER_WIDTH, PICKLIST_OPTION_HEIGHT } from './consts' import clsx from 'clsx' import styles from './styles.module.scss' // hack to force TS recognition :( // @see: https://github.com/bvaughn/react-virtualized/issues/1739#issuecomment-1264276522 const FixedSizeList = _FixedSizeList as unknown as React.FC; const AutoSizer = _AutoSizer as unknown as React.FC; export class Dropdown extends PureComponent { private dropdownRef = React.createRef() private picklistRef = React.createRef() private debouncedSetPicklistState: () => void private resizePicklist: ResizeObserver | undefined; static defaultProps = { optional: false, options: [], initialOptions: [], placeholder: 'Select…', filterable: false, multiselect: false, typeaheadValue: '', onChange: noop, onSelect: noop, onDeselect: noop, onShowPicker: noop, onHidePicker: noop, onTypeaheadChange: noop, onPicklistChange: noop, } constructor(props: Props) { super(props) let selected: State['selected'] if (props.selected) { selected = isObject(props.selected) ? new Set([...props.selected]) : new Set([props.selected]) } else { selected = new Set() } this.state = { selected, open: false, // allow pre-population of the initial value, which is particularly useful // for typeaheads search: props.typeaheadValue || '', initialTypeaheadValue: props.typeaheadValue, options: props.options, // Retain a copy of the props we received, for later comparison so we can // determine if they were changed externally initialOptions: props.options, autofill: '', } this.debouncedSetPicklistState = debounce(this.setPicklistState, 300) } componentDidMount(): void { this.resizePicklist = new ResizeObserver(() => { if (!this.dropdownRef.current || !this.picklistRef.current) { return; } const dropdown = this.dropdownRef.current.getBoundingClientRect() this.picklistRef.current.style.width = `${dropdown.width}px` }) } componentDidUpdate(): void { if (!this.dropdownRef.current || !this.picklistRef.current) { return; } const dropdown = this.dropdownRef.current.getBoundingClientRect() // We need to sum up anything inside the picklist container (ie the optional search input) const childHeights = sumBy(this.picklistRef.current.children, child => child.getBoundingClientRect().height) let top = dropdown.height + dropdown.top - BORDER_WIDTH // If picklist would go beyond the viewport, see if we can shift it upward, // provided there is sufficient space available if (top + childHeights >= window.innerHeight && top >= childHeights) { top = dropdown.top - childHeights + BORDER_WIDTH } // Take scrollbar position into account const scrollOffset = document.documentElement.scrollTop || document.body.scrollTop; top = top + scrollOffset; this.picklistRef.current.style.top = `${top}px` this.picklistRef.current.style.left = `${dropdown.left}px` } componentWillUnmount(): void { if (this.resizePicklist) { this.resizePicklist.disconnect(); } } public render(): JSX.Element { const classes = clsx(styles['Dropdown'], this.props.className, { [styles.disabled]: this.isDisabled(), }) return (
{this.renderHeader()} {this.renderPicklist()}
) } /** * * @returns {JSX.Element} */ renderHeader(): JSX.Element { if (this.props.header) { const selectedValue = this.getSelectedValue() return (
{this.props.header({ selectedValue, selected: this.state.selected, show: this.showPicker, hide: this.hidePicker, })}
) } if (this.props.typeahead) { return (
{this.renderClearIcon()} {this.renderArrowIcon()}
) } return (
{this.getSelectedValue()} {this.renderArrowIcon()}
) } renderPicklist(): JSX.Element|null { if (!this.state.open) { return null } if (this.props.typeahead && this.state.options.length === 0) { return null } if (this.resizePicklist && this.dropdownRef.current) { // Observer on dropdown element resize this.resizePicklist.observe(this.dropdownRef.current); } if (this.props.picklist) { const customPicklistStyle: React.CSSProperties = { zIndex: this.props.zIndex, width: this.dropdownRef.current?.offsetWidth, position: 'absolute', } return (
{this.props.picklist({ onClick: this.onChange, options: this.props.options, selectedOptions: this.props.selectedOptions, hide: this.hidePicker, })}
) } const classes = clsx(styles['Picklist'], { [styles['search-active']]: this.state.search.trim() !== '', [styles['multiselect']]: this.props.multiselect, }) const style: React.CSSProperties = { zIndex: this.props.zIndex, width: this.dropdownRef.current?.offsetWidth, minHeight: Math.min(this.state.options.length * PICKLIST_OPTION_HEIGHT, 300) } return (
{this.renderSearch()}
    {this.renderDeselectOption()} {this.renderPicklistItems()}
) } /** * Render an extra option in the picklist, displaying the placeholder value, * so users can "deselect" their current selection - ie. this dropdown is optional */ renderDeselectOption(): JSX.Element|null { // These options automatically infer that we should not render the extra option // !optional: if this is off, there's nothing to do here // multiselect: this dropdown style inherently supports deselection // state.search: temporarily hide deselect option while actively filtering if (!this.props.optional || this.props.multiselect || this.state.search.trim()) { return null } // Only show the deselect option when a value is currently selected if (this.state.selected.size === 0 && !this.props.value) { return null } return (