import type { IFieldDropdownOption } from '@automatisch/types'; import ListItemButton from '@mui/material/ListItemButton'; import ListItemText from '@mui/material/ListItemText'; import throttle from 'lodash/throttle'; import * as React from 'react'; import { FixedSizeList, ListChildComponentProps } from 'react-window'; import { Typography } from '@mui/material'; import SearchInput from 'components/SearchInput'; import useFormatMessage from 'hooks/useFormatMessage'; import { SearchInputWrapper } from './style'; interface OptionsProps { data: readonly IFieldDropdownOption[]; onOptionClick: (event: React.MouseEvent, option: any) => void; }; const SHORT_LIST_LENGTH = 4; const LIST_ITEM_HEIGHT = 64; const computeListHeight = (currentLength: number) => { const numberOfRenderedItems = Math.min(SHORT_LIST_LENGTH, currentLength); return LIST_ITEM_HEIGHT * numberOfRenderedItems; } const renderItemFactory = ({ onOptionClick }: Pick) => (props: ListChildComponentProps) => { const { index, style, data } = props; const suboption = data[index]; return ( onOptionClick(event, suboption)} data-test="power-input-suggestion-item" key={index} style={style} > ); }; const Options = (props: OptionsProps) => { const formatMessage = useFormatMessage(); const { data, onOptionClick } = props; const [filteredData, setFilteredData] = React.useState( data ); React.useEffect(function syncOptions() { setFilteredData((filteredData) => { if (filteredData.length === 0 && filteredData.length !== data.length) { return data; } return filteredData; }) }, [data]); const renderItem = React.useMemo(() => renderItemFactory({ onOptionClick }), [onOptionClick]); const onSearchChange = React.useMemo( () => throttle((event: React.ChangeEvent) => { const search = (event.target as HTMLInputElement).value.toLowerCase(); if (!search) { setFilteredData(data); return; } const newFilteredData = data.filter(option => `${option.label}\n${option.value}`.toLowerCase().includes(search.toLowerCase())); setFilteredData(newFilteredData); }, 400), [data] ); return ( <> {renderItem} {filteredData.length === 0 && ( theme.spacing(0, 0, 2, 2) }}> {formatMessage('customAutocomplete.noOptions')} )} ); }; export default Options;