Skip to content

Commit

Permalink
[@mantine/core] MultiSelect: Fix selectFirstOptionOnChange prop not…
Browse files Browse the repository at this point in the history
… working (#4997)

* [@mantine/core] Implement selectFirstOptionOnChange for MultiSelect

* [@mantine/core] Fix MultiSelect nothingFoundMessage

* Prettier formatting

---------

Co-authored-by: Jaden Gregory <jaden.gregory@imcontrol.com.au>
  • Loading branch information
DoubleJ-G and Jaden Gregory committed Oct 12, 2023
1 parent 07d85c5 commit 66fe076
Showing 1 changed file with 13 additions and 6 deletions.
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

0 comments on commit 66fe076

Please sign in to comment.