Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#2826]trim values before search #2844

Merged
merged 3 commits into from
Dec 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -2951,6 +2951,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, '');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we're probably okay to just use searchValue = searchValue.trim(); instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@caseyjhol
thank you very much.
Since some users sometimes enter full-width spaces, so I have added support for trimming all half-width spaces and full-width spaces.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah gotcha. Maybe just mention this in a brief comment above this line.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have added a comment. thank you.

}

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