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

Fix: Clear button reverses items order #1265

Merged
merged 2 commits into from
Feb 15, 2025
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
4 changes: 1 addition & 3 deletions public/assets/scripts/choices.js
Original file line number Diff line number Diff line change
Expand Up @@ -4232,9 +4232,7 @@
_this._removeItem(itemToRemove);
_this._triggerChange(itemToRemove.value);
if (_this._isSelectOneElement && !_this._hasNonChoicePlaceholder) {
var placeholderChoice = _this._store.choices
.reverse()
.find(function (choice) { return !choice.disabled && choice.placeholder; });
var placeholderChoice = (_this.config.shouldSort ? _this._store.choices.reverse() : _this._store.choices).find(function (choice) { return !choice.disabled && choice.placeholder; });
if (placeholderChoice) {
_this._addItem(placeholderChoice);
_this.unhighlightAll();
Expand Down
2 changes: 1 addition & 1 deletion public/assets/scripts/choices.min.js

Large diffs are not rendered by default.

46 changes: 46 additions & 0 deletions public/test/select-one/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,52 @@ <h2>Select one inputs</h2>
</script>
</div>

<div data-test-hook="remove-button-with-sorting-on">
<label for="choices-remove-button-with-sorting-on">Remove button with should sort On</label>
<select
class="form-control"
name="choices-remove-button-with-sorting-on"
id="choices-remove-button-with-sorting-on"
>
<option value="Choice 4" selected>Choice 4</option>
<option value="Choice 3">Choice 3</option>
<option value="Choice 2">Choice 2</option>
<option value="Choice 1">Choice 1</option>
</select>
<script>
document.addEventListener('DOMContentLoaded', function() {
new Choices('#choices-remove-button-with-sorting-on', {
allowHTML: true,
removeItemButton: true,
shouldSort: true
});
});
</script>
</div>

<div data-test-hook="remove-button-with-sorting-off">
<label for="choices-remove-button-with-sorting-off">Remove button with should sort off</label>
<select
class="form-control"
name="choices-remove-button-with-sorting-off"
id="choices-remove-button-with-sorting-off"
>
<option value="Choice 4" selected>Choice 4</option>
<option value="Choice 3">Choice 3</option>
<option value="Choice 2">Choice 2</option>
<option value="Choice 1">Choice 1</option>
</select>
<script>
document.addEventListener('DOMContentLoaded', function() {
new Choices('#choices-remove-button-with-sorting-off', {
allowHTML: true,
removeItemButton: true,
shouldSort: false
});
});
</script>
</div>

<div data-test-hook="no-press-to-select">
<label for="no-press-to-select">No press to select</label>
<select
Expand Down
6 changes: 3 additions & 3 deletions src/scripts/choices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1220,9 +1220,9 @@ class Choices {
this._triggerChange(itemToRemove.value);

if (this._isSelectOneElement && !this._hasNonChoicePlaceholder) {
const placeholderChoice = this._store.choices
.reverse()
.find((choice) => !choice.disabled && choice.placeholder);
const placeholderChoice = (this.config.shouldSort ? this._store.choices.reverse() : this._store.choices).find(
(choice) => !choice.disabled && choice.placeholder,
);
if (placeholderChoice) {
this._addItem(placeholderChoice);
this.unhighlightAll();
Expand Down
36 changes: 36 additions & 0 deletions test-e2e/tests/select-one.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,42 @@ describe(`Choices - select one`, () => {
await suite.expectedValue('');
await suite.expectHiddenDropdown();
});

describe('with should sort', () => {
test('on', async ({ page, bundle }) => {
const suite = new SelectTestSuit(page, bundle, testUrl, 'remove-button-with-sorting-on');
await suite.startWithClick();
await suite.expectVisibleDropdown();

await suite.items.getByRole('button', { name: 'Remove item' }).last().click();
await suite.advanceClock();

const firstChoice = suite.choices.first();
await expect(firstChoice).toHaveText('Choice 1');
const lastChoice = suite.choices.last();
await expect(lastChoice).toHaveText('Choice 4');

await suite.escapeKey();
await suite.expectHiddenDropdown();
});

test('off', async ({ page, bundle }) => {
const suite = new SelectTestSuit(page, bundle, testUrl, 'remove-button-with-sorting-off');
await suite.startWithClick();
await suite.expectVisibleDropdown();

await suite.items.getByRole('button', { name: 'Remove item' }).last().click();
await suite.advanceClock();

const firstChoice = suite.choices.first();
await expect(firstChoice).toHaveText('Choice 4');
const lastChoice = suite.choices.last();
await expect(lastChoice).toHaveText('Choice 1');

await suite.escapeKey();
await suite.expectHiddenDropdown();
});
});
});
});

Expand Down