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

Fix #6245 Select searchValue not changeable when value and searchValue are controlled #6272

Merged
merged 10 commits into from
Jun 13, 2024
18 changes: 18 additions & 0 deletions packages/@mantine/core/src/components/Select/Select.story.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,24 @@ export function SearchControlledValue() {
);
}

export function ControlledSearchAndValue() {
const [value, setValue] = useState<string | null>('Angular');
const [searchValue, setSearchValue] = useState('');

return (
<div style={{ padding: 40 }}>
<Select
searchable
searchValue={searchValue}
onSearchChange={setSearchValue}
data={['React', 'Angular', 'Svelte']}
value={value}
onChange={setValue}
/>
</div>
);
}

export function AllowDeselectFalse() {
return (
<div style={{ padding: 40 }}>
Expand Down
47 changes: 47 additions & 0 deletions packages/@mantine/core/src/components/Select/Select.test.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import React, { useState } from 'react';
import {
inputDefaultProps,
inputStylesApiSelectors,
Expand Down Expand Up @@ -149,4 +150,50 @@ describe('@mantine/core/Select', () => {
await userEvent.click(screen.getByRole('textbox', { name: 'Second' }));
expect(screen.queryByRole('listbox', { name: 'Second' })).toBeVisible();
});

it('supports dynamically changing data', async () => {
const Wrapper: React.FunctionComponent = () => {
const [data, setData] = useState([{ value: '1', label: 'initial-label' }]);
return (
<>
<Select label="First" data={data} value="1" />
<button type="button" onClick={() => setData([{ value: '1', label: 'new-label' }])}>
Set Data
</button>
</>
);
};

render(<Wrapper />);

expect(screen.getByRole('textbox')).toHaveValue('initial-label');
await userEvent.click(screen.getByRole('button'));
expect(screen.getByRole('textbox')).toHaveValue('new-label');
});

it('allows to change controlled search value when value is controlled and selected', async () => {
const Wrapper: React.FunctionComponent = () => {
const [value, setValue] = useState<string | null>('Angular');
const [searchValue, setSearchValue] = useState('');

return (
<Select
{...defaultProps}
value={value}
onChange={setValue}
searchable
searchValue={searchValue}
onSearchChange={setSearchValue}
data={['React', 'Angular', 'Svelte']}
/>
);
};

render(<Wrapper />);

await userEvent.click(screen.getByRole('textbox'));
// type backspace to remove last character
await userEvent.type(screen.getByRole('textbox'), '{backspace}');
expect(screen.getByRole('textbox')).toHaveValue('Angula');
});
});
11 changes: 9 additions & 2 deletions packages/@mantine/core/src/components/Select/Select.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useEffect, useMemo } from 'react';
import { useId, useUncontrolled } from '@mantine/hooks';
import { useId, usePrevious, useUncontrolled } from '@mantine/hooks';
import {
BoxProps,
ElementProps,
Expand Down Expand Up @@ -163,6 +163,8 @@ export const Select = factory<SelectFactory>((_props, ref) => {
});

const selectedOption = typeof _value === 'string' ? optionsLockup[_value] : undefined;
const previousSelectedOption = usePrevious(selectedOption);

const [search, setSearch] = useUncontrolled({
value: searchValue,
defaultValue: defaultSearchValue,
Expand Down Expand Up @@ -200,7 +202,12 @@ export const Select = factory<SelectFactory>((_props, ref) => {
setSearch('');
}

if (typeof value === 'string' && selectedOption) {
if (
typeof value === 'string' &&
selectedOption &&
(previousSelectedOption?.value !== selectedOption.value ||
previousSelectedOption?.label !== selectedOption.label)
) {
setSearch(selectedOption.label);
}
}, [value, selectedOption]);
Expand Down
Loading