Dropdown with ScrollArea
Dropdown with ScrollArea.Autosize scrollbars
dropdown
🍎 Apples
🍌 Bananas
🥦 Broccoli
🥕 Carrots
🍫 Chocolate
🍇 Grapes
🍋 Lemon
🥬 Lettuce
🍄 Mushrooms
🍊 Oranges
🥔 Potatoes
🍅 Tomatoes
🥚 Eggs
🥛 Milk
🍞 Bread
🍗 Chicken
🍔 Hamburger
🧀 Cheese
🥩 Steak
🍟 French Fries
🍕 Pizza
🥦 Cauliflower
🥜 Peanuts
🍦 Ice Cream
🍯 Honey
🥖 Baguette
🍣 Sushi
🥝 Kiwi
🍓 Strawberries
import { useState } from 'react';
import { Combobox, TextInput, useCombobox } from '@mantine/core';
const groceries = [
'🍎 Apples',
'🍌 Bananas',
'🥦 Broccoli',
'🥕 Carrots',
'🍫 Chocolate',
'🍇 Grapes',
'🍋 Lemon',
'🥬 Lettuce',
'🍄 Mushrooms',
'🍊 Oranges',
'🥔 Potatoes',
'🍅 Tomatoes',
'🥚 Eggs',
'🥛 Milk',
'🍞 Bread',
'🍗 Chicken',
'🍔 Hamburger',
'🧀 Cheese',
'🥩 Steak',
'🍟 French Fries',
'🍕 Pizza',
'🥦 Cauliflower',
'🥜 Peanuts',
'🍦 Ice Cream',
'🍯 Honey',
'🥖 Baguette',
'🍣 Sushi',
'🥝 Kiwi',
'🍓 Strawberries',
];
export function DropdownScrollArea() {
const store = useCombobox({
onDropdownClose: () => store.resetSelectedOption(),
});
const [value, setValue] = useState('');
const shouldFilterOptions = !groceries.some((item) => item === value);
const filteredOptions = shouldFilterOptions
? groceries.filter((item) => item.toLowerCase().includes(value.toLowerCase().trim()))
: groceries;
const options = filteredOptions.map((item) => (
<Combobox.Option value={item} key={item}>
{item}
</Combobox.Option>
));
return (
<Combobox
onOptionSubmit={(optionValue) => {
setValue(optionValue);
store.closeDropdown();
}}
store={store}
withinPortal={false}
>
<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.ScrollArea mah={200} viewportProps={{ tabIndex: -1 }}>
<Combobox.Options>
{options.length === 0 ? <Combobox.Empty>Nothing found</Combobox.Empty> : options}
</Combobox.Options>
</Combobox.ScrollArea>
</Combobox.Dropdown>
</Combobox>
);
}