import * as React from 'react'; import TextField from '@mui/material/TextField'; import Autocomplete from '@mui/material/Autocomplete'; import useMediaQuery from '@mui/material/useMediaQuery'; import { useTheme } from '@mui/material/styles'; import { VariableSizeList, ListChildComponentProps } from 'react-window'; import Typography from '@mui/material/Typography'; const LISTBOX_PADDING = 9; function renderRow(props: ListChildComponentProps) { const { data, index, style } = props; const dataSet = data[index]; const inlineStyle = { ...style, top: (style.top as number) + LISTBOX_PADDING, }; const { label, sub_label, sub_label_variant } = dataSet[1]; let color = 'inherit'; if (sub_label_variant !== undefined) { color = sub_label_variant; } return ( {/* prettier-ignore */}
{label} {sub_label}
); } const OuterElementContext = React.createContext({}); const OuterElementType = React.forwardRef((props, ref) => { const outerProps = React.useContext(OuterElementContext); return
; }); function useResetCache(data: any) { const ref = React.useRef(null); React.useEffect(() => { if (ref.current != null) { ref.current.resetAfterIndex(0, true); } }, [data]); return ref; } // Adapter for react-window const ListboxComponent = React.forwardRef< HTMLDivElement, React.HTMLAttributes >(function ListboxComponent(props, ref) { const { children, ...other } = props; const itemData: React.ReactChild[] = []; (children as React.ReactChild[]).forEach( (item: React.ReactChild & { children?: React.ReactChild[] }) => { itemData.push(item); itemData.push(...(item.children || [])); }, ); const theme = useTheme(); const smUp = useMediaQuery(theme.breakpoints.up('sm'), { noSsr: true, }); const itemCount = itemData.length; const itemSize = smUp ? 36 : 48; const getChildSize = (child: React.ReactChild) => { // eslint-disable-next-line no-prototype-builtins if (child.hasOwnProperty('group')) { return 48; } return itemSize; }; const getHeight = () => { if (itemCount > 8) { return 8 * itemSize; } return itemData.map(getChildSize).reduce((a, b) => a + b, 0); }; const gridRef = useResetCache(itemCount); return (
getChildSize(itemData[index])} overscanCount={5} itemCount={itemCount} > {renderRow}
); }); const Virtualize = ({ options, onInputRef, textFieldProps, ...rest }: any) => { return ( option?.id === value?.value } renderInput={(params) => ( )} renderOption={(props, option, state) => [props, option, state.index] as React.ReactNode } // TODO: Post React 18 update - validate this conversion, look like a hidden bug renderGroup={(params) => params as unknown as React.ReactNode} {...rest} /> ); }; export default Virtualize;