Skip to content

Commit 46fb7cd

Browse files
authored
fix: previous values should not be reused as selected values (#121)
* fix: previous values should not be reused as selected values * chore: add more E2E tests to cover the bug fix
1 parent 824bc9f commit 46fb7cd

File tree

3 files changed

+17
-13
lines changed

3 files changed

+17
-13
lines changed

demo/src/getting-started.html

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,17 @@ <h5>GitHub <i class="fa fa-link"></i></h5>
1818
<section>
1919
<h5>CDN</h5>
2020
<p>
21-
<a href="https://www.jsdelivr.com/" target="__blank">jsDelivr</a> graciously provide CDNs for many JavaScript libraries including Multiple-Select-Vanilla. Just use
22-
the following CDN links.
21+
<a href="https://www.jsdelivr.com/" target="__blank">jsDelivr</a> graciously provide CDNs for many JavaScript libraries
22+
including Multiple-Select-Vanilla. Just use the following CDN links.
2323
</p>
2424

2525
<div style="background: #f7f7f7; padding: 10px">
2626
<pre>
2727
&lt;!-- Latest compiled and minified CSS --&gt;
28-
&lt;link href=&quot;<span style="color:#880000">https://cdn.jsdelivr.net/npm/[email protected].5/dist/styles/css/multiple-select.css</span>&quot; rel=&quot;<span style="color:#880000">stylesheet</span>&quot;&gt;
28+
&lt;link href=&quot;<span style="color:#880000">https://cdn.jsdelivr.net/npm/[email protected].9/dist/styles/css/multiple-select.css</span>&quot; rel=&quot;<span style="color:#880000">stylesheet</span>&quot;&gt;
2929

3030
&lt;!-- Latest compiled and minified JavaScript --&gt;
31-
&lt;script src=&quot;<span style="color:#880000">https://cdn.jsdelivr.net/npm/[email protected].5/dist/multiple-select.js</span>&quot;&gt;&lt;/script&gt;</pre>
31+
&lt;script src=&quot;<span style="color:#880000">https://cdn.jsdelivr.net/npm/[email protected].9/dist/multiple-select.js</span>&quot;&gt;&lt;/script&gt;</pre>
3232
</div>
3333
</section>
3434

lib/src/MultipleSelectInstance.ts

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -969,15 +969,9 @@ export class MultipleSelectInstance {
969969
this.elm.value = selectedValues.length ? selectedValues[0] : '';
970970
} else {
971971
// when multiple values could be set, we need to loop through each
972-
const selectOptions = this.elm.options;
973-
for (let i = 0, ln = selectOptions.length; i < ln; i++) {
974-
for (const selectedVal of selectedValues) {
975-
const isSelected = selectedVal === selectOptions[i].value;
976-
if (isSelected) {
977-
selectOptions[i].selected = isSelected;
978-
}
979-
}
980-
}
972+
Array.from(this.elm.options).forEach((option) => {
973+
option.selected = selectedValues.some((val) => val === option.value);
974+
});
981975
}
982976

983977
// trigger <select> change event

playwright/e2e/example07.spec.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,15 @@ test.describe('Example 07 - Submit Data', () => {
3131
await page.getByRole('button', { name: 'Third, Fourth' }).click();
3232
await page.getByTestId('submit').click();
3333
await expect(dialogText).toBe('select1=1&select2=3&select2=4');
34+
35+
// unselect 3,4 and select 1,2 instead
36+
await page.locator('[data-test=select2].ms-parent').click();
37+
await page.getByRole('listitem').filter({ hasText: 'Third' }).locator('span').click();
38+
await page.getByRole('listitem').filter({ hasText: 'Fourth' }).locator('span').click();
39+
await page.getByRole('listitem').filter({ hasText: 'First' }).locator('span').click();
40+
await page.getByRole('listitem').filter({ hasText: 'Second' }).locator('span').click();
41+
await page.locator('[data-test=select2].ms-parent').click();
42+
await page.getByTestId('submit').click();
43+
await expect(dialogText).toBe('select1=1&select2=1&select2=2');
3444
});
3545
});

0 commit comments

Comments
 (0)