-
Notifications
You must be signed in to change notification settings - Fork 89
feat(text-area): provide additional context for AT users when character limit exceeds #7412
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -46,6 +46,7 @@ import { | |
| updateHostInteraction, | ||
| } from "../../utils/interactive"; | ||
| import { CharacterLengthObj } from "./interfaces"; | ||
| import { guid } from "../../utils/guid"; | ||
|
|
||
| /** | ||
| * @slot - A slot for adding text. | ||
|
|
@@ -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())} | ||
| aria-label={getLabelText(this)} | ||
| autofocus={this.autofocus} | ||
| class={{ | ||
| [CSS.readOnly]: this.readOnly, | ||
| [CSS.textAreaInvalid]: this.value?.length > this.maxLength, | ||
| [CSS.textAreaInvalid]: this.isCharacterLimitExceeded(), | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
| [CSS.footerSlotted]: this.endSlotHasElements && this.startSlotHasElements, | ||
| [CSS.blockSizeFull]: !hasFooter, | ||
| [CSS.borderColor]: !hasFooter, | ||
|
|
@@ -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> | ||
| ); | ||
| } | ||
|
|
@@ -371,6 +378,8 @@ export class TextArea | |
| updateMessages(this, this.effectiveLocale); | ||
| } | ||
|
|
||
| private guid = guid(); | ||
|
|
||
| //-------------------------------------------------------------------------- | ||
| // | ||
| // Private Methods | ||
|
|
@@ -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() }}> | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To support context with NVDA and FF, add a unique |
||
| {this.localizedCharacterLengthObj.currentLength} | ||
| </span> | ||
| {"/"} | ||
|
|
@@ -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); | ||
|
|
@@ -514,4 +502,29 @@ export class TextArea | |
| footerWidth, | ||
| }; | ||
| } | ||
|
|
||
| private replacePlaceHoldersInMessages(): string { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nitpick: was the move of this function intentional?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
| } | ||
| } | ||
There was a problem hiding this comment.
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-describedbyto provide additional context.