Skip to content

Commit

Permalink
fix(sbb-toggle): deal with undefined option component
Browse files Browse the repository at this point in the history
  • Loading branch information
jeripeierSBB committed Jan 10, 2024
1 parent c4493bc commit e6eca79
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/components/toggle/toggle/toggle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,15 @@ export class SbbToggleElement extends LitElement {

private _valueChanged(value: any | undefined): void {
const options = this._options;
// If options are not yet defined, we can check if attribute is already set as a fallback.
// We do this by checking whether value property is available (defined component).
const selectedOption =
options.find((o) => o.value === value) ?? options.find((o) => o.checked) ?? options[0];
options.find(
(o) => value === ('value' in o ? o.value : (o as HTMLElement).getAttribute('value')),
) ??
options.find((o) => o.checked) ??
options[0];

if (!selectedOption) {
isBrowser() && console.warn(`sbb-toggle: No available options! (${this.id || 'No id'})`);
return;
Expand Down Expand Up @@ -152,7 +159,11 @@ export class SbbToggleElement extends LitElement {

const options = this._options;

if (options.every((o) => !o.checked) || !this._toggleElement) {
if (
options.every((o) => !o.checked) ||
options.every((o) => !o.clientWidth) ||
!this._toggleElement
) {
return;
}

Expand Down

0 comments on commit e6eca79

Please sign in to comment.