Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[@mantine/core] Implement selectFirstOptionOnChange for MultiSelect #4997

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions src/mantine-core/src/components/MultiSelect/MultiSelect.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import React, { useEffect } from 'react';
import { useId, useUncontrolled } from '@mantine/hooks';
import {
BoxProps,
Expand Down Expand Up @@ -236,6 +236,12 @@ export const MultiSelect = factory<MultiSelectFactory>((_props, ref) => {
</Pill>
));

useEffect(() => {
if (selectFirstOptionOnChange) {
combobox.selectFirstOption();
}
}, [selectFirstOptionOnChange, _value]);

const clearButton = clearable && _value.length > 0 && !disabled && !readOnly && (
<Combobox.ClearButton
size={size as string}
Expand All @@ -247,6 +253,8 @@ export const MultiSelect = factory<MultiSelectFactory>((_props, ref) => {
/>
);

const filteredData = filterPickedValues({ data: parsedData, value: _value });

return (
<>
<Combobox
Expand Down Expand Up @@ -335,6 +343,7 @@ export const MultiSelect = factory<MultiSelectFactory>((_props, ref) => {
onChange={(event) => {
setSearchValue(event.currentTarget.value);
searchable && combobox.openDropdown();
selectFirstOptionOnChange && combobox.selectFirstOption();
}}
disabled={disabled}
readOnly={readOnly || !searchable}
Expand All @@ -346,17 +355,15 @@ export const MultiSelect = factory<MultiSelectFactory>((_props, ref) => {
</Combobox.DropdownTarget>

<OptionsDropdown
data={
hidePickedOptions ? filterPickedValues({ data: parsedData, value: _value }) : parsedData
}
data={hidePickedOptions ? filteredData : parsedData}
hidden={readOnly || disabled}
filter={filter}
search={_searchValue}
limit={limit}
hiddenWhenEmpty={
hidePickedOptions ||
!searchable ||
!nothingFoundMessage ||
(!searchable && _searchValue.trim().length !== 0)
(hidePickedOptions && filteredData.length === 0 && _searchValue.trim().length === 0)
}
withScrollArea={withScrollArea}
maxDropdownHeight={maxDropdownHeight}
Expand Down