Skip to content
Merged
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
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export const CSS = {
assistiveText: "assistive-text",
characterLimit: "character-limit",
content: "content",
container: "container",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,10 @@ textarea.block-size--full {
@apply justify-end;
}

.assistive-text {
@apply sr-only;
}

@include hidden-form-input();
@include disabled();
@include base-component();
63 changes: 38 additions & 25 deletions packages/calcite-components/src/components/text-area/text-area.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import {
updateHostInteraction,
} from "../../utils/interactive";
import { CharacterLengthObj } from "./interfaces";
import { guid } from "../../utils/guid";

/**
* @slot - A slot for adding text.
Expand Down Expand Up @@ -260,12 +261,13 @@ export class TextArea
return (
<Host>
<textarea
aria-invalid={toAriaBoolean(this.value?.length > this.maxLength)}
aria-describedby={this.guid}
aria-invalid={toAriaBoolean(this.isCharacterLimitExceeded())}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

To mitigate with NVDA and FF, add in an aria-describedby to provide additional context.

aria-label={getLabelText(this)}
autofocus={this.autofocus}
class={{
[CSS.readOnly]: this.readOnly,
[CSS.textAreaInvalid]: this.value?.length > this.maxLength,
[CSS.textAreaInvalid]: this.isCharacterLimitExceeded(),
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Nitpick: this is called a few times during rendering, so maybe this could be a state prop computed when value or maxLength changes.

[CSS.footerSlotted]: this.endSlotHasElements && this.startSlotHasElements,
[CSS.blockSizeFull]: !hasFooter,
[CSS.borderColor]: !hasFooter,
Expand Down Expand Up @@ -317,6 +319,11 @@ export class TextArea
{this.renderCharacterLimit()}
</footer>
<HiddenFormInputSlot component={this} />
{this.isCharacterLimitExceeded() && (
<span aria-hidden={true} aria-live="polite" class={CSS.assistiveText} id={this.guid}>
{this.replacePlaceHoldersInMessages()}
</span>
)}
</Host>
);
}
Expand Down Expand Up @@ -371,6 +378,8 @@ export class TextArea
updateMessages(this, this.effectiveLocale);
}

private guid = guid();

//--------------------------------------------------------------------------
//
// Private Methods
Expand Down Expand Up @@ -410,7 +419,7 @@ export class TextArea
this.localizedCharacterLengthObj = this.getLocalizedCharacterLength();
return (
<span class={CSS.characterLimit}>
<span class={{ [CSS.characterOverLimit]: this.value?.length > this.maxLength }}>
<span class={{ [CSS.characterOverLimit]: this.isCharacterLimitExceeded() }}>
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

To support context with NVDA and FF, add a unique id, which can be referenced in the aria-describedby in the textarea, similar to combobox-item, where a guid is created.

{this.localizedCharacterLengthObj.currentLength}
</span>
{"/"}
Expand Down Expand Up @@ -454,32 +463,11 @@ export class TextArea

syncHiddenFormInput(input: HTMLInputElement): void {
input.setCustomValidity("");
if (this.value?.length > this.maxLength) {
if (this.isCharacterLimitExceeded()) {
input.setCustomValidity(this.replacePlaceHoldersInMessages());
}
}

private replacePlaceHoldersInMessages(): string {
return this.messages.tooLong
.replace("{maxLength}", this.localizedCharacterLengthObj.maxLength)
.replace("{currentLength}", this.localizedCharacterLengthObj.currentLength);
}

// height and width are set to auto here to avoid overlapping on to neighboring elements in the layout when user starts resizing.
// throttle is used to avoid flashing of textarea when user resizes.
private setHeightAndWidthToAuto = throttle(
(): void => {
if (this.resize === "vertical" || this.resize === "both") {
this.el.style.height = "auto";
}
if (this.resize === "horizontal" || this.resize === "both") {
this.el.style.width = "auto";
}
},
RESIZE_TIMEOUT,
{ leading: false }
);

setTextAreaEl = (el: HTMLTextAreaElement): void => {
this.textAreaEl = el;
this.resizeObserver.observe(el);
Expand Down Expand Up @@ -514,4 +502,29 @@ export class TextArea
footerWidth,
};
}

private replacePlaceHoldersInMessages(): string {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Nitpick: was the move of this function intentional?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

yup, this is intentional to move all the private method to the end.

return this.messages.tooLong
.replace("{maxLength}", this.localizedCharacterLengthObj.maxLength)
.replace("{currentLength}", this.localizedCharacterLengthObj.currentLength);
}

// height and width are set to auto here to avoid overlapping on to neighboring elements in the layout when user starts resizing.
// throttle is used to avoid flashing of textarea when user resizes.
private setHeightAndWidthToAuto = throttle(
(): void => {
if (this.resize === "vertical" || this.resize === "both") {
this.el.style.height = "auto";
}
if (this.resize === "horizontal" || this.resize === "both") {
this.el.style.width = "auto";
}
},
RESIZE_TIMEOUT,
{ leading: false }
);

private isCharacterLimitExceeded(): boolean {
return this.value?.length > this.maxLength;
}
}