From a3c530714b12b8c6e7d3b9651ce6d06febfceab9 Mon Sep 17 00:00:00 2001 From: eggbread Date: Thu, 21 Nov 2019 20:17:01 +0900 Subject: [PATCH 1/2] Search includes object key values as match --- .../src/useAutocomplete/useAutocomplete.js | 20 +++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/packages/material-ui-lab/src/useAutocomplete/useAutocomplete.js b/packages/material-ui-lab/src/useAutocomplete/useAutocomplete.js index 2bdc11a27d4116..a019469b827506 100644 --- a/packages/material-ui-lab/src/useAutocomplete/useAutocomplete.js +++ b/packages/material-ui-lab/src/useAutocomplete/useAutocomplete.js @@ -10,13 +10,29 @@ function stripDiacritics(string) { ? string.normalize('NFD').replace(/[\u0300-\u036f]/g, '') : 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; From dcd2ae20aaf4c4c9a860c9f9d69d2ec5ba90c47e Mon Sep 17 00:00:00 2001 From: Olivier Tassinari Date: Thu, 21 Nov 2019 14:39:12 +0100 Subject: [PATCH 2/2] add test case --- .../src/Autocomplete/Autocomplete.test.js | 34 +++++++++++++++++++ .../src/useAutocomplete/useAutocomplete.js | 32 +++++++++-------- 2 files changed, 51 insertions(+), 15 deletions(-) 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 a019469b827506..f833a0c0c37135 100644 --- a/packages/material-ui-lab/src/useAutocomplete/useAutocomplete.js +++ b/packages/material-ui-lab/src/useAutocomplete/useAutocomplete.js @@ -10,23 +10,25 @@ function stripDiacritics(string) { ? string.normalize('NFD').replace(/[\u0300-\u036f]/g, '') : 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); + 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,