From 5806e8c00ca27fe18e172d1ac22f78348a2af061 Mon Sep 17 00:00:00 2001 From: Natrim Date: Tue, 12 Nov 2019 09:08:12 +0100 Subject: [PATCH 1/2] useSuggestions - don't crash on empty value --- packages/ra-core/src/form/useSuggestions.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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[]; From f34364c59bd0b9122d739fb6e55cb901daa760aa Mon Sep 17 00:00:00 2001 From: Natrim Date: Wed, 13 Nov 2019 08:32:42 +0100 Subject: [PATCH 2/2] useSuggestions - add falsy value test --- packages/ra-core/src/form/useSuggestions.spec.ts | 12 ++++++++++++ 1 file changed, 12 insertions(+) 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); + }); });