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 useSugggestion tries to match selected items #6256

Merged
merged 1 commit into from
May 7, 2021
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
29 changes: 29 additions & 0 deletions packages/ra-core/src/form/useSuggestions.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,35 @@ describe('getSuggestions', () => {
]);
});

it('should not add createSuggestion if allowCreate is true and the current filter matches exactly the selected item', () => {
expect(
getSuggestions({
...defaultOptions,
selectedItem: { id: 1, value: 'one' },
allowCreate: true,
})('one')
).toEqual([
{ id: 2, value: 'two' },
{ id: 3, value: 'three' },
]);
});

it('should add createSuggestion if allowCreate is true and selectedItem is an array', () => {
expect(
getSuggestions({
...defaultOptions,
selectedItem: [
{ id: 1, value: 'one' },
{ id: 2, value: 'two' },
],
allowCreate: true,
})('')
).toEqual([
{ id: 3, value: 'three' },
{ id: '@@create', value: 'ra.action.create' },
]);
});

it('should limit the number of choices', () => {
expect(
getSuggestions({
Expand Down
10 changes: 6 additions & 4 deletions packages/ra-core/src/form/useSuggestions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,11 +226,13 @@ export const getSuggestionsFactory = ({
matchSuggestion(filter, suggestion, true)
);

const filterIsSelectedItem = !!selectedItem
? matchSuggestion(filter, selectedItem, true)
: false;

if (allowCreate) {
const filterIsSelectedItem =
// If the selectedItem is an array (for example AutocompleteArrayInput)
// we should't try to match
!!selectedItem && !Array.isArray(selectedItem)
? matchSuggestion(filter, selectedItem, true)
: false;
if (!hasExactMatch && !filterIsSelectedItem) {
suggestions.push(
getSuggestion({
Expand Down