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 Paginate Passed Choices #7889

Merged
merged 1 commit into from
Jun 24, 2022
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
6 changes: 5 additions & 1 deletion packages/ra-core/src/form/choices/useChoicesContext.ts
Original file line number Diff line number Diff line change
@@ -9,7 +9,11 @@ export const useChoicesContext = <ChoicesType extends RaRecord = RaRecord>(
const context = useContext(ChoicesContext) as ChoicesContextValue<
ChoicesType
>;
const { data, ...list } = useList<ChoicesType>({ data: options.choices });
const { data, ...list } = useList<ChoicesType>({
data: options.choices,
// When not in a ChoicesContext, paginating does not make sense (e.g. AutocompleteInput).
perPage: Infinity,
});
const result = useMemo(
() => ({
allChoices: context?.allChoices ?? data,
18 changes: 17 additions & 1 deletion packages/ra-ui-materialui/src/input/AutocompleteInput.spec.tsx
Original file line number Diff line number Diff line change
@@ -12,7 +12,10 @@ import { SimpleForm } from '../form';

import { AutocompleteInput } from './AutocompleteInput';
import { useCreateSuggestionContext } from './useSupportCreateSuggestion';
import { InsideReferenceInput } from './AutocompleteInput.stories';
import {
InsideReferenceInput,
VeryLargeOptionsNumber,
} from './AutocompleteInput.stories';
import { act } from '@testing-library/react-hooks';
import { ReferenceArrayInput } from './ReferenceArrayInput';
import { AutocompleteArrayInput } from './AutocompleteArrayInput';
@@ -935,4 +938,17 @@ describe('<AutocompleteInput />', () => {
screen.getByText('No options');
});
});

it('should allow a very large number of choices', async () => {
render(<VeryLargeOptionsNumber />);
await waitFor(() => {
expect(screen.getByRole('textbox'));
});

screen.getByRole('textbox').click();
userEvent.type(screen.getByRole('textbox'), '1050');
await waitFor(() => {
screen.getByText(/Dalmatian #1050/);
});
});
});
40 changes: 40 additions & 0 deletions packages/ra-ui-materialui/src/input/AutocompleteInput.stories.tsx
Original file line number Diff line number Diff line change
@@ -9,6 +9,7 @@ import {
DialogActions,
Button,
Stack,
Typography,
} from '@mui/material';

import { Edit } from '../detail';
@@ -418,3 +419,42 @@ export const InsideReferenceInputWithCreationSupport = () => (
<Resource name="books" edit={BookEditWithReferenceAndCreationSupport} />
</Admin>
);

const DalmatianEdit = () => {
const dalmatians: any[] = [];
for (let index = 0; index < 1100; index++) {
dalmatians.push({
id: index + 1,
name: `Dalmatian #${index + 1}`,
altData: `altData #${index + 1}`,
});
}

return (
<Edit mutationMode="pessimistic">
<SimpleForm>
<Typography aria-label="count" variant="body2">
choices: {dalmatians.length}
</Typography>
<AutocompleteInput
source="dalmatians"
choices={dalmatians}
fullWidth
/>
</SimpleForm>
</Edit>
);
};

export const VeryLargeOptionsNumber = () => {
return (
<Admin
dataProvider={dataProvider}
history={createMemoryHistory({
initialEntries: ['/dalmatians/1'],
})}
>
<Resource name="dalmatians" edit={<DalmatianEdit />} />
</Admin>
);
};