import {IPopoverProps, PopoverPosition} from '@blueprintjs/core'; import {IItemListRendererProps, Select} from '@blueprintjs/select'; import * as _ from 'lodash'; import {action, computed, observable} from 'mobx'; import {observer} from 'mobx-react'; import * as React from 'react'; import {DropdownOption} from '../../interfaces/DropdownOption'; import {ErrorMessage} from '../ErrorMessage'; import {Icon, IconName} from '../Icon'; import {Label} from '../Label'; import styles from './Dropdown.scss'; import classNames from 'classnames'; import {IItemRendererProps} from '@blueprintjs/select/src/common/itemRenderer'; interface DropdownProps { options: DropdownOption[]; selectedValue: string; onChange: (newValue: string) => void; label?: string; placeholder?: string; error?: string; searchable?: boolean; onClear?: () => void; popoverProps?: IPopoverProps; className?: string; toggleClassName?: string; optionsClassName?: string; optionClassName?: string; } type OptionRenderer = (item: DropdownOption, index: number) => JSX.Element; @observer export class Dropdown extends React.Component { @observable private searchQuery: string = ''; @observable private scrollPosition: number = 0; private popoverReference?: HTMLElement; private toggleReference?: HTMLElement; private optionsListReference?: HTMLElement; private static readonly DEFAULT_PLACEHOLDER: string = 'Select a value...'; public componentDidMount() { this.updateScrollPosition(); } public componentDidUpdate() { this.updateScrollPosition(); } @computed private get selectedOption() { const {options, selectedValue} = this.props; return options.find((option) => option.value === selectedValue); } @computed private get selectedOptionText() { return this.selectedOption?.label || null; } private handleOptionsListOpening = () => { this.scrollToSelectedOption(); this.updateOptionsListMinWidth(); } private scrollToSelectedOption = () => { this.optionsListReference.scrollTop = this.scrollPosition; } private updateOptionsListMinWidth = () => { const {popoverReference, toggleReference, optionsListReference} = this; const searchInput: HTMLElement = popoverReference?.querySelector('.bp4-input-group'); const buttonWidth = toggleReference.clientWidth; optionsListReference.style.width = `${buttonWidth}px`; if (searchInput) { searchInput.style.width = `${buttonWidth}px`; } } private handleToggleReference = (reference: HTMLElement) => { this.toggleReference = reference; } private handlePopoverReference = (reference: HTMLElement) => { this.popoverReference = reference; } @action public updateScrollPosition = () => { const {selectedOption, props: {options}} = this; const optionGroups = _.uniq(_.compact(options.map((option) => option.group))); const selectedOptionGroupIndex = optionGroups.findIndex((optionGroup) => optionGroup === selectedOption?.group); const selectedOptionIndex = options.findIndex((option) => option === selectedOption); const optionHeight = 28; const countOfItemsAbove = 3; const scrollPosition = optionHeight * (selectedOptionIndex + selectedOptionGroupIndex + 1 - countOfItemsAbove); this.scrollPosition = scrollPosition < 0 ? 0 : scrollPosition; } @action private handleSearchQueryChange = (newSearchQuery: string) => { this.searchQuery = newSearchQuery; } private handleOptionSelect = (selectedOption: DropdownOption) => { this.props.onChange(selectedOption.value); } private handleClear = (e: React.MouseEvent) => { e.stopPropagation(); this.props.onClear(); } private itemListRenderer = ({filteredItems: options, renderItem}: IItemListRendererProps) => { const {searchable, optionsClassName} = this.props; return (
this.optionsListReference = element} className={classNames(styles.options, {[styles.searchable]: searchable}, optionsClassName)} > {options.length > 0 ? this.renderOptions(options, renderItem) :
No results.
}
); } private renderOptions = (options: DropdownOption[], renderOption: OptionRenderer) => { const groupedOptions: {[key: string]: DropdownOption[]} = _.groupBy(options, 'group'); return Object.entries(groupedOptions).map(([groupName, groupOptions]) => { return ( {groupName !== 'undefined' &&
{groupName}
} {groupOptions.map(renderOption)}
); }); } private renderOption = (option: DropdownOption, {handleClick}: IItemRendererProps) => { const isSelected = option === this.selectedOption; return (
{option.label}
); } private itemPredicate = (query: string, item: DropdownOption) => { if (query) { return item.label.toLowerCase().includes(query.toLowerCase()); } else { return true; } } private renderClearButton(onClick: (e: React.MouseEvent) => void) { return (
); } public render() { const {options, selectedValue, label, placeholder, error, searchable = false, onClear, popoverProps, className, toggleClassName} = this.props; return (
{label &&
); } }