Skip to content

Commit f34c82f

Browse files
皆虚youluna
皆虚
authored andcommitted
fix(CascaderSelect): should ignore case when searching
1 parent 359651f commit f34c82f

File tree

2 files changed

+42
-3
lines changed

2 files changed

+42
-3
lines changed

src/cascader-select/cascader-select.jsx

+13-3
Original file line numberDiff line numberDiff line change
@@ -246,18 +246,28 @@ class CascaderSelect extends Component {
246246
checkStrictly: false,
247247
showSearch: false,
248248
filter: (searchValue, path) => {
249-
return path.some(item => item.label.indexOf(searchValue) > -1);
249+
return path.some(
250+
item =>
251+
String(item.label)
252+
.toLowerCase()
253+
.indexOf(String(searchValue).toLowerCase()) > -1
254+
);
250255
},
251256
resultRender: (searchValue, path) => {
252257
const parts = [];
253258
path.forEach((item, i) => {
254-
const others = item.label.split(searchValue);
259+
const reExp = searchValue.replace(/[-.+*?^$()[\]{}|\\]/g, v => `\\${v}`);
260+
261+
const re = new RegExp(reExp, 'gi');
262+
const others = item.label.split(re);
263+
const matches = item.label.match(re);
264+
255265
others.forEach((other, j) => {
256266
if (other) {
257267
parts.push(other);
258268
}
259269
if (j < others.length - 1) {
260-
parts.push(<em key={`${i}-${j}`}>{searchValue}</em>);
270+
parts.push(<em key={`${i}-${j}`}>{matches[j]}</em>);
261271
}
262272
});
263273
if (i < path.length - 1) {

test/cascader-select/index-spec.js

+29
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,35 @@ describe('CascaderSelect', () => {
329329
assert(document.querySelector('.next-cascader-filtered-list em').textContent.trim() === '高陵');
330330
});
331331

332+
it('should ignore case when searching', () => {
333+
const SpecialChars = '-[.+*?^$()[]{}|\\';
334+
const dataSource = [
335+
{
336+
value: 'Aa',
337+
label: 'Aa',
338+
children: [
339+
{
340+
value: 'Bb',
341+
label: 'Bb',
342+
},
343+
{
344+
value: SpecialChars,
345+
label: SpecialChars,
346+
},
347+
],
348+
},
349+
];
350+
wrapper = mount(<CascaderSelect showSearch defaultVisible defaultValue="Aa" dataSource={dataSource} />);
351+
352+
const specialCharCases = SpecialChars.split('').map(c => [c, c]);
353+
354+
[['aa', 'Aa'], ['BB', 'Bb'], ...specialCharCases].forEach(([iptVal, excepted]) => {
355+
wrapper.find('.next-select-trigger-search input').simulate('change', { target: { value: iptVal } });
356+
wrapper.update();
357+
assert(document.querySelector('.next-cascader-filtered-list em').textContent.trim() === excepted);
358+
});
359+
});
360+
332361
it('should support keyborad', done => {
333362
wrapper = mount(<CascaderSelect dataSource={ChinaArea} />);
334363
wrapper.find('.next-select').simulate('click');

0 commit comments

Comments
 (0)