Skip to content

Commit 5a8bcb7

Browse files
authored
Merge pull request #6256 from marmelab/fix-use-suggestions-with-selected-item-array
Fix useSugggestion tries to match selected items
2 parents e5ca11b + a73d6e2 commit 5a8bcb7

File tree

2 files changed

+35
-4
lines changed

2 files changed

+35
-4
lines changed

packages/ra-core/src/form/useSuggestions.spec.ts

+29
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,35 @@ describe('getSuggestions', () => {
110110
]);
111111
});
112112

113+
it('should not add createSuggestion if allowCreate is true and the current filter matches exactly the selected item', () => {
114+
expect(
115+
getSuggestions({
116+
...defaultOptions,
117+
selectedItem: { id: 1, value: 'one' },
118+
allowCreate: true,
119+
})('one')
120+
).toEqual([
121+
{ id: 2, value: 'two' },
122+
{ id: 3, value: 'three' },
123+
]);
124+
});
125+
126+
it('should add createSuggestion if allowCreate is true and selectedItem is an array', () => {
127+
expect(
128+
getSuggestions({
129+
...defaultOptions,
130+
selectedItem: [
131+
{ id: 1, value: 'one' },
132+
{ id: 2, value: 'two' },
133+
],
134+
allowCreate: true,
135+
})('')
136+
).toEqual([
137+
{ id: 3, value: 'three' },
138+
{ id: '@@create', value: 'ra.action.create' },
139+
]);
140+
});
141+
113142
it('should limit the number of choices', () => {
114143
expect(
115144
getSuggestions({

packages/ra-core/src/form/useSuggestions.ts

+6-4
Original file line numberDiff line numberDiff line change
@@ -226,11 +226,13 @@ export const getSuggestionsFactory = ({
226226
matchSuggestion(filter, suggestion, true)
227227
);
228228

229-
const filterIsSelectedItem = !!selectedItem
230-
? matchSuggestion(filter, selectedItem, true)
231-
: false;
232-
233229
if (allowCreate) {
230+
const filterIsSelectedItem =
231+
// If the selectedItem is an array (for example AutocompleteArrayInput)
232+
// we should't try to match
233+
!!selectedItem && !Array.isArray(selectedItem)
234+
? matchSuggestion(filter, selectedItem, true)
235+
: false;
234236
if (!hasExactMatch && !filterIsSelectedItem) {
235237
suggestions.push(
236238
getSuggestion({

0 commit comments

Comments
 (0)