import * as React from 'react'; import {DropdownOption} from '../../interfaces/DropdownOption'; import {ErrorMessage} from '../ErrorMessage'; import {Icon, IconName} from '../Icon'; import {Label} from '../Label'; import {Tag} from '../Tag'; import styles from './MultiSelectDropdown.scss'; import {observer} from 'mobx-react'; import {IItemListRendererProps, Select} from '@blueprintjs/select'; import {IPopoverProps, Menu, MenuItem, PopoverPosition} from '@blueprintjs/core'; import {action, computed, observable} from 'mobx'; import * as _ from 'lodash'; import classNames from 'classnames'; interface MultiSelectDropdownProps { options: DropdownOption[]; onChange: (values: string[]) => void; selectedValues: string[]; label?: string; placeholder?: string; error?: string; searchable?: boolean; onClear?: () => void; popoverProps?: IPopoverProps; className?: string; toggleClassName?: string; } @observer export class MultiSelectDropdown extends React.Component { @observable private searchQuery: string = ''; private popoverReference?: HTMLElement; private toggleReference?: HTMLElement; private static readonly DEFAULT_PLACEHOLDER: string = 'Select values'; @computed private get selectedOptions() { const {options, selectedValues} = this.props; return selectedValues .map((selectedValue) => options.find((option) => option.value === selectedValue)) .filter((option) => !!option); } private updateMenuMinWidth = () => { const {popoverReference, toggleReference} = this; const menu: HTMLElement = popoverReference.querySelector('.bp4-menu'); const searchInput: HTMLElement = popoverReference.querySelector('.bp4-input-group'); const buttonWidth = toggleReference.clientWidth; menu.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 private handleSearchQueryChange = (newSearchQuery: string) => { this.searchQuery = newSearchQuery; } private handleSelectedValueAdd = (valueToAdd: string) => { const {selectedValues} = this.props; const updatedValues = [...selectedValues, valueToAdd]; this.props.onChange(updatedValues); } private handleSelectedValueRemove = (valueToRemove: string) => { const {selectedValues} = this.props; const updatedValues = selectedValues.filter((selectedValue) => selectedValue !== valueToRemove); this.props.onChange(updatedValues); } private handleClear = (e: React.MouseEvent) => { e.stopPropagation(); this.props.onClear(); } private itemPredicate = (query: string, item: DropdownOption) => { if (query) { return item.label.toLowerCase().includes(query.toLowerCase()); } else { return true; } } private itemListRenderer = ({filteredItems: options}: IItemListRendererProps) => { return ( {options.length > 0 ? this.renderOptions(options) : } ); } private renderOptions = (options: DropdownOption[]) => { const groupedOptions: {[key: string]: DropdownOption[]} = _.groupBy(options, 'group'); return Object.entries(groupedOptions).map(([groupName, groupOptions]) => { return ( {groupName !== 'undefined' &&
{groupName}
} {groupOptions.map(this.renderOption)}
); }); } private renderOption = (option: DropdownOption) => { const isSelected = this.props.selectedValues.includes(option.value); return ( { e.stopPropagation(); isSelected ? this.handleSelectedValueRemove(option.value) : this.handleSelectedValueAdd(option.value); }} icon={isSelected ? : undefined} className={classNames(styles.option, {[styles.selected]: isSelected})} key={option.value} /> ); } private renderSelectedOptionsTags() { return (
{this.selectedOptions.map((option) => ( { e.stopPropagation(); this.handleSelectedValueRemove(option.value); }} className={styles.selectedOption} key={option.value} /> ))}
); } private renderClearButton(onClick: (e: React.MouseEvent) => void) { return (
); } public render() { const {options, label, placeholder, error, searchable = false, onClear, popoverProps, className, toggleClassName} = this.props; const selectedOptionsArePresent = this.selectedOptions.length > 0; return (
{label &&
); } }