Custom value pills
Use a custom component to render selected values
multiselect
Pick one or more values
United States
Great Britain
Finland
France
Russia
import { useState } from 'react';
import { CheckIcon, Combobox, Group, Input, Pill, PillsInput, useCombobox } from '@mantine/core';
import { countriesData, flags } from './countries-data';
import { CountryPill } from './CountryPill';
export function MultiSelectValueRenderer() {
const store = useCombobox({
onDropdownClose: () => store.resetSelectedOption(),
onDropdownOpen: () => store.updateSelectedOptionIndex('active'),
});
const [value, setValue] = useState<string[]>([]);
const handleValueSelect = (val: string) =>
setValue((current) =>
current.includes(val) ? current.filter((v) => v !== val) : [...current, val]
);
const handleValueRemove = (val: string) =>
setValue((current) => current.filter((v) => v !== val));
const values = value.map((item) => (
<CountryPill key={item} value={item} onRemove={() => handleValueRemove(item)}>
{item}
</CountryPill>
));
const options = countriesData.map((item) => {
const OptionFlag = flags[item.value];
return (
<Combobox.Option value={item.value} key={item.value} active={value.includes(item.value)}>
<Group gap="sm">
{value.includes(item.value) ? <CheckIcon size={12} /> : null}
<Group gap={7}>
<OptionFlag />
<span>{item.label}</span>
</Group>
</Group>
</Combobox.Option>
);
});
return (
<Combobox store={store} onOptionSubmit={handleValueSelect} withinPortal={false}>
<Combobox.DropdownTarget>
<PillsInput pointer onClick={() => store.toggleDropdown()}>
<Pill.Group>
{values.length > 0 ? (
values
) : (
<Input.Placeholder>Pick one or more values</Input.Placeholder>
)}
<Combobox.EventsTarget>
<PillsInput.Field
type="hidden"
aria-label="Pick one or more values"
onBlur={() => store.closeDropdown()}
onKeyDown={(event) => {
if (event.key === 'Backspace' && value.length > 0) {
event.preventDefault();
handleValueRemove(value[value.length - 1]);
}
}}
/>
</Combobox.EventsTarget>
</Pill.Group>
</PillsInput>
</Combobox.DropdownTarget>
<Combobox.Dropdown>
<Combobox.Options aria-multiselectable>{options}</Combobox.Options>
</Combobox.Dropdown>
</Combobox>
);
}