Skip to content

Commit df48b46

Browse files
authored
[#2826] trim values before search (#2844)
* [#2826]trim values before searching * [#2826]add test * [#2826]add comment
1 parent bd05260 commit df48b46

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
describe('Trim values before search', () => {
2+
beforeEach(() => {
3+
cy.visit('/tests/index.html');
4+
});
5+
6+
it('Matches even if there are leading and trailing spaces', () => {
7+
cy.selectpicker({
8+
html: `
9+
<select class="selectpicker" data-live-search="true">
10+
${[
11+
'apple',
12+
'pear',
13+
'dragon fruit'
14+
].map((value) =>
15+
`<option value="${value}">${value}</option>`
16+
).join('')}}
17+
</select>
18+
`
19+
}).then(($select) => {
20+
const button = `[data-id="${$select[0].id}"]`;
21+
cy.get(button).click();
22+
$select.on('fetched.bs.select', cy.stub().as('fetched'));
23+
[{
24+
searchValue: ' app ', // leading and trailing spaces
25+
expectedContain: 'apple'
26+
}, {
27+
searchValue: '  pea', // both half and full space mixed
28+
expectedContain: 'pear'
29+
}, {
30+
searchValue: ' ', // text with half-space also match
31+
expectedContain: 'dragon fruit'
32+
}].forEach((test) => {
33+
cy.get('input').type(test.searchValue);
34+
cy.get('.dropdown-menu').find('li').first().should(($el) => {
35+
expect($el).to.have.class('active')
36+
expect($el).to.contain(test.expectedContain);
37+
});
38+
cy.get('.dropdown-menu').find('li').first().click();
39+
cy.get(button).contains(test.expectedContain).click();
40+
});
41+
});
42+
});
43+
});

js/bootstrap-select.js

+5
Original file line numberDiff line numberDiff line change
@@ -3007,6 +3007,11 @@
30073007

30083008
this.$searchbox.on('input propertychange', function () {
30093009
var searchValue = that.$searchbox[0].value;
3010+
var isWhitespace = /^\s*$/.test(searchValue);
3011+
if (!isWhitespace) {
3012+
// trim leading and trailing half-width spaces and full-width spaces.
3013+
searchValue = searchValue.replace(/^\s+|\s+$/g, '');
3014+
}
30103015

30113016
that.selectpicker.search.elements = [];
30123017
that.selectpicker.search.data = [];

0 commit comments

Comments
 (0)