diff --git a/packages/ra-core/src/form/useSuggestions.spec.ts b/packages/ra-core/src/form/useSuggestions.spec.ts index b0211289358..9a24ec91771 100644 --- a/packages/ra-core/src/form/useSuggestions.spec.ts +++ b/packages/ra-core/src/form/useSuggestions.spec.ts @@ -31,6 +31,12 @@ describe('getSuggestions', () => { { id: 1, value: 'one' }, { id: 2, value: 'two' }, ]); + expect( + getSuggestions({ + ...defaultOptions, + choices: [{ id: 0, value: '0' }, { id: 1, value: 'one' }], + })('0') + ).toEqual([{ id: 0, value: '0' }]); }); it('should filter choices according to the filter argument when it contains RegExp reserved characters', () => { @@ -108,4 +114,10 @@ describe('getSuggestions', () => { { id: 2, value: 'two' }, ]); }); + + it('should return all choices on empty/falsy values', () => { + expect(getSuggestions(defaultOptions)(undefined)).toEqual(choices); + expect(getSuggestions(defaultOptions)(false)).toEqual(choices); + expect(getSuggestions(defaultOptions)(null)).toEqual(choices); + }); }); diff --git a/packages/ra-core/src/form/useSuggestions.ts b/packages/ra-core/src/form/useSuggestions.ts index 8029593f2c5..52064f98600 100644 --- a/packages/ra-core/src/form/useSuggestions.ts +++ b/packages/ra-core/src/form/useSuggestions.ts @@ -84,7 +84,8 @@ const useSuggestions = ({ export default useSuggestions; -const escapeRegExp = value => value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string +const escapeRegExp = value => + value ? value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&') : ''; // $& means the whole matched string interface Options extends UseChoicesOptions { choices: any[];