From aed1f38f17aeb25f29b9948c6134c8ed6364f1c6 Mon Sep 17 00:00:00 2001 From: gyeongmin kim Date: Fri, 22 Nov 2019 03:11:48 +0900 Subject: [PATCH] [Autocomplete] Ignore object keys in default filter (#18480) --- .../src/Autocomplete/Autocomplete.test.js | 34 +++++++++++++++++++ .../src/useAutocomplete/useAutocomplete.js | 20 ++++++++++- 2 files changed, 53 insertions(+), 1 deletion(-) diff --git a/packages/material-ui-lab/src/Autocomplete/Autocomplete.test.js b/packages/material-ui-lab/src/Autocomplete/Autocomplete.test.js index 88261758fb9b1c..bd00a2c31a4188 100644 --- a/packages/material-ui-lab/src/Autocomplete/Autocomplete.test.js +++ b/packages/material-ui-lab/src/Autocomplete/Autocomplete.test.js @@ -658,4 +658,38 @@ describe('', () => { expect(textbox.value).to.equal(''); }); }); + + describe('prop: filterOptions', () => { + it('should ignore object keys by default', () => { + const { queryAllByRole, getByRole } = render( + option.name} + renderInput={params => } + />, + ); + let options; + const textbox = getByRole('textbox'); + + options = queryAllByRole('option'); + expect(options.length).to.equal(2); + + fireEvent.change(textbox, { target: { value: 'value' } }); + options = queryAllByRole('option'); + expect(options.length).to.equal(0); + + fireEvent.change(textbox, { target: { value: 'one' } }); + options = queryAllByRole('option'); + expect(options.length).to.equal(1); + }); + }); }); diff --git a/packages/material-ui-lab/src/useAutocomplete/useAutocomplete.js b/packages/material-ui-lab/src/useAutocomplete/useAutocomplete.js index 2bdc11a27d4116..f833a0c0c37135 100644 --- a/packages/material-ui-lab/src/useAutocomplete/useAutocomplete.js +++ b/packages/material-ui-lab/src/useAutocomplete/useAutocomplete.js @@ -11,12 +11,30 @@ function stripDiacritics(string) { : string; } +function defaultStringify(value) { + if (value == null) { + return ''; + } + + if (typeof value === 'string') { + return value; + } + + if (typeof value === 'object') { + return Object.keys(value) + .map(key => value[key]) + .join(' '); + } + + return JSON.stringify(value); +} + export function createFilterOptions(config = {}) { const { ignoreAccents = true, ignoreCase = true, matchFrom = 'any', - stringify = JSON.stringify, + stringify = defaultStringify, trim = false, } = config;