import clsx from 'clsx';
import { filter, find, get, noop } from 'lodash';
import PropTypes from 'prop-types';
import { createRef, Children, cloneElement, Component, forwardRef } from 'react';
import Count from '../count';
import Gridicon from '../gridicon';
import DropdownItem from './item';
import DropdownLabel from './label';
import DropdownSeparator from './separator';
import TranslatableString from './translatable/proptype';
import './style.scss';
class SelectDropdown extends Component {
static Item = DropdownItem;
static Separator = DropdownSeparator;
static Label = DropdownLabel;
static propTypes = {
id: PropTypes.string,
selectedText: TranslatableString,
selectedIcon: PropTypes.element,
selectedCount: PropTypes.number,
selectedSecondaryIcon: PropTypes.element,
positionSelectedSecondaryIconOnRight: PropTypes.bool,
initialSelected: PropTypes.string,
className: PropTypes.string,
style: PropTypes.object,
onSelect: PropTypes.func,
onToggle: PropTypes.func,
focusSibling: PropTypes.func,
tabIndex: PropTypes.number,
disabled: PropTypes.bool,
options: PropTypes.arrayOf(
PropTypes.shape( {
value: PropTypes.string.isRequired,
label: PropTypes.oneOfType( [ TranslatableString, PropTypes.node ] ).isRequired,
path: PropTypes.string,
icon: PropTypes.element,
secondaryIcon: PropTypes.element,
count: PropTypes.number,
} )
),
isLoading: PropTypes.bool,
ariaLabel: PropTypes.string,
showSelectedOption: PropTypes.bool,
children: PropTypes.node,
};
static defaultProps = {
options: [],
onSelect: noop,
onToggle: noop,
style: {},
showSelectedOption: true,
};
instanceId = crypto.randomUUID();
state = {
isOpen: false,
selected: this.getInitialSelectedItem(),
};
itemRefs = [];
setItemRef = ( index ) => ( itemEl ) => {
this.itemRefs[ index ] = itemEl;
};
constructor( props ) {
super( props );
this.dropdownContainerRef = props.innerRef ?? createRef();
}
componentWillUnmount() {
window.removeEventListener( 'click', this.handleOutsideClick );
}
componentDidUpdate( prevProps, prevState ) {
if ( this.state.isOpen ) {
window.addEventListener( 'click', this.handleOutsideClick );
} else {
window.removeEventListener( 'click', this.handleOutsideClick );
}
if ( this.state.isOpen !== prevState.isOpen ) {
this.props.onToggle( {
target: this,
open: this.state.isOpen,
} );
}
}
getInitialSelectedItem() {
// This method is only useful for the case when the component is uncontrolled, i.e., the
// selected state is in local state as opposed to being maintained by parent container.
// The `SelectDropdown` is uncontrolled iff the items are specified as `options` prop.
// (And is controlled when the items are specified as `children`.)
if ( ! this.props.options.length ) {
return undefined;
}
// Use the `initialSelected` prop if specified
if ( this.props.initialSelected ) {
return this.props.initialSelected;
}
// Otherwise find the first option that is an item, i.e., not label or separator
return get(
find( this.props.options, ( item ) => item && ! item.isLabel ),
'value'
);
}
getSelectedText() {
const { options, selectedText } = this.props;
const { selected } = this.state;
if ( selectedText ) {
return selectedText;
}
// return currently selected text
return get( find( options, { value: selected } ), 'label' );
}
getSelectedIcon() {
const { options, selectedIcon } = this.props;
const { selected } = this.state;
if ( selectedIcon ) {
return selectedIcon;
}
// return currently selected icon
return get( find( options, { value: selected } ), 'icon' );
}
getSelectedSecondaryIcon() {
const { options, selectedSecondaryIcon } = this.props;
const { selected } = this.state;
if ( selectedSecondaryIcon ) {
return selectedSecondaryIcon;
}
return get( find( options, { value: selected } ), 'secondaryIcon' );
}
dropdownOptions() {
let refIndex = 0;
if ( this.props.children ) {
// add refs and focus-on-click handlers to children
return Children.map( this.props.children, ( child ) => {
if (
! child ||
! [ DropdownItem, DropdownSeparator, DropdownLabel ].includes( child.type )
) {
return null;
}
return cloneElement( child, {
ref: child.type === DropdownItem ? this.setItemRef( refIndex++ ) : null,
onClick: ( event ) => {
this.dropdownContainerRef.current.focus();
if ( typeof child.props.onClick === 'function' ) {
child.props.onClick( event );
}
},
} );
} );
}
return this.props.options
.filter( ( item ) => {
if ( this.props.showSelectedOption ) {
return true;
}
return item.value !== this.state.selected;
} )
.map( ( item, index ) => {
if ( ! item ) {
return