From 0789cf44727b1a06c388828f17cd81746d142c25 Mon Sep 17 00:00:00 2001 From: Patrik Olsson Date: Thu, 19 Oct 2017 21:09:00 +0200 Subject: [PATCH] fix(Dropdown): make deburr opt-in, and deburr input --- .../Usage/DropdownExampleDeburrSearch.js | 24 ++++++++ .../Examples/modules/Dropdown/Usage/index.js | 5 ++ src/modules/Dropdown/Dropdown.d.ts | 3 + src/modules/Dropdown/Dropdown.js | 14 ++++- test/specs/modules/Dropdown/Dropdown-test.js | 55 +++++++++++++++++-- 5 files changed, 92 insertions(+), 9 deletions(-) create mode 100644 docs/app/Examples/modules/Dropdown/Usage/DropdownExampleDeburrSearch.js diff --git a/docs/app/Examples/modules/Dropdown/Usage/DropdownExampleDeburrSearch.js b/docs/app/Examples/modules/Dropdown/Usage/DropdownExampleDeburrSearch.js new file mode 100644 index 0000000000..650c575767 --- /dev/null +++ b/docs/app/Examples/modules/Dropdown/Usage/DropdownExampleDeburrSearch.js @@ -0,0 +1,24 @@ +import React from 'react' +import { Dropdown } from 'semantic-ui-react' + +const options = [ + { key: 'a', value: 'a', text: 'Café with accent' }, + { key: 'b', value: 'b', text: 'Cafe without accent' }, + { key: 'c', value: 'c', text: 'Déjà vu' }, + { key: 'd', value: 'd', text: 'Deja vu' }, + { key: 'e', value: 'e', text: 'Scandinavian å ä æ ø ö' }, + { key: 'f', value: 'f', text: 'Scandinavian a a ae o o' }, +] + +const DropdownExampleSearchSelection = () => ( + +) + +export default DropdownExampleSearchSelection diff --git a/docs/app/Examples/modules/Dropdown/Usage/index.js b/docs/app/Examples/modules/Dropdown/Usage/index.js index 58f85e4b70..fd70099a5b 100644 --- a/docs/app/Examples/modules/Dropdown/Usage/index.js +++ b/docs/app/Examples/modules/Dropdown/Usage/index.js @@ -100,6 +100,11 @@ const DropdownUsageExamples = () => ( selection. + { - const { multiple, search, allowAdditions, additionPosition, additionLabel } = this.props + const { multiple, search, allowAdditions, additionPosition, additionLabel, deburr } = this.props const { searchQuery } = this.state let filteredOptions = options @@ -802,9 +806,13 @@ export default class Dropdown extends Component { if (_.isFunction(search)) { filteredOptions = search(filteredOptions, searchQuery) } else { - const re = new RegExp(_.escapeRegExp(searchQuery), 'i') // remove diacritics on search - filteredOptions = _.filter(filteredOptions, opt => re.test(_.deburr(opt.text))) + const strippedQuery = deburr ? _.deburr(searchQuery) : searchQuery + + const re = new RegExp(_.escapeRegExp(strippedQuery), 'i') + + filteredOptions = _.filter(filteredOptions, opt => + re.test(deburr ? _.deburr(opt.text) : opt.text)) } } diff --git a/test/specs/modules/Dropdown/Dropdown-test.js b/test/specs/modules/Dropdown/Dropdown-test.js index bdf3cb9b90..ae1ad2fd00 100644 --- a/test/specs/modules/Dropdown/Dropdown-test.js +++ b/test/specs/modules/Dropdown/Dropdown-test.js @@ -658,22 +658,65 @@ describe('Dropdown', () => { .find('.selected') .should.contain.text('a2') }) - it('filter after diacritics', () => { + it('filters diacritics on options when using deburr prop', () => { + const inputText = 'floresti' + const textToFind = 'FLOREŞTI' + + const opts = [ + { text: textToFind, value: '1' }, + { text: `ŞANŢU ${textToFind}`, value: '2' }, + { text: `${textToFind} Alba`, value: '3' }, + ] + + // search for 'floresti' + wrapperMount() + .simulate('click') + .find('input.search') + .simulate('change', { target: { value: inputText } }) + + wrapper + .find('.selected') + .should.contain.text(textToFind) + }) + it('filters diacritics on input when using deburr prop', () => { + const inputText = 'FLORÉŞTI' + const textToFind = 'FLORESTI' + + const opts = [ + { text: textToFind, value: '1' }, + { text: `SANTU ${textToFind}`, value: '2' }, + { text: `${textToFind} Alba`, value: '3' }, + ] + + // search for 'floresti' + wrapperMount() + .simulate('click') + .find('input.search') + .simulate('change', { target: { value: inputText } }) + + wrapper + .find('.selected') + .should.contain.text(textToFind) + }) + it('should not filter diacritics when deburr is not set', () => { + const inputText = 'FLORÉŞTI' + const textToFind = 'FLORESTI' + const opts = [ - { text: 'FLOREŞTI', value: '1' }, - { text: 'ŞANŢU FLOREŞTI', value: '2' }, - { text: 'FLOREŞTI Alba', value: '3' }, + { text: textToFind, value: '1' }, + { text: `SANTU ${textToFind}`, value: '2' }, + { text: `${textToFind} Alba`, value: '3' }, ] // search for 'floresti' wrapperMount() .simulate('click') .find('input.search') - .simulate('change', { target: { value: 'floresti' } }) + .simulate('change', { target: { value: inputText } }) wrapper .find('.selected') - .should.contain.text('FLOREŞTI') + .should.not.contain.text(textToFind) }) it('still works after encountering "no results"', () => { const opts = [