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 AutocompleteInput flickers inside ReferenceInput #8599

Merged
merged 2 commits into from
Jan 27, 2023
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,12 @@ export const useReferenceArrayInputController = <
filter: { ...params.filter, ...filter },
meta,
},
{ retry: false, enabled: isGetMatchingEnabled, ...otherQueryOptions }
{
retry: false,
enabled: isGetMatchingEnabled,
keepPreviousData: true,
...otherQueryOptions,
}
);

// We merge the currently selected records with the matching ones, otherwise
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { AdminContext } from '../AdminContext';
import { SimpleForm } from '../form';
import { AutocompleteArrayInput } from './AutocompleteArrayInput';
import { useCreateSuggestionContext } from './useSupportCreateSuggestion';
import { InsideReferenceArrayInput } from './AutocompleteArrayInput.stories';

describe('<AutocompleteArrayInput />', () => {
const defaultProps = {
Expand Down Expand Up @@ -931,4 +932,60 @@ describe('<AutocompleteArrayInput />', () => {
);
expect(screen.queryAllByRole('option')).toHaveLength(2);
});

it('should display "No options" and not throw any error inside a ReferenceArrayInput field when referenced list is empty', async () => {
render(<InsideReferenceArrayInput />);
// Give time for the (previously thrown) error to happen
await new Promise(resolve => setTimeout(resolve, 1000));
await waitFor(() => {
screen.getByText('Author');
});
screen.getByRole('textbox').focus();
fireEvent.click(screen.getByLabelText('Clear value'));
fireEvent.change(screen.getByRole('textbox'), {
target: { value: 'plop' },
});
await waitFor(
() => {
screen.getByText('No options');
},
{ timeout: 2000 }
);
});

it('should not display "No options" inside a ReferenceArrayInput field when referenced list loading', async () => {
render(<InsideReferenceArrayInput />);
// Give time for the (previously thrown) error to happen
await new Promise(resolve => setTimeout(resolve, 1000));
await waitFor(() => {
screen.getByText('Author');
});
screen.getByRole('textbox').focus();
fireEvent.click(screen.getByLabelText('Clear value'));
fireEvent.change(screen.getByRole('textbox'), {
target: { value: 'Vic' },
});

// As the No options message might only be displayed after a small delay,
// we need to check for its presence for a few seconds.
// This test failed before the fix
const noOptionsAppeared = await new Promise(resolve => {
let noOptionsAppeared = false;
const checkForNoOptions = () => {
noOptionsAppeared = screen.queryByText('No options') != null;
if (noOptionsAppeared) {
clearInterval(interval);
resolve(noOptionsAppeared);
}
};

const interval = setInterval(checkForNoOptions, 100);
setTimeout(() => {
clearInterval(interval);
resolve(noOptionsAppeared);
}, 2000);
});

expect(noOptionsAppeared).toBe(false);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -819,7 +819,7 @@ const FanList = props => {
return data ? (
<>
{data.map(fan => (
<RecordContextProvider value={fan}>
<RecordContextProvider value={fan} key={fan.id}>
<Stack
direction="row"
alignItems="center"
Expand Down