Skip to content
Open
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: 3 additions & 1 deletion src/components/select/select.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,9 @@ export default class SlSelect extends ShoelaceElement implements ShoelaceFormCon
@state()
set value(val: string | string[]) {
if (this.multiple) {
val = Array.isArray(val) ? val : val.split(' ');
if (!Array.isArray(val)) {
val = typeof val === 'string' ? val.split(' ') : [val].filter(Boolean);
Copy link
Member

Choose a reason for hiding this comment

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

Why are we filtering here? While this removes empty values, wouldn't it also remove '0' and other falsey values that might be intended?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry for the late reply, I was on vacation. You are right, this should just filter out things like 'false' or 'undefined', but certainly not '0'. Will update this and rerequest a review 👍

}
} else {
val = Array.isArray(val) ? val.join(' ') : val;
}
Expand Down
41 changes: 41 additions & 0 deletions src/components/select/select.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,47 @@ describe('<sl-select>', () => {
* @ts-expect-error */
el.handleDocumentKeyDown(event);
});

describe('#2384: should not break on invalid values of the "value" prop when "multiple" is set', () => {
it('should support an empty string', async () => {
const el = await fixture<SlSelect>('<sl-select multiple></sl-select>');
el.value = '';
await expect(el.value).to.eql(['']);
});

it('should support a string', async () => {
const el = await fixture<SlSelect>('<sl-select multiple></sl-select>');
el.value = 'value';
await expect(el.value).to.eql(['value']);
});

it('should support none falsy values', async () => {
const el = await fixture<SlSelect>('<sl-select multiple></sl-select>');
// @ts-expect-error Testing for invalid values
el.value = 1;
await expect(el.value).to.eql([1]);
});

it('should not allow falsy values', async () => {
const el = await fixture<SlSelect>('<sl-select multiple></sl-select>');
// @ts-expect-error Testing for invalid values
el.value = false;
await expect(el.value).to.eql([]);
});

it('should not allow undefined values', async () => {
const el = await fixture<SlSelect>('<sl-select multiple></sl-select>');
// @ts-expect-error Testing for invalid values
el.value = undefined;
await expect(el.value).to.eql([]);
});

it('should support an array of strings', async () => {
const el = await fixture<SlSelect>('<sl-select multiple></sl-select>');
el.value = ['a', 'b', 'c'];
await expect(el.value).to.eql(['a', 'b', 'c']);
});
});
});

it('should open the listbox when any letter key is pressed with sl-select is on focus', async () => {
Expand Down