Autocomplete with options limit

Limiting the number of options rendered at a time is the most efficient way to deal with large data sets. The example below filters a data set of 100,000 options and stops after the first 7 matches, so only 7 options are rendered at a time.

autocomplete
import { useState } from 'react';
import { Combobox, TextInput, useCombobox } from '@mantine/core';

const MOCKDATA = Array(100000)
  .fill(0)
  .map((_, index) => `Item ${index}`);

function getFilteredOptions(data: string[], searchQuery: string, limit: number) {
  const result: string[] = [];

  for (let i = 0; i < data.length; i += 1) {
    if (result.length === limit) {
      break;
    }

    if (data[i].toLowerCase().includes(searchQuery.trim().toLowerCase())) {
      result.push(data[i]);
    }
  }

  return result;
}

export function AutocompleteLimit() {
  const store = useCombobox({
    onDropdownClose: () => store.resetSelectedOption(),
  });
  const [value, setValue] = useState('');
  const filteredOptions = getFilteredOptions(MOCKDATA, value, 7);

  const options = filteredOptions.map((item) => (
    <Combobox.Option value={item} key={item}>
      {item}
    </Combobox.Option>
  ));

  return (
    <Combobox
      onOptionSubmit={(optionValue) => {
        setValue(optionValue);
        store.closeDropdown();
      }}
      withinPortal={false}
      store={store}
    >
      <Combobox.Target>
        <TextInput
          label="Pick value or type anything"
          placeholder="Pick value or type anything"
          value={value}
          onChange={(event) => {
            setValue(event.currentTarget.value);
            store.openDropdown();
            store.updateSelectedOptionIndex();
          }}
          onClick={() => store.openDropdown()}
          onFocus={() => store.openDropdown()}
          onBlur={() => store.closeDropdown()}
        />
      </Combobox.Target>

      <Combobox.Dropdown>
        <Combobox.Options>
          {options.length === 0 ? <Combobox.Empty>Nothing found</Combobox.Empty> : options}
        </Combobox.Options>
      </Combobox.Dropdown>
    </Combobox>
  );
}