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
24 changes: 24 additions & 0 deletions packages/main/cypress/specs/Switch.cy.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,30 @@ describe("General accesibility attributes", () => {
cy.get("[ui5-switch]")
.ui5SwitchCheckAttributeInShadowDomRoot("aria-required", "false");
});

it("Should have 'aria-describedby' attribute when readonly", () => {
cy.mount(<Switch readonly></Switch>);

cy.get("[ui5-switch]")
.then($switch => {
const switchId = ($switch.get(0) as Switch)._id;
const expectedDescribedBy = `${switchId}-readonly-desc`;

cy.wrap($switch)
.ui5SwitchCheckAttributeInShadowDomRoot("aria-describedby", expectedDescribedBy);
});
});

it("Should not have 'aria-describedby' attribute when not readonly", () => {
cy.mount(<Switch></Switch>);

cy.mount(<Switch></Switch>);

cy.get("[ui5-switch]")
.shadow()
.find(".ui5-switch-root")
.should("not.have.attr", "aria-describedby");
});
});

describe("General interactions in form", () => {
Expand Down
9 changes: 9 additions & 0 deletions packages/main/src/Switch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
FORM_CHECKABLE_REQUIRED,
SWITCH_ON,
SWITCH_OFF,
ACC_STATE_READONLY,
} from "./generated/i18n/i18n-defaults.js";

// Template
Expand Down Expand Up @@ -364,6 +365,14 @@ class Switch extends UI5Element implements IFormInputElement {
get ariaLabelText() {
return getEffectiveAriaLabelText(this) || getAssociatedLabelForTexts(this) || undefined;
}

get ariaDescribedBy() {
return this.readonly ? `${this._id}-readonly-desc` : undefined;
}

get ariaDescribedByText() {
return this.readonly ? Switch.i18nBundle.getText(ACC_STATE_READONLY) : "";
}
}

Switch.define();
Expand Down
3 changes: 3 additions & 0 deletions packages/main/src/SwitchTemplate.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export default function SwitchTemplate(this: Switch) {
aria-disabled={this.effectiveAriaDisabled}
aria-readonly={this.effectiveAriaReadonly}
aria-required={this.required}
aria-describedby={this.ariaDescribedBy}
onClick={this._onclick}
onKeyUp={this._onkeyup}
onKeyDown={this._onkeydown}
Expand Down Expand Up @@ -61,6 +62,8 @@ export default function SwitchTemplate(this: Switch) {
</>
}

{this.readonly && <span class="ui5-switch-text ui5-switch-text--readonly" part="readonly" id={this.ariaDescribedBy} aria-hidden={this._textAriaHidden}>{this.ariaDescribedByText}</span>}

<span class="ui5-switch-handle" part="handle"></span>
</div>
</div>
Expand Down
6 changes: 4 additions & 2 deletions packages/main/src/themes/Switch.css
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,12 @@
cursor: default;
}

.ui5-switch-root.ui5-switch--checked .ui5-switch-text--off {
.ui5-switch-root.ui5-switch--checked .ui5-switch-text--off,
.ui5-switch-root.ui5-switch--checked .ui5-switch-text--readonly {
visibility: var(--_ui5_switch_text_hidden);
}
.ui5-switch-root:not(.ui5-switch--checked) .ui5-switch-text--on {
.ui5-switch-root:not(.ui5-switch--checked) .ui5-switch-text--on,
.ui5-switch-root:not(.ui5-switch--checked) .ui5-switch-text--readonly {
visibility: var(--_ui5_switch_text_hidden);
}

Expand Down
Loading