Skip to content

Commit

Permalink
Merge pull request #3971 from natrim/patch-19
Browse files Browse the repository at this point in the history
[RFR][v3] useSuggestions - don't crash on empty value
  • Loading branch information
fzaninotto authored Nov 13, 2019
2 parents 83a86f1 + f34364c commit a0e0893
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
12 changes: 12 additions & 0 deletions packages/ra-core/src/form/useSuggestions.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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);
});
});
3 changes: 2 additions & 1 deletion packages/ra-core/src/form/useSuggestions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[];
Expand Down

0 comments on commit a0e0893

Please sign in to comment.