Skip to content

Commit

Permalink
feat(ui5-textarea): add ariaLabel and ariaLabelledby properties (#2124)
Browse files Browse the repository at this point in the history
As the Input, the TextArea should support aria-label and aria-labelledby set on the custom element.
Note: internally we also set aria-label, using the base method "getEffectiveAriaLabelText" for the purpose.

Related to #2107
  • Loading branch information
ilhan007 authored Aug 20, 2020
1 parent c9ee25e commit c005478
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 5 deletions.
6 changes: 3 additions & 3 deletions packages/main/src/TextArea.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
?readonly="{{readonly}}"
?required="{{required}}"
aria-required="{{required}}"
aria-labelledby={{ariaLabelledBy}}
aria-describedby={{ariaDescribedBy}}
aria-label="{{ariaLabelText}}"
aria-describedby="{{ariaDescribedBy}}"
maxlength="{{_exceededTextProps.calcedMaxLength}}"
.value="{{value}}"
@input="{{_oninput}}"
Expand All @@ -39,7 +39,7 @@
{{> afterTextarea}}

{{#if showExceededText}}
<span id="{{_id}}-exceededText" class="ui5-textarea-exceeded-text">{{_exceededTextProps.exceededText}}</span>
<span class="ui5-textarea-exceeded-text">{{_exceededTextProps.exceededText}}</span>
{{/if}}

{{#if hasValueState}}
Expand Down
40 changes: 38 additions & 2 deletions packages/main/src/TextArea.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import UI5Element from "@ui5/webcomponents-base/dist/UI5Element.js";
import litRender from "@ui5/webcomponents-base/dist/renderer/LitRenderer.js";
import ResizeHandler from "@ui5/webcomponents-base/dist/delegate/ResizeHandler.js";
import Integer from "@ui5/webcomponents-base/dist/types/Integer.js";
import getEffectiveAriaLabelText from "@ui5/webcomponents-base/dist/util/getEffectiveAriaLabelText.js";
import { fetchI18nBundle, getI18nBundle } from "@ui5/webcomponents-base/dist/i18nBundle.js";
import { getFeature } from "@ui5/webcomponents-base/dist/FeaturesRegistry.js";
import { isIE } from "@ui5/webcomponents-base/dist/Device.js";
Expand Down Expand Up @@ -209,6 +210,31 @@ const metadata = {
type: String,
},

/**
* Defines the aria-label attribute for the textarea.
*
* @type {String}
* @since 1.0.0-rc.9
* @private
* @defaultvalue ""
*/
ariaLabel: {
type: String,
},


/**
* Receives id(or many ids) of the elements that label the textarea.
*
* @type {String}
* @defaultvalue ""
* @private
* @since 1.0.0-rc.9
*/
ariaLabelledby: {
type: String,
},

/**
* @private
*/
Expand Down Expand Up @@ -539,8 +565,18 @@ class TextArea extends UI5Element {
return this.disabled ? undefined : "0";
}

get ariaLabelledBy() {
return this.showExceededText ? `${this._id}-exceededText` : undefined;
get ariaLabelText() {
const effectiveAriaLabelText = getEffectiveAriaLabelText(this);

if (this.showExceededText) {
if (effectiveAriaLabelText) {
return `${effectiveAriaLabelText} ${this._exceededTextProps.exceededText}`;
}

return this._exceededTextProps.exceededText;
}

return effectiveAriaLabelText;
}

get ariaDescribedBy() {
Expand Down
6 changes: 6 additions & 0 deletions packages/main/test/pages/TextArea.html
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,12 @@
<ui5-textarea growing growing-max-lines="4"></ui5-textarea>
</section>

<section class="aria-label and aria-labelledby">
<span id="infoText">info text</span>
<ui5-textarea id="textAreaAriaLabel" aria-label="Hello World"></ui5-textarea>
<ui5-textarea id="textAreaAriaLabelledBy" maxlength="20" show-exceeded-text aria-labelledby="infoText" class="fixed-width fixed-height"></ui5-textarea>
</section>

<script>
var changeCounter = 0;
var inputCounter = 0;
Expand Down
12 changes: 12 additions & 0 deletions packages/main/test/specs/TextArea.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,18 @@ describe("Attributes propagation", () => {

assert.strictEqual(browser.$("#basic-textarea").shadow$("textarea").getValue(), sExpectedValue, "Value property was set correctly");
});

it("Tests aria-label and aria-labelledby", () => {
const textArea1 = browser.$("#textAreaAriaLabel").shadow$("textarea");
const textArea2 = browser.$("#textAreaAriaLabelledBy").shadow$("textarea");
const EXPECTED_ARIA_LABEL1 = "Hello World";
const EXPECTED_ARIA_LABEL2 = "info text 20 characters remaining";

assert.strictEqual(textArea1.getAttribute("aria-label"), EXPECTED_ARIA_LABEL1,
"The aria-label is correctly set internally.");
assert.strictEqual(textArea2.getAttribute("aria-label"), EXPECTED_ARIA_LABEL2,
"The aria-label is correctly set internally.");
});
});

describe("disabled and readonly textarea", () => {
Expand Down

0 comments on commit c005478

Please sign in to comment.