Select 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.
select
Item 0
Item 1
Item 2
Item 3
Item 4
Item 5
Item 6
import { useState } from 'react';
import { Combobox, InputBase, 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 SelectLimit() {
const store = useCombobox({
onDropdownClose: () => store.resetSelectedOption(),
});
const [value, setValue] = useState<string | null>(null);
const [search, setSearch] = useState('');
const filteredOptions = getFilteredOptions(MOCKDATA, search, 7);
const options = filteredOptions.map((item) => (
<Combobox.Option value={item} key={item}>
{item}
</Combobox.Option>
));
return (
<Combobox
store={store}
withinPortal={false}
onOptionSubmit={(val) => {
setValue(val);
setSearch(val);
store.closeDropdown();
}}
>
<Combobox.Target>
<InputBase
rightSection={<Combobox.Chevron />}
value={search}
onChange={(event) => {
store.openDropdown();
store.updateSelectedOptionIndex();
setSearch(event.currentTarget.value);
}}
onClick={() => store.openDropdown()}
onFocus={() => store.openDropdown()}
onBlur={() => {
store.closeDropdown();
setSearch(value || '');
}}
placeholder="Search value"
aria-label="Search value"
rightSectionPointerEvents="none"
/>
</Combobox.Target>
<Combobox.Dropdown>
<Combobox.Options>
{options.length > 0 ? options : <Combobox.Empty>Nothing found</Combobox.Empty>}
</Combobox.Options>
</Combobox.Dropdown>
</Combobox>
);
}