Skip to content

Commit

Permalink
[#2826] trim values before search (#2844)
Browse files Browse the repository at this point in the history
* [#2826]trim values before searching

* [#2826]add test

* [#2826]add comment
  • Loading branch information
megumiimai authored Dec 12, 2023
1 parent bd05260 commit df48b46
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
43 changes: 43 additions & 0 deletions cypress/integration/trim_values_before_search.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
describe('Trim values before search', () => {
beforeEach(() => {
cy.visit('/tests/index.html');
});

it('Matches even if there are leading and trailing spaces', () => {
cy.selectpicker({
html: `
<select class="selectpicker" data-live-search="true">
${[
'apple',
'pear',
'dragon fruit'
].map((value) =>
`<option value="${value}">${value}</option>`
).join('')}}
</select>
`
}).then(($select) => {
const button = `[data-id="${$select[0].id}"]`;
cy.get(button).click();
$select.on('fetched.bs.select', cy.stub().as('fetched'));
[{
searchValue: ' app ', // leading and trailing spaces
expectedContain: 'apple'
}, {
searchValue: '  pea', // both half and full space mixed
expectedContain: 'pear'
}, {
searchValue: ' ', // text with half-space also match
expectedContain: 'dragon fruit'
}].forEach((test) => {
cy.get('input').type(test.searchValue);
cy.get('.dropdown-menu').find('li').first().should(($el) => {
expect($el).to.have.class('active')
expect($el).to.contain(test.expectedContain);
});
cy.get('.dropdown-menu').find('li').first().click();
cy.get(button).contains(test.expectedContain).click();
});
});
});
});
5 changes: 5 additions & 0 deletions js/bootstrap-select.js
Original file line number Diff line number Diff line change
Expand Up @@ -3007,6 +3007,11 @@

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

that.selectpicker.search.elements = [];
that.selectpicker.search.data = [];
Expand Down

0 comments on commit df48b46

Please sign in to comment.