Basic button
Primitive combobox with button target
button
🍎 Apples
🍌 Bananas
🥦 Broccoli
🥕 Carrots
🍫 Chocolate
🍇 Grapes
Selected item: Nothing selected
import { useState } from 'react';
import { Box, Button, Combobox, Text, useCombobox } from '@mantine/core';
const groceries = [
'🍎 Apples',
'🍌 Bananas',
'🥦 Broccoli',
'🥕 Carrots',
'🍫 Chocolate',
'🍇 Grapes',
];
export function BasicButton() {
const [selectedItem, setSelectedItem] = useState<string | null>(null);
const store = useCombobox({
onDropdownClose: () => store.resetSelectedOption(),
});
const options = groceries.map((item) => (
<Combobox.Option value={item} key={item}>
{item}
</Combobox.Option>
));
return (
<>
<Combobox
store={store}
width={250}
position="bottom-start"
withArrow
withinPortal={false}
onOptionSubmit={(val) => {
setSelectedItem(val);
store.closeDropdown();
}}
>
<Combobox.Target targetType="button">
<Button
aria-label="Pick item"
onClick={() => store.toggleDropdown()}
onBlur={() => store.closeDropdown()}
>
Pick item
</Button>
</Combobox.Target>
<Combobox.Dropdown>
<Combobox.Options>{options}</Combobox.Options>
</Combobox.Dropdown>
</Combobox>
<Box mt="xs">
<Text span size="sm">
Selected item:{' '}
</Text>
<Text span size="sm">
{selectedItem || 'Nothing selected'}
</Text>
</Box>
</>
);
}