import React, { Component, KeyboardEventHandler } from 'react'; import removeAccents from 'remove-accents'; import classnames from 'classnames'; import { withFocusOutside, BaseControl, Flex, FlexItem, } from '@wordpress/components'; import { useState } from '@wordpress/element'; import { __ } from '@wordpress/i18n'; import { speak } from '@wordpress/a11y'; // @ts-ignore import { FixedSizeList as List } from 'react-window'; import { chevronDown, chevronUp, Icon } from '@wordpress/icons'; import { useInstanceId } from '@wordpress/compose'; export type Option = { label: string; value: string | number; isGroupLabel?: boolean; isSubGroup?: boolean; }; /** * Extend the base Component to hook the `onFocusOutside` function. */ const DetectOutside = withFocusOutside( // @ts-ignore class extends Component { handleFocusOutside( event: Event ) { // @ts-ignore this.props.onFocusOutside( event ); } render() { return this.props.children; } } ); /** * RowProps Type */ type RowProps = { index: number; style: React.CSSProperties; data: { onChange: ( nextValue: string | number ) => void; options: Option[]; focusedItemIndex: number; query: string; id: string; }; }; /** * The Row component for the list items, * * @param {RowProps} args * @class */ const Row = ( { index, style, data }: RowProps ) => { const { id, options, onChange, focusedItemIndex, query } = data; const [ isFocused, setIsFocus ] = useState( false ); let label = options[ index ].label; const isGroupLabel = options[ index ].isGroupLabel || false; if ( isGroupLabel ) { return ( ); } if ( query.length > 3 ) { label = label.replace( new RegExp( `(${ query })`, 'gi' ), '$1' ); } const padding = options[ index ]?.isSubGroup ? 24 : 8; return ( { isExpanded && ( = 5 ? 120 : options.length * 24 } itemCount={ options.length } itemData={ { options, focusedItemIndex, query, id: instanceId, onChange: selectChange, } } itemSize={ 24 } width={ 248 } ref={ listRef } > { Row } ) } ); } ); export default VirtualizedComboBox;