This commit is contained in:
Cory LaViska
2023-03-07 11:03:03 -05:00
parent 6aaf17b81a
commit f2177dccaf
3 changed files with 17 additions and 0 deletions

View File

@@ -41,4 +41,14 @@ describe('<sl-option>', () => {
expect(slotChangeHandler).to.have.been.calledOnce;
});
it('should convert non-string values to string', async () => {
const el = await fixture<SlOption>(html` <sl-option>Text</sl-option> `);
// @ts-expect-error - intentional
el.value = 10;
await el.updateComplete;
expect(el.value).to.equal('10');
});
});

View File

@@ -92,6 +92,12 @@ export default class SlOption extends ShoelaceElement {
@watch('value')
handleValueChange() {
// Ensure the value is a string. This ensures the next line doesn't error and allows framework users to pass numbers
// instead of requiring them to cast the value to a string.
if (typeof this.value !== 'string') {
this.value = String(this.value);
}
if (this.value.includes(' ')) {
console.error(`Option values cannot include a space. All spaces have been replaced with underscores.`, this);
this.value = this.value.replace(/ /g, '_');